ntdll: Fill the object type index in System(Extended)HandleInformation.
[wine.git] / server / thread.c
blob3cee717e169f4f2e11570366cfb2a3dcae96576c
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;
493 if (apc->caller) release_object( apc->caller );
494 if (apc->owner) release_object( apc->owner );
497 /* queue an async procedure call */
498 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
500 struct thread_apc *apc;
502 if ((apc = alloc_object( &thread_apc_ops )))
504 apc->call = *call_data;
505 apc->caller = NULL;
506 apc->owner = owner;
507 apc->executed = 0;
508 apc->result.type = APC_NONE;
509 if (owner) grab_object( owner );
511 return apc;
514 /* get a thread pointer from a thread id (and increment the refcount) */
515 struct thread *get_thread_from_id( thread_id_t id )
517 struct object *obj = get_ptid_entry( id );
519 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
520 set_error( STATUS_INVALID_CID );
521 return NULL;
524 /* get a thread from a handle (and increment the refcount) */
525 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
527 return (struct thread *)get_handle_obj( current->process, handle,
528 access, &thread_ops );
531 /* find a thread from a Unix tid */
532 struct thread *get_thread_from_tid( int tid )
534 struct thread *thread;
536 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
538 if (thread->unix_tid == tid) return thread;
540 return NULL;
543 /* find a thread from a Unix pid */
544 struct thread *get_thread_from_pid( int pid )
546 struct thread *thread;
548 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
550 if (thread->unix_pid == pid) return thread;
552 return NULL;
555 int set_thread_affinity( struct thread *thread, affinity_t affinity )
557 int ret = 0;
558 #ifdef HAVE_SCHED_SETAFFINITY
559 if (thread->unix_tid != -1)
561 cpu_set_t set;
562 int i;
563 affinity_t mask;
565 CPU_ZERO( &set );
566 for (i = 0, mask = 1; mask; i++, mask <<= 1)
567 if (affinity & mask) CPU_SET( i, &set );
569 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
571 #endif
572 if (!ret) thread->affinity = affinity;
573 return ret;
576 affinity_t get_thread_affinity( struct thread *thread )
578 affinity_t mask = 0;
579 #ifdef HAVE_SCHED_SETAFFINITY
580 if (thread->unix_tid != -1)
582 cpu_set_t set;
583 unsigned int i;
585 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
586 for (i = 0; i < 8 * sizeof(mask); i++)
587 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
589 #endif
590 if (!mask) mask = ~(affinity_t)0;
591 return mask;
594 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
595 #define THREAD_PRIORITY_REALTIME_LOWEST -7
597 /* set all information about a thread */
598 static void set_thread_info( struct thread *thread,
599 const struct set_thread_info_request *req )
601 if (req->mask & SET_THREAD_INFO_PRIORITY)
603 int max = THREAD_PRIORITY_HIGHEST;
604 int min = THREAD_PRIORITY_LOWEST;
605 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
607 max = THREAD_PRIORITY_REALTIME_HIGHEST;
608 min = THREAD_PRIORITY_REALTIME_LOWEST;
610 if ((req->priority >= min && req->priority <= max) ||
611 req->priority == THREAD_PRIORITY_IDLE ||
612 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
613 thread->priority = req->priority;
614 else
615 set_error( STATUS_INVALID_PARAMETER );
617 if (req->mask & SET_THREAD_INFO_AFFINITY)
619 if ((req->affinity & thread->process->affinity) != req->affinity)
620 set_error( STATUS_INVALID_PARAMETER );
621 else if (thread->state == TERMINATED)
622 set_error( STATUS_THREAD_IS_TERMINATING );
623 else if (set_thread_affinity( thread, req->affinity ))
624 file_set_error();
626 if (req->mask & SET_THREAD_INFO_TOKEN)
627 security_set_thread_token( thread, req->token );
628 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
629 thread->entry_point = req->entry_point;
630 if (req->mask & SET_THREAD_INFO_DBG_HIDDEN)
631 thread->dbg_hidden = 1;
632 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
634 WCHAR *desc;
635 data_size_t desc_len = get_req_data_size();
637 if (desc_len)
639 if ((desc = mem_alloc( desc_len )))
641 memcpy( desc, get_req_data(), desc_len );
642 free( thread->desc );
643 thread->desc = desc;
644 thread->desc_len = desc_len;
647 else
649 free( thread->desc );
650 thread->desc = NULL;
651 thread->desc_len = 0;
656 /* stop a thread (at the Unix level) */
657 void stop_thread( struct thread *thread )
659 if (thread->context) return; /* already suspended, no need for a signal */
660 if (!(thread->context = create_thread_context( thread ))) return;
661 /* can't stop a thread while initialisation is in progress */
662 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
665 /* suspend a thread */
666 int suspend_thread( struct thread *thread )
668 int old_count = thread->suspend;
669 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
671 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
673 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
674 return old_count;
677 /* resume a thread */
678 int resume_thread( struct thread *thread )
680 int old_count = thread->suspend;
681 if (thread->suspend > 0)
683 if (!(--thread->suspend)) resume_delayed_debug_events( thread );
684 if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
686 return old_count;
689 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
690 int add_queue( struct object *obj, struct wait_queue_entry *entry )
692 grab_object( obj );
693 entry->obj = obj;
694 list_add_tail( &obj->wait_queue, &entry->entry );
695 return 1;
698 /* remove a thread from an object wait queue */
699 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
701 list_remove( &entry->entry );
702 release_object( obj );
705 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
707 return entry->wait->thread;
710 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
712 return entry->wait->select;
715 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
717 return entry->wait->key;
720 void make_wait_abandoned( struct wait_queue_entry *entry )
722 entry->wait->abandoned = 1;
725 /* finish waiting */
726 static unsigned int end_wait( struct thread *thread, unsigned int status )
728 struct thread_wait *wait = thread->wait;
729 struct wait_queue_entry *entry;
730 int i;
732 assert( wait );
733 thread->wait = wait->next;
735 if (status < wait->count) /* wait satisfied, tell it to the objects */
737 if (wait->select == SELECT_WAIT_ALL)
739 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
740 entry->obj->ops->satisfied( entry->obj, entry );
742 else
744 entry = wait->queues + status;
745 entry->obj->ops->satisfied( entry->obj, entry );
747 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
749 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
750 entry->obj->ops->remove_queue( entry->obj, entry );
751 if (wait->user) remove_timeout_user( wait->user );
752 free( wait );
753 return status;
756 /* build the thread wait structure */
757 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
758 int flags, abstime_t when )
760 struct thread_wait *wait;
761 struct wait_queue_entry *entry;
762 unsigned int i;
764 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
765 wait->next = current->wait;
766 wait->thread = current;
767 wait->count = count;
768 wait->flags = flags;
769 wait->select = select_op->op;
770 wait->cookie = 0;
771 wait->user = NULL;
772 wait->when = when;
773 wait->abandoned = 0;
774 current->wait = wait;
776 for (i = 0, entry = wait->queues; i < count; i++, entry++)
778 struct object *obj = objects[i];
779 entry->wait = wait;
780 if (!obj->ops->add_queue( obj, entry ))
782 wait->count = i;
783 end_wait( current, get_error() );
784 return 0;
787 return 1;
790 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
791 int flags, abstime_t when )
793 struct object *objects[MAXIMUM_WAIT_OBJECTS];
794 unsigned int i;
795 int ret = 0;
797 assert( count <= MAXIMUM_WAIT_OBJECTS );
799 for (i = 0; i < count; i++)
800 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
801 break;
803 if (i == count) ret = wait_on( select_op, count, objects, flags, when );
805 while (i > 0) release_object( objects[--i] );
806 return ret;
809 /* check if the thread waiting condition is satisfied */
810 static int check_wait( struct thread *thread )
812 int i;
813 struct thread_wait *wait = thread->wait;
814 struct wait_queue_entry *entry;
816 assert( wait );
818 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
819 return STATUS_KERNEL_APC;
821 /* Suspended threads may not acquire locks, but they can run system APCs */
822 if (thread->process->suspend + thread->suspend > 0) return -1;
824 if (wait->select == SELECT_WAIT_ALL)
826 int not_ok = 0;
827 /* Note: we must check them all anyway, as some objects may
828 * want to do something when signaled, even if others are not */
829 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
830 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
831 if (!not_ok) return STATUS_WAIT_0;
833 else
835 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
836 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
839 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
840 if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
841 if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
842 return -1;
845 /* send the wakeup signal to a thread */
846 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
848 struct wake_up_reply reply;
849 int ret;
851 /* check if we're waking current suspend wait */
852 if (thread->context && thread->suspend_cookie == cookie
853 && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
855 if (!thread->context->regs.flags)
857 release_object( thread->context );
858 thread->context = NULL;
860 else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
863 memset( &reply, 0, sizeof(reply) );
864 reply.cookie = cookie;
865 reply.signaled = signaled;
866 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
867 return 0;
868 if (ret >= 0)
869 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
870 else if (errno == EPIPE)
871 kill_thread( thread, 0 ); /* normal death */
872 else
873 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
874 return -1;
877 /* attempt to wake up a thread */
878 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
879 int wake_thread( struct thread *thread )
881 int signaled, count;
882 client_ptr_t cookie;
884 for (count = 0; thread->wait; count++)
886 if ((signaled = check_wait( thread )) == -1) break;
888 cookie = thread->wait->cookie;
889 signaled = end_wait( thread, signaled );
890 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
891 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
893 if (!count) count = -1;
894 break;
897 return count;
900 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
901 int wake_thread_queue_entry( struct wait_queue_entry *entry )
903 struct thread_wait *wait = entry->wait;
904 struct thread *thread = wait->thread;
905 int signaled;
906 client_ptr_t cookie;
908 if (thread->wait != wait) return 0; /* not the current wait */
909 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
911 assert( wait->select != SELECT_WAIT_ALL );
913 cookie = wait->cookie;
914 signaled = end_wait( thread, entry - wait->queues );
915 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
917 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
918 wake_thread( thread ); /* check other waits too */
920 return 1;
923 /* thread wait timeout */
924 static void thread_timeout( void *ptr )
926 struct thread_wait *wait = ptr;
927 struct thread *thread = wait->thread;
928 client_ptr_t cookie = wait->cookie;
930 wait->user = NULL;
931 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
932 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
934 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
935 end_wait( thread, STATUS_TIMEOUT );
937 assert( cookie );
938 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
939 /* check if other objects have become signaled in the meantime */
940 wake_thread( thread );
943 /* try signaling an event flag, a semaphore or a mutex */
944 static int signal_object( obj_handle_t handle )
946 struct object *obj;
947 int ret = 0;
949 obj = get_handle_obj( current->process, handle, 0, NULL );
950 if (obj)
952 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
953 release_object( obj );
955 return ret;
958 /* select on a list of handles */
959 static void select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
960 int flags, abstime_t when )
962 int ret;
963 unsigned int count;
964 struct object *object;
966 switch (select_op->op)
968 case SELECT_NONE:
969 if (!wait_on( select_op, 0, NULL, flags, when )) return;
970 break;
972 case SELECT_WAIT:
973 case SELECT_WAIT_ALL:
974 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
975 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
977 set_error( STATUS_INVALID_PARAMETER );
978 return;
980 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
981 return;
982 break;
984 case SELECT_SIGNAL_AND_WAIT:
985 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
986 return;
987 if (select_op->signal_and_wait.signal)
989 if (!signal_object( select_op->signal_and_wait.signal ))
991 end_wait( current, get_error() );
992 return;
994 /* check if we woke ourselves up */
995 if (!current->wait) return;
997 break;
999 case SELECT_KEYED_EVENT_WAIT:
1000 case SELECT_KEYED_EVENT_RELEASE:
1001 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
1002 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
1003 if (!object) return;
1004 ret = wait_on( select_op, 1, &object, flags, when );
1005 release_object( object );
1006 if (!ret) return;
1007 current->wait->key = select_op->keyed_event.key;
1008 break;
1010 default:
1011 set_error( STATUS_INVALID_PARAMETER );
1012 return;
1015 if ((ret = check_wait( current )) != -1)
1017 /* condition is already satisfied */
1018 set_error( end_wait( current, ret ));
1019 return;
1022 /* now we need to wait */
1023 if (current->wait->when != TIMEOUT_INFINITE)
1025 if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
1026 thread_timeout, current->wait )))
1028 end_wait( current, get_error() );
1029 return;
1032 current->wait->cookie = cookie;
1033 set_error( STATUS_PENDING );
1034 return;
1037 /* attempt to wake threads sleeping on the object wait queue */
1038 void wake_up( struct object *obj, int max )
1040 struct list *ptr;
1041 int ret;
1043 LIST_FOR_EACH( ptr, &obj->wait_queue )
1045 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
1046 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
1047 if (ret > 0 && max && !--max) break;
1048 /* restart at the head of the list since a wake up can change the object wait queue */
1049 ptr = &obj->wait_queue;
1053 /* return the apc queue to use for a given apc type */
1054 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
1056 switch(type)
1058 case APC_NONE:
1059 case APC_USER:
1060 case APC_TIMER:
1061 return &thread->user_apc;
1062 default:
1063 return &thread->system_apc;
1067 /* check if thread is currently waiting for a (system) apc */
1068 static inline int is_in_apc_wait( struct thread *thread )
1070 return (thread->process->suspend || thread->suspend ||
1071 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
1074 /* queue an existing APC to a given thread */
1075 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
1077 struct list *queue;
1079 if (thread && thread->state == TERMINATED && process)
1080 thread = NULL;
1082 if (!thread) /* find a suitable thread inside the process */
1084 struct thread *candidate;
1086 /* first try to find a waiting thread */
1087 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1089 if (candidate->state == TERMINATED) continue;
1090 if (is_in_apc_wait( candidate ))
1092 thread = candidate;
1093 break;
1096 if (!thread)
1098 /* then use the first one that accepts a signal */
1099 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1101 if (send_thread_signal( candidate, SIGUSR1 ))
1103 thread = candidate;
1104 break;
1108 if (!thread) return 0; /* nothing found */
1109 queue = get_apc_queue( thread, apc->call.type );
1111 else
1113 if (thread->state == TERMINATED) return 0;
1114 queue = get_apc_queue( thread, apc->call.type );
1115 /* send signal for system APCs if needed */
1116 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1118 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1120 /* cancel a possible previous APC with the same owner */
1121 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1124 grab_object( apc );
1125 list_add_tail( queue, &apc->entry );
1126 if (!list_prev( queue, &apc->entry )) /* first one */
1127 wake_thread( thread );
1129 return 1;
1132 /* queue an async procedure call */
1133 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1135 struct thread_apc *apc;
1136 int ret = 0;
1138 if ((apc = create_apc( owner, call_data )))
1140 ret = queue_apc( process, thread, apc );
1141 release_object( apc );
1143 return ret;
1146 /* cancel the async procedure call owned by a specific object */
1147 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1149 struct thread_apc *apc;
1150 struct list *queue = get_apc_queue( thread, type );
1152 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1154 if (apc->owner != owner) continue;
1155 list_remove( &apc->entry );
1156 apc->executed = 1;
1157 wake_up( &apc->obj, 0 );
1158 release_object( apc );
1159 return;
1163 /* remove the head apc from the queue; the returned object must be released by the caller */
1164 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
1166 struct thread_apc *apc = NULL;
1167 struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
1169 if (ptr)
1171 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1172 list_remove( ptr );
1174 return apc;
1177 /* clear an APC queue, cancelling all the APCs on it */
1178 static void clear_apc_queue( struct list *queue )
1180 struct list *ptr;
1182 while ((ptr = list_head( queue )))
1184 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1185 list_remove( &apc->entry );
1186 apc->executed = 1;
1187 wake_up( &apc->obj, 0 );
1188 release_object( apc );
1192 /* add an fd to the inflight list */
1193 /* return list index, or -1 on error */
1194 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1196 int i;
1198 if (server == -1) return -1;
1199 if (client == -1)
1201 close( server );
1202 return -1;
1205 /* first check if we already have an entry for this fd */
1206 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1207 if (thread->inflight[i].client == client)
1209 close( thread->inflight[i].server );
1210 thread->inflight[i].server = server;
1211 return i;
1214 /* now find a free spot to store it */
1215 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1216 if (thread->inflight[i].client == -1)
1218 thread->inflight[i].client = client;
1219 thread->inflight[i].server = server;
1220 return i;
1223 close( server );
1224 return -1;
1227 /* get an inflight fd and purge it from the list */
1228 /* the fd must be closed when no longer used */
1229 int thread_get_inflight_fd( struct thread *thread, int client )
1231 int i, ret;
1233 if (client == -1) return -1;
1237 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1239 if (thread->inflight[i].client == client)
1241 ret = thread->inflight[i].server;
1242 thread->inflight[i].server = thread->inflight[i].client = -1;
1243 return ret;
1246 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1247 return -1;
1250 /* kill a thread on the spot */
1251 void kill_thread( struct thread *thread, int violent_death )
1253 if (thread->state == TERMINATED) return; /* already killed */
1254 thread->state = TERMINATED;
1255 thread->exit_time = current_time;
1256 if (current == thread) current = NULL;
1257 if (debug_level)
1258 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1259 thread->id, thread->exit_code );
1260 if (thread->wait)
1262 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1263 send_thread_wakeup( thread, 0, thread->exit_code );
1264 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1265 violent_death = 0;
1267 kill_console_processes( thread, 0 );
1268 abandon_mutexes( thread );
1269 wake_up( &thread->obj, 0 );
1270 if (violent_death) send_thread_signal( thread, SIGQUIT );
1271 cleanup_thread( thread );
1272 remove_process_thread( thread->process, thread );
1273 release_object( thread );
1276 /* copy parts of a context structure */
1277 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1279 assert( to->cpu == from->cpu );
1280 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1281 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1282 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1283 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1284 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1285 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1286 if (flags & SERVER_CTX_YMM_REGISTERS) to->ymm = from->ymm;
1289 /* return the context flags that correspond to system regs */
1290 /* (system regs are the ones we can't access on the client side) */
1291 static unsigned int get_context_system_regs( enum cpu_type cpu )
1293 switch (cpu)
1295 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1296 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1297 case CPU_POWERPC: return 0;
1298 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1299 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1301 return 0;
1304 /* gets the current impersonation token */
1305 struct token *thread_get_impersonation_token( struct thread *thread )
1307 if (thread->token)
1308 return thread->token;
1309 else
1310 return thread->process->token;
1313 /* check if a cpu type can be supported on this server */
1314 int is_cpu_supported( enum cpu_type cpu )
1316 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1318 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1319 if (!(supported_cpus & prefix_cpu_mask))
1320 set_error( STATUS_NOT_SUPPORTED );
1321 else if (supported_cpus & CPU_FLAG(cpu))
1322 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1323 else
1324 set_error( STATUS_INVALID_IMAGE_FORMAT );
1325 return 0;
1328 /* return the cpu mask for supported cpus */
1329 unsigned int get_supported_cpu_mask(void)
1331 return supported_cpus & get_prefix_cpu_mask();
1334 /* create a new thread */
1335 DECL_HANDLER(new_thread)
1337 struct thread *thread;
1338 struct process *process;
1339 struct unicode_str name;
1340 const struct security_descriptor *sd;
1341 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1342 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1344 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1346 if (request_fd != -1) close( request_fd );
1347 return;
1350 if (process != current->process)
1352 if (request_fd != -1) /* can't create a request fd in a different process */
1354 close( request_fd );
1355 set_error( STATUS_INVALID_PARAMETER );
1356 goto done;
1358 if (process->running_threads) /* only the initial thread can be created in another process */
1360 set_error( STATUS_ACCESS_DENIED );
1361 goto done;
1364 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1366 if (request_fd != -1) close( request_fd );
1367 set_error( STATUS_INVALID_HANDLE );
1368 goto done;
1371 if ((thread = create_thread( request_fd, process, sd )))
1373 thread->system_regs = current->system_regs;
1374 if (req->suspend) thread->suspend++;
1375 reply->tid = get_thread_id( thread );
1376 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1377 req->access, objattr->attributes )))
1379 /* thread object will be released when the thread gets killed */
1380 goto done;
1382 kill_thread( thread, 1 );
1384 done:
1385 release_object( process );
1388 static int init_thread( struct thread *thread, int reply_fd, int wait_fd )
1390 if ((reply_fd = thread_get_inflight_fd( thread, reply_fd )) == -1)
1392 set_error( STATUS_TOO_MANY_OPENED_FILES );
1393 return 0;
1395 if ((wait_fd = thread_get_inflight_fd( thread, wait_fd )) == -1)
1397 set_error( STATUS_TOO_MANY_OPENED_FILES );
1398 goto error;
1401 if (thread->reply_fd) /* already initialised */
1403 set_error( STATUS_INVALID_PARAMETER );
1404 goto error;
1407 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1409 thread->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &thread->obj, 0 );
1410 thread->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &thread->obj, 0 );
1411 return thread->reply_fd && thread->wait_fd;
1413 error:
1414 if (reply_fd != -1) close( reply_fd );
1415 if (wait_fd != -1) close( wait_fd );
1416 return 0;
1419 /* initialize the first thread of a new process */
1420 DECL_HANDLER(init_first_thread)
1422 struct process *process = current->process;
1424 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1426 if (!is_valid_address(req->teb) || !is_valid_address(req->peb))
1428 set_error( STATUS_INVALID_PARAMETER );
1429 return;
1432 if (!is_cpu_supported( req->cpu )) return;
1434 current->unix_pid = process->unix_pid = req->unix_pid;
1435 current->unix_tid = req->unix_tid;
1436 current->teb = req->teb;
1437 process->peb = req->peb;
1438 process->ldt_copy = req->ldt_copy;
1439 process->cpu = req->cpu;
1441 if (!process->parent_id)
1442 process->affinity = current->affinity = get_thread_affinity( current );
1443 else
1444 set_thread_affinity( current, current->affinity );
1446 debug_level = max( debug_level, req->debug_level );
1448 reply->pid = get_process_id( process );
1449 reply->tid = get_thread_id( current );
1450 reply->info_size = get_process_startup_info_size( process );
1451 reply->server_start = server_start_time;
1452 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1455 /* initialize a new thread */
1456 DECL_HANDLER(init_thread)
1458 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1460 if (!is_valid_address(req->teb))
1462 set_error( STATUS_INVALID_PARAMETER );
1463 return;
1466 current->unix_pid = current->process->unix_pid;
1467 current->unix_tid = req->unix_tid;
1468 current->teb = req->teb;
1469 current->entry_point = req->entry;
1471 init_thread_context( current );
1472 generate_debug_event( current, DbgCreateThreadStateChange, &req->entry );
1473 set_thread_affinity( current, current->affinity );
1475 reply->pid = get_process_id( current->process );
1476 reply->tid = get_thread_id( current );
1477 reply->suspend = (current->suspend || current->process->suspend || current->context != NULL);
1480 /* terminate a thread */
1481 DECL_HANDLER(terminate_thread)
1483 struct thread *thread;
1485 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1487 thread->exit_code = req->exit_code;
1488 if (thread != current) kill_thread( thread, 1 );
1489 else reply->self = 1;
1490 release_object( thread );
1494 /* open a handle to a thread */
1495 DECL_HANDLER(open_thread)
1497 struct thread *thread = get_thread_from_id( req->tid );
1499 reply->handle = 0;
1500 if (thread)
1502 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1503 release_object( thread );
1507 /* fetch information about a thread */
1508 DECL_HANDLER(get_thread_info)
1510 struct thread *thread;
1511 unsigned int access = req->access & (THREAD_QUERY_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION);
1513 if (!access) access = THREAD_QUERY_LIMITED_INFORMATION;
1514 thread = get_thread_from_handle( req->handle, access );
1515 if (thread)
1517 reply->pid = get_process_id( thread->process );
1518 reply->tid = get_thread_id( thread );
1519 reply->teb = thread->teb;
1520 reply->entry_point = thread->entry_point;
1521 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1522 reply->priority = thread->priority;
1523 reply->affinity = thread->affinity;
1524 reply->last = thread->process->running_threads == 1;
1525 reply->suspend_count = thread->suspend;
1526 reply->dbg_hidden = thread->dbg_hidden;
1527 reply->desc_len = thread->desc_len;
1529 if (thread->desc && get_reply_max_size())
1531 if (thread->desc_len <= get_reply_max_size())
1532 set_reply_data( thread->desc, thread->desc_len );
1533 else
1534 set_error( STATUS_BUFFER_TOO_SMALL );
1537 release_object( thread );
1541 /* fetch information about thread times */
1542 DECL_HANDLER(get_thread_times)
1544 struct thread *thread;
1546 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION )))
1548 reply->creation_time = thread->creation_time;
1549 reply->exit_time = thread->exit_time;
1550 reply->unix_pid = thread->unix_pid;
1551 reply->unix_tid = thread->unix_tid;
1553 release_object( thread );
1557 /* set information about a thread */
1558 DECL_HANDLER(set_thread_info)
1560 struct thread *thread;
1562 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1564 set_thread_info( thread, req );
1565 release_object( thread );
1569 /* suspend a thread */
1570 DECL_HANDLER(suspend_thread)
1572 struct thread *thread;
1574 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1576 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1577 else reply->count = suspend_thread( thread );
1578 release_object( thread );
1582 /* resume a thread */
1583 DECL_HANDLER(resume_thread)
1585 struct thread *thread;
1587 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1589 reply->count = resume_thread( thread );
1590 release_object( thread );
1594 /* select on a handle list */
1595 DECL_HANDLER(select)
1597 select_op_t select_op;
1598 data_size_t op_size;
1599 struct thread_apc *apc;
1600 const apc_result_t *result = get_req_data();
1602 if (get_req_data_size() < sizeof(*result) ||
1603 get_req_data_size() - sizeof(*result) < req->size ||
1604 req->size & 3)
1606 set_error( STATUS_INVALID_PARAMETER );
1607 return;
1610 if (get_req_data_size() - sizeof(*result) - req->size == sizeof(context_t))
1612 const context_t *context = (const context_t *)((const char *)(result + 1) + req->size);
1613 if ((current->context && current->context->status != STATUS_PENDING) || context->cpu != current->process->cpu)
1615 set_error( STATUS_INVALID_PARAMETER );
1616 return;
1619 if (!current->context && !(current->context = create_thread_context( current ))) return;
1620 copy_context( &current->context->regs, context,
1621 context->flags & ~(current->context->regs.flags | get_context_system_regs(current->process->cpu)) );
1622 current->context->status = STATUS_SUCCESS;
1623 current->suspend_cookie = req->cookie;
1624 wake_up( &current->context->obj, 0 );
1627 if (!req->cookie)
1629 set_error( STATUS_INVALID_PARAMETER );
1630 return;
1633 op_size = min( req->size, sizeof(select_op) );
1634 memset( &select_op, 0, sizeof(select_op) );
1635 memcpy( &select_op, result + 1, op_size );
1637 /* first store results of previous apc */
1638 if (req->prev_apc)
1640 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1641 0, &thread_apc_ops ))) return;
1642 apc->result = *result;
1643 apc->executed = 1;
1644 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1646 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1647 apc->caller->process, 0, 0, DUPLICATE_SAME_ACCESS );
1648 close_handle( current->process, apc->result.create_thread.handle );
1649 apc->result.create_thread.handle = handle;
1650 clear_error(); /* ignore errors from the above calls */
1652 else if (apc->result.type == APC_ASYNC_IO)
1654 if (apc->owner)
1655 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1657 wake_up( &apc->obj, 0 );
1658 close_handle( current->process, req->prev_apc );
1659 release_object( apc );
1662 select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1664 while (get_error() == STATUS_USER_APC)
1666 if (!(apc = thread_dequeue_apc( current, 0 )))
1667 break;
1668 /* Optimization: ignore APC_NONE calls, they are only used to
1669 * wake up a thread, but since we got here the thread woke up already.
1671 if (apc->call.type != APC_NONE &&
1672 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1674 reply->call = apc->call;
1675 release_object( apc );
1676 break;
1678 apc->executed = 1;
1679 wake_up( &apc->obj, 0 );
1680 release_object( apc );
1683 if (get_error() == STATUS_KERNEL_APC)
1685 apc = thread_dequeue_apc( current, 1 );
1686 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1687 reply->call = apc->call;
1688 else
1690 apc->executed = 1;
1691 wake_up( &apc->obj, 0 );
1693 release_object( apc );
1695 else if (get_error() != STATUS_PENDING && get_reply_max_size() == sizeof(context_t) &&
1696 current->context && current->suspend_cookie == req->cookie)
1698 if (current->context->regs.flags)
1700 unsigned int system_flags = get_context_system_regs(current->process->cpu) &
1701 current->context->regs.flags;
1702 if (system_flags) set_thread_context( current, &current->context->regs, system_flags );
1703 set_reply_data( &current->context->regs, sizeof(context_t) );
1705 release_object( current->context );
1706 current->context = NULL;
1710 /* queue an APC for a thread or process */
1711 DECL_HANDLER(queue_apc)
1713 struct thread *thread = NULL;
1714 struct process *process = NULL;
1715 struct thread_apc *apc;
1717 if (!(apc = create_apc( NULL, &req->call ))) return;
1719 switch (apc->call.type)
1721 case APC_NONE:
1722 case APC_USER:
1723 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1724 break;
1725 case APC_VIRTUAL_ALLOC:
1726 case APC_VIRTUAL_FREE:
1727 case APC_VIRTUAL_PROTECT:
1728 case APC_VIRTUAL_FLUSH:
1729 case APC_VIRTUAL_LOCK:
1730 case APC_VIRTUAL_UNLOCK:
1731 case APC_UNMAP_VIEW:
1732 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1733 break;
1734 case APC_VIRTUAL_QUERY:
1735 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1736 break;
1737 case APC_MAP_VIEW:
1738 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1739 if (process && process != current->process)
1741 /* duplicate the handle into the target process */
1742 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1743 process, 0, 0, DUPLICATE_SAME_ACCESS );
1744 if (handle) apc->call.map_view.handle = handle;
1745 else
1747 release_object( process );
1748 process = NULL;
1751 break;
1752 case APC_CREATE_THREAD:
1753 case APC_BREAK_PROCESS:
1754 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1755 break;
1756 default:
1757 set_error( STATUS_INVALID_PARAMETER );
1758 break;
1761 if (thread)
1763 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1764 release_object( thread );
1766 else if (process)
1768 reply->self = (process == current->process);
1769 if (!reply->self)
1771 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1772 if (handle)
1774 if (queue_apc( process, NULL, apc ))
1776 apc->caller = (struct thread *)grab_object( current );
1777 reply->handle = handle;
1779 else
1781 close_handle( current->process, handle );
1782 set_error( STATUS_PROCESS_IS_TERMINATING );
1786 release_object( process );
1789 release_object( apc );
1792 /* Get the result of an APC call */
1793 DECL_HANDLER(get_apc_result)
1795 struct thread_apc *apc;
1797 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1798 0, &thread_apc_ops ))) return;
1800 if (apc->executed) reply->result = apc->result;
1801 else set_error( STATUS_PENDING );
1803 /* close the handle directly to avoid an extra round-trip */
1804 close_handle( current->process, req->handle );
1805 release_object( apc );
1808 /* retrieve the current context of a thread */
1809 DECL_HANDLER(get_thread_context)
1811 struct context *thread_context = NULL;
1812 unsigned int system_flags;
1813 struct thread *thread;
1814 context_t *context;
1816 if (get_reply_max_size() < sizeof(context_t))
1818 set_error( STATUS_INVALID_PARAMETER );
1819 return;
1822 if ((thread_context = (struct context *)get_handle_obj( current->process, req->handle, 0, &context_ops )))
1824 close_handle( current->process, req->handle ); /* avoid extra server call */
1825 system_flags = get_context_system_regs( thread_context->regs.cpu );
1827 else if ((thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT )))
1829 clear_error();
1830 system_flags = get_context_system_regs( thread->process->cpu );
1831 if (thread->state == RUNNING)
1833 reply->self = (thread == current);
1834 if (thread != current) stop_thread( thread );
1835 if (thread->context)
1837 /* make sure that system regs are valid in thread context */
1838 if (thread->unix_tid != -1 && (req->flags & system_flags & ~thread->context->regs.flags))
1839 get_thread_context( thread, &thread->context->regs, req->flags & system_flags );
1840 if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
1842 else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
1844 assert( reply->self );
1845 memset( context, 0, sizeof(context_t) );
1846 context->cpu = thread->process->cpu;
1847 if (req->flags & system_flags)
1849 get_thread_context( thread, context, req->flags & system_flags );
1850 context->flags |= req->flags & system_flags;
1854 else set_error( STATUS_UNSUCCESSFUL );
1855 release_object( thread );
1857 if (get_error() || !thread_context) return;
1859 set_error( thread_context->status );
1860 if (!thread_context->status && (context = set_reply_data_size( sizeof(context_t) )))
1862 memset( context, 0, sizeof(context_t) );
1863 context->cpu = thread_context->regs.cpu;
1864 copy_context( context, &thread_context->regs, req->flags );
1865 context->flags = req->flags;
1867 else if (thread_context->status == STATUS_PENDING)
1869 reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
1872 release_object( thread_context );
1875 /* set the current context of a thread */
1876 DECL_HANDLER(set_thread_context)
1878 struct thread *thread;
1879 const context_t *context = get_req_data();
1881 if (get_req_data_size() < sizeof(context_t))
1883 set_error( STATUS_INVALID_PARAMETER );
1884 return;
1886 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1887 reply->self = (thread == current);
1889 if (thread->state == TERMINATED) set_error( STATUS_UNSUCCESSFUL );
1890 else if (context->cpu != thread->process->cpu) set_error( STATUS_INVALID_PARAMETER );
1891 else
1893 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1895 if (thread != current) stop_thread( thread );
1896 else if (system_flags) set_thread_context( thread, context, system_flags );
1897 if (thread->context && !get_error())
1899 copy_context( &thread->context->regs, context, context->flags );
1900 thread->context->regs.flags |= context->flags;
1904 release_object( thread );
1907 /* fetch a selector entry for a thread */
1908 DECL_HANDLER(get_selector_entry)
1910 struct thread *thread;
1911 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1913 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1914 release_object( thread );