winegcc: Add entry symbol underscore when building linker command.
[wine.git] / server / thread.c
blobea545d5c30ef9e6a1b24928594fff854fb344794
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 dump_thread_apc, /* dump */
109 no_get_type, /* get_type */
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 no_map_access, /* map_access */
117 default_get_sd, /* get_sd */
118 default_set_sd, /* set_sd */
119 no_lookup_name, /* lookup_name */
120 no_link_name, /* link_name */
121 NULL, /* unlink_name */
122 no_open_file, /* open_file */
123 no_kernel_obj_list, /* get_kernel_obj_list */
124 no_close_handle, /* close_handle */
125 thread_apc_destroy /* destroy */
129 /* thread CPU context */
131 struct context
133 struct object obj; /* object header */
134 unsigned int status; /* status of the context */
135 context_t regs; /* context data */
138 static void dump_context( struct object *obj, int verbose );
139 static int context_signaled( struct object *obj, struct wait_queue_entry *entry );
141 static const struct object_ops context_ops =
143 sizeof(struct context), /* size */
144 dump_context, /* dump */
145 no_get_type, /* get_type */
146 add_queue, /* add_queue */
147 remove_queue, /* remove_queue */
148 context_signaled, /* signaled */
149 no_satisfied, /* satisfied */
150 no_signal, /* signal */
151 no_get_fd, /* get_fd */
152 no_map_access, /* map_access */
153 default_get_sd, /* get_sd */
154 default_set_sd, /* set_sd */
155 no_lookup_name, /* lookup_name */
156 no_link_name, /* link_name */
157 NULL, /* unlink_name */
158 no_open_file, /* open_file */
159 no_kernel_obj_list, /* get_kernel_obj_list */
160 no_close_handle, /* close_handle */
161 no_destroy /* destroy */
165 /* thread operations */
167 static void dump_thread( struct object *obj, int verbose );
168 static struct object_type *thread_get_type( struct object *obj );
169 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
170 static unsigned int thread_map_access( struct object *obj, unsigned int access );
171 static void thread_poll_event( struct fd *fd, int event );
172 static struct list *thread_get_kernel_obj_list( struct object *obj );
173 static void destroy_thread( struct object *obj );
175 static const struct object_ops thread_ops =
177 sizeof(struct thread), /* size */
178 dump_thread, /* dump */
179 thread_get_type, /* get_type */
180 add_queue, /* add_queue */
181 remove_queue, /* remove_queue */
182 thread_signaled, /* signaled */
183 no_satisfied, /* satisfied */
184 no_signal, /* signal */
185 no_get_fd, /* get_fd */
186 thread_map_access, /* map_access */
187 default_get_sd, /* get_sd */
188 default_set_sd, /* set_sd */
189 no_lookup_name, /* lookup_name */
190 no_link_name, /* link_name */
191 NULL, /* unlink_name */
192 no_open_file, /* open_file */
193 thread_get_kernel_obj_list, /* get_kernel_obj_list */
194 no_close_handle, /* close_handle */
195 destroy_thread /* destroy */
198 static const struct fd_ops thread_fd_ops =
200 NULL, /* get_poll_events */
201 thread_poll_event, /* poll_event */
202 NULL, /* flush */
203 NULL, /* get_fd_type */
204 NULL, /* ioctl */
205 NULL, /* queue_async */
206 NULL /* reselect_async */
209 static struct list thread_list = LIST_INIT(thread_list);
211 /* initialize the structure for a newly allocated thread */
212 static inline void init_thread_structure( struct thread *thread )
214 int i;
216 thread->unix_pid = -1; /* not known yet */
217 thread->unix_tid = -1; /* not known yet */
218 thread->context = NULL;
219 thread->teb = 0;
220 thread->entry_point = 0;
221 thread->debug_ctx = NULL;
222 thread->system_regs = 0;
223 thread->queue = NULL;
224 thread->wait = NULL;
225 thread->error = 0;
226 thread->req_data = NULL;
227 thread->req_toread = 0;
228 thread->reply_data = NULL;
229 thread->reply_towrite = 0;
230 thread->request_fd = NULL;
231 thread->reply_fd = NULL;
232 thread->wait_fd = NULL;
233 thread->state = RUNNING;
234 thread->exit_code = 0;
235 thread->priority = 0;
236 thread->suspend = 0;
237 thread->desktop_users = 0;
238 thread->token = NULL;
239 thread->desc = NULL;
240 thread->desc_len = 0;
242 thread->creation_time = current_time;
243 thread->exit_time = 0;
245 list_init( &thread->mutex_list );
246 list_init( &thread->system_apc );
247 list_init( &thread->user_apc );
248 list_init( &thread->kernel_object );
250 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
251 thread->inflight[i].server = thread->inflight[i].client = -1;
254 /* check if address looks valid for a client-side data structure (TEB etc.) */
255 static inline int is_valid_address( client_ptr_t addr )
257 return addr && !(addr % sizeof(int));
261 /* dump a context on stdout for debugging purposes */
262 static void dump_context( struct object *obj, int verbose )
264 struct context *context = (struct context *)obj;
265 assert( obj->ops == &context_ops );
267 fprintf( stderr, "context flags=%x\n", context->regs.flags );
271 static int context_signaled( struct object *obj, struct wait_queue_entry *entry )
273 struct context *context = (struct context *)obj;
274 return context->status != STATUS_PENDING;
278 static struct context *create_thread_context( struct thread *thread )
280 struct context *context;
281 if (!(context = alloc_object( &context_ops ))) return NULL;
282 context->status = STATUS_PENDING;
283 memset( &context->regs, 0, sizeof(context->regs) );
284 context->regs.cpu = thread->process->cpu;
285 return context;
289 /* create a new thread */
290 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
292 struct thread *thread;
293 int request_pipe[2];
295 if (fd == -1)
297 if (pipe( request_pipe ) == -1)
299 file_set_error();
300 return NULL;
302 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
304 close( request_pipe[0] );
305 close( request_pipe[1] );
306 return NULL;
308 close( request_pipe[1] );
309 fd = request_pipe[0];
312 if (process->is_terminating)
314 close( fd );
315 set_error( STATUS_PROCESS_IS_TERMINATING );
316 return NULL;
319 if (!(thread = alloc_object( &thread_ops )))
321 close( fd );
322 return NULL;
325 init_thread_structure( thread );
327 thread->process = (struct process *)grab_object( process );
328 thread->desktop = process->desktop;
329 thread->affinity = process->affinity;
330 if (!current) current = thread;
332 list_add_head( &thread_list, &thread->entry );
334 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
335 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
336 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
337 process->token ))
339 close( fd );
340 release_object( thread );
341 return NULL;
343 if (!(thread->id = alloc_ptid( thread )))
345 close( fd );
346 release_object( thread );
347 return NULL;
349 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
351 release_object( thread );
352 return NULL;
355 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
356 add_process_thread( thread->process, thread );
357 return thread;
360 /* handle a client event */
361 static void thread_poll_event( struct fd *fd, int event )
363 struct thread *thread = get_fd_user( fd );
364 assert( thread->obj.ops == &thread_ops );
366 grab_object( thread );
367 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
368 else if (event & POLLIN) read_request( thread );
369 else if (event & POLLOUT) write_reply( thread );
370 release_object( thread );
373 static struct list *thread_get_kernel_obj_list( struct object *obj )
375 struct thread *thread = (struct thread *)obj;
376 return &thread->kernel_object;
379 /* cleanup everything that is no longer needed by a dead thread */
380 /* used by destroy_thread and kill_thread */
381 static void cleanup_thread( struct thread *thread )
383 int i;
385 if (thread->context)
387 thread->context->status = STATUS_ACCESS_DENIED;
388 wake_up( &thread->context->obj, 0 );
389 release_object( thread->context );
390 thread->context = NULL;
392 clear_apc_queue( &thread->system_apc );
393 clear_apc_queue( &thread->user_apc );
394 free( thread->req_data );
395 free( thread->reply_data );
396 if (thread->request_fd) release_object( thread->request_fd );
397 if (thread->reply_fd) release_object( thread->reply_fd );
398 if (thread->wait_fd) release_object( thread->wait_fd );
399 cleanup_clipboard_thread(thread);
400 destroy_thread_windows( thread );
401 free_msg_queue( thread );
402 close_thread_desktop( thread );
403 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
405 if (thread->inflight[i].client != -1)
407 close( thread->inflight[i].server );
408 thread->inflight[i].client = thread->inflight[i].server = -1;
411 free( thread->desc );
412 thread->req_data = NULL;
413 thread->reply_data = NULL;
414 thread->request_fd = NULL;
415 thread->reply_fd = NULL;
416 thread->wait_fd = NULL;
417 thread->desktop = 0;
418 thread->desc = NULL;
419 thread->desc_len = 0;
422 /* destroy a thread when its refcount is 0 */
423 static void destroy_thread( struct object *obj )
425 struct thread *thread = (struct thread *)obj;
426 assert( obj->ops == &thread_ops );
428 assert( !thread->debug_ctx ); /* cannot still be debugging something */
429 list_remove( &thread->entry );
430 cleanup_thread( thread );
431 release_object( thread->process );
432 if (thread->id) free_ptid( thread->id );
433 if (thread->token) release_object( thread->token );
436 /* dump a thread on stdout for debugging purposes */
437 static void dump_thread( struct object *obj, int verbose )
439 struct thread *thread = (struct thread *)obj;
440 assert( obj->ops == &thread_ops );
442 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
443 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
446 static struct object_type *thread_get_type( struct object *obj )
448 static const WCHAR name[] = {'T','h','r','e','a','d'};
449 static const struct unicode_str str = { name, sizeof(name) };
450 return get_object_type( &str );
453 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
455 struct thread *mythread = (struct thread *)obj;
456 return (mythread->state == TERMINATED);
459 static unsigned int thread_map_access( struct object *obj, unsigned int access )
461 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
462 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
463 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
464 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
465 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
467 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
468 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
470 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
473 static void dump_thread_apc( struct object *obj, int verbose )
475 struct thread_apc *apc = (struct thread_apc *)obj;
476 assert( obj->ops == &thread_apc_ops );
478 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
481 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
483 struct thread_apc *apc = (struct thread_apc *)obj;
484 return apc->executed;
487 static void thread_apc_destroy( struct object *obj )
489 struct thread_apc *apc = (struct thread_apc *)obj;
490 if (apc->caller) release_object( apc->caller );
491 if (apc->owner) release_object( apc->owner );
494 /* queue an async procedure call */
495 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
497 struct thread_apc *apc;
499 if ((apc = alloc_object( &thread_apc_ops )))
501 apc->call = *call_data;
502 apc->caller = NULL;
503 apc->owner = owner;
504 apc->executed = 0;
505 apc->result.type = APC_NONE;
506 if (owner) grab_object( owner );
508 return apc;
511 /* get a thread pointer from a thread id (and increment the refcount) */
512 struct thread *get_thread_from_id( thread_id_t id )
514 struct object *obj = get_ptid_entry( id );
516 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
517 set_error( STATUS_INVALID_CID );
518 return NULL;
521 /* get a thread from a handle (and increment the refcount) */
522 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
524 return (struct thread *)get_handle_obj( current->process, handle,
525 access, &thread_ops );
528 /* find a thread from a Unix tid */
529 struct thread *get_thread_from_tid( int tid )
531 struct thread *thread;
533 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
535 if (thread->unix_tid == tid) return thread;
537 return NULL;
540 /* find a thread from a Unix pid */
541 struct thread *get_thread_from_pid( int pid )
543 struct thread *thread;
545 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
547 if (thread->unix_pid == pid) return thread;
549 return NULL;
552 int set_thread_affinity( struct thread *thread, affinity_t affinity )
554 int ret = 0;
555 #ifdef HAVE_SCHED_SETAFFINITY
556 if (thread->unix_tid != -1)
558 cpu_set_t set;
559 int i;
560 affinity_t mask;
562 CPU_ZERO( &set );
563 for (i = 0, mask = 1; mask; i++, mask <<= 1)
564 if (affinity & mask) CPU_SET( i, &set );
566 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
568 #endif
569 if (!ret) thread->affinity = affinity;
570 return ret;
573 affinity_t get_thread_affinity( struct thread *thread )
575 affinity_t mask = 0;
576 #ifdef HAVE_SCHED_SETAFFINITY
577 if (thread->unix_tid != -1)
579 cpu_set_t set;
580 unsigned int i;
582 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
583 for (i = 0; i < 8 * sizeof(mask); i++)
584 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
586 #endif
587 if (!mask) mask = ~(affinity_t)0;
588 return mask;
591 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
592 #define THREAD_PRIORITY_REALTIME_LOWEST -7
594 /* set all information about a thread */
595 static void set_thread_info( struct thread *thread,
596 const struct set_thread_info_request *req )
598 if (req->mask & SET_THREAD_INFO_PRIORITY)
600 int max = THREAD_PRIORITY_HIGHEST;
601 int min = THREAD_PRIORITY_LOWEST;
602 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
604 max = THREAD_PRIORITY_REALTIME_HIGHEST;
605 min = THREAD_PRIORITY_REALTIME_LOWEST;
607 if ((req->priority >= min && req->priority <= max) ||
608 req->priority == THREAD_PRIORITY_IDLE ||
609 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
610 thread->priority = req->priority;
611 else
612 set_error( STATUS_INVALID_PARAMETER );
614 if (req->mask & SET_THREAD_INFO_AFFINITY)
616 if ((req->affinity & thread->process->affinity) != req->affinity)
617 set_error( STATUS_INVALID_PARAMETER );
618 else if (thread->state == TERMINATED)
619 set_error( STATUS_THREAD_IS_TERMINATING );
620 else if (set_thread_affinity( thread, req->affinity ))
621 file_set_error();
623 if (req->mask & SET_THREAD_INFO_TOKEN)
624 security_set_thread_token( thread, req->token );
625 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
626 thread->entry_point = req->entry_point;
627 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
629 WCHAR *desc;
630 data_size_t desc_len = get_req_data_size();
632 if (desc_len)
634 if ((desc = mem_alloc( desc_len )))
636 memcpy( desc, get_req_data(), desc_len );
637 free( thread->desc );
638 thread->desc = desc;
639 thread->desc_len = desc_len;
642 else
644 free( thread->desc );
645 thread->desc = NULL;
646 thread->desc_len = 0;
651 /* stop a thread (at the Unix level) */
652 void stop_thread( struct thread *thread )
654 if (thread->context) return; /* already suspended, no need for a signal */
655 if (!(thread->context = create_thread_context( thread ))) return;
656 /* can't stop a thread while initialisation is in progress */
657 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
660 /* suspend a thread */
661 int suspend_thread( struct thread *thread )
663 int old_count = thread->suspend;
664 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
666 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
668 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
669 return old_count;
672 /* resume a thread */
673 int resume_thread( struct thread *thread )
675 int old_count = thread->suspend;
676 if (thread->suspend > 0)
678 if (!(--thread->suspend)) resume_delayed_debug_events( thread );
679 if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
681 return old_count;
684 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
685 int add_queue( struct object *obj, struct wait_queue_entry *entry )
687 grab_object( obj );
688 entry->obj = obj;
689 list_add_tail( &obj->wait_queue, &entry->entry );
690 return 1;
693 /* remove a thread from an object wait queue */
694 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
696 list_remove( &entry->entry );
697 release_object( obj );
700 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
702 return entry->wait->thread;
705 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
707 return entry->wait->select;
710 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
712 return entry->wait->key;
715 void make_wait_abandoned( struct wait_queue_entry *entry )
717 entry->wait->abandoned = 1;
720 /* finish waiting */
721 static unsigned int end_wait( struct thread *thread, unsigned int status )
723 struct thread_wait *wait = thread->wait;
724 struct wait_queue_entry *entry;
725 int i;
727 assert( wait );
728 thread->wait = wait->next;
730 if (status < wait->count) /* wait satisfied, tell it to the objects */
732 if (wait->select == SELECT_WAIT_ALL)
734 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
735 entry->obj->ops->satisfied( entry->obj, entry );
737 else
739 entry = wait->queues + status;
740 entry->obj->ops->satisfied( entry->obj, entry );
742 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
744 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
745 entry->obj->ops->remove_queue( entry->obj, entry );
746 if (wait->user) remove_timeout_user( wait->user );
747 free( wait );
748 return status;
751 /* build the thread wait structure */
752 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
753 int flags, abstime_t when )
755 struct thread_wait *wait;
756 struct wait_queue_entry *entry;
757 unsigned int i;
759 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
760 wait->next = current->wait;
761 wait->thread = current;
762 wait->count = count;
763 wait->flags = flags;
764 wait->select = select_op->op;
765 wait->cookie = 0;
766 wait->user = NULL;
767 wait->when = when;
768 wait->abandoned = 0;
769 current->wait = wait;
771 for (i = 0, entry = wait->queues; i < count; i++, entry++)
773 struct object *obj = objects[i];
774 entry->wait = wait;
775 if (!obj->ops->add_queue( obj, entry ))
777 wait->count = i;
778 end_wait( current, get_error() );
779 return 0;
782 return 1;
785 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
786 int flags, abstime_t when )
788 struct object *objects[MAXIMUM_WAIT_OBJECTS];
789 unsigned int i;
790 int ret = 0;
792 assert( count <= MAXIMUM_WAIT_OBJECTS );
794 for (i = 0; i < count; i++)
795 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
796 break;
798 if (i == count) ret = wait_on( select_op, count, objects, flags, when );
800 while (i > 0) release_object( objects[--i] );
801 return ret;
804 /* check if the thread waiting condition is satisfied */
805 static int check_wait( struct thread *thread )
807 int i;
808 struct thread_wait *wait = thread->wait;
809 struct wait_queue_entry *entry;
811 assert( wait );
813 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
814 return STATUS_KERNEL_APC;
816 /* Suspended threads may not acquire locks, but they can run system APCs */
817 if (thread->process->suspend + thread->suspend > 0) return -1;
819 if (wait->select == SELECT_WAIT_ALL)
821 int not_ok = 0;
822 /* Note: we must check them all anyway, as some objects may
823 * want to do something when signaled, even if others are not */
824 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
825 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
826 if (!not_ok) return STATUS_WAIT_0;
828 else
830 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
831 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
834 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
835 if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
836 if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
837 return -1;
840 /* send the wakeup signal to a thread */
841 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
843 struct wake_up_reply reply;
844 int ret;
846 /* check if we're waking current suspend wait */
847 if (thread->context && thread->suspend_cookie == cookie
848 && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
850 if (!thread->context->regs.flags)
852 release_object( thread->context );
853 thread->context = NULL;
855 else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
858 memset( &reply, 0, sizeof(reply) );
859 reply.cookie = cookie;
860 reply.signaled = signaled;
861 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
862 return 0;
863 if (ret >= 0)
864 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
865 else if (errno == EPIPE)
866 kill_thread( thread, 0 ); /* normal death */
867 else
868 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
869 return -1;
872 /* attempt to wake up a thread */
873 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
874 int wake_thread( struct thread *thread )
876 int signaled, count;
877 client_ptr_t cookie;
879 for (count = 0; thread->wait; count++)
881 if ((signaled = check_wait( thread )) == -1) break;
883 cookie = thread->wait->cookie;
884 signaled = end_wait( thread, signaled );
885 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
886 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
888 if (!count) count = -1;
889 break;
892 return count;
895 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
896 int wake_thread_queue_entry( struct wait_queue_entry *entry )
898 struct thread_wait *wait = entry->wait;
899 struct thread *thread = wait->thread;
900 int signaled;
901 client_ptr_t cookie;
903 if (thread->wait != wait) return 0; /* not the current wait */
904 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
906 assert( wait->select != SELECT_WAIT_ALL );
908 cookie = wait->cookie;
909 signaled = end_wait( thread, entry - wait->queues );
910 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
912 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
913 wake_thread( thread ); /* check other waits too */
915 return 1;
918 /* thread wait timeout */
919 static void thread_timeout( void *ptr )
921 struct thread_wait *wait = ptr;
922 struct thread *thread = wait->thread;
923 client_ptr_t cookie = wait->cookie;
925 wait->user = NULL;
926 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
927 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
929 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
930 end_wait( thread, STATUS_TIMEOUT );
932 assert( cookie );
933 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
934 /* check if other objects have become signaled in the meantime */
935 wake_thread( thread );
938 /* try signaling an event flag, a semaphore or a mutex */
939 static int signal_object( obj_handle_t handle )
941 struct object *obj;
942 int ret = 0;
944 obj = get_handle_obj( current->process, handle, 0, NULL );
945 if (obj)
947 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
948 release_object( obj );
950 return ret;
953 /* select on a list of handles */
954 static void select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
955 int flags, abstime_t when )
957 int ret;
958 unsigned int count;
959 struct object *object;
961 switch (select_op->op)
963 case SELECT_NONE:
964 if (!wait_on( select_op, 0, NULL, flags, when )) return;
965 break;
967 case SELECT_WAIT:
968 case SELECT_WAIT_ALL:
969 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
970 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
972 set_error( STATUS_INVALID_PARAMETER );
973 return;
975 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
976 return;
977 break;
979 case SELECT_SIGNAL_AND_WAIT:
980 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
981 return;
982 if (select_op->signal_and_wait.signal)
984 if (!signal_object( select_op->signal_and_wait.signal ))
986 end_wait( current, get_error() );
987 return;
989 /* check if we woke ourselves up */
990 if (!current->wait) return;
992 break;
994 case SELECT_KEYED_EVENT_WAIT:
995 case SELECT_KEYED_EVENT_RELEASE:
996 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
997 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
998 if (!object) return;
999 ret = wait_on( select_op, 1, &object, flags, when );
1000 release_object( object );
1001 if (!ret) return;
1002 current->wait->key = select_op->keyed_event.key;
1003 break;
1005 default:
1006 set_error( STATUS_INVALID_PARAMETER );
1007 return;
1010 if ((ret = check_wait( current )) != -1)
1012 /* condition is already satisfied */
1013 set_error( end_wait( current, ret ));
1014 return;
1017 /* now we need to wait */
1018 if (current->wait->when != TIMEOUT_INFINITE)
1020 if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
1021 thread_timeout, current->wait )))
1023 end_wait( current, get_error() );
1024 return;
1027 current->wait->cookie = cookie;
1028 set_error( STATUS_PENDING );
1029 return;
1032 /* attempt to wake threads sleeping on the object wait queue */
1033 void wake_up( struct object *obj, int max )
1035 struct list *ptr;
1036 int ret;
1038 LIST_FOR_EACH( ptr, &obj->wait_queue )
1040 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
1041 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
1042 if (ret > 0 && max && !--max) break;
1043 /* restart at the head of the list since a wake up can change the object wait queue */
1044 ptr = &obj->wait_queue;
1048 /* return the apc queue to use for a given apc type */
1049 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
1051 switch(type)
1053 case APC_NONE:
1054 case APC_USER:
1055 case APC_TIMER:
1056 return &thread->user_apc;
1057 default:
1058 return &thread->system_apc;
1062 /* check if thread is currently waiting for a (system) apc */
1063 static inline int is_in_apc_wait( struct thread *thread )
1065 return (thread->process->suspend || thread->suspend ||
1066 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
1069 /* queue an existing APC to a given thread */
1070 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
1072 struct list *queue;
1074 if (thread && thread->state == TERMINATED && process)
1075 thread = NULL;
1077 if (!thread) /* find a suitable thread inside the process */
1079 struct thread *candidate;
1081 /* first try to find a waiting thread */
1082 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1084 if (candidate->state == TERMINATED) continue;
1085 if (is_in_apc_wait( candidate ))
1087 thread = candidate;
1088 break;
1091 if (!thread)
1093 /* then use the first one that accepts a signal */
1094 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1096 if (send_thread_signal( candidate, SIGUSR1 ))
1098 thread = candidate;
1099 break;
1103 if (!thread) return 0; /* nothing found */
1104 queue = get_apc_queue( thread, apc->call.type );
1106 else
1108 if (thread->state == TERMINATED) return 0;
1109 queue = get_apc_queue( thread, apc->call.type );
1110 /* send signal for system APCs if needed */
1111 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1113 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1115 /* cancel a possible previous APC with the same owner */
1116 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1119 grab_object( apc );
1120 list_add_tail( queue, &apc->entry );
1121 if (!list_prev( queue, &apc->entry )) /* first one */
1122 wake_thread( thread );
1124 return 1;
1127 /* queue an async procedure call */
1128 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1130 struct thread_apc *apc;
1131 int ret = 0;
1133 if ((apc = create_apc( owner, call_data )))
1135 ret = queue_apc( process, thread, apc );
1136 release_object( apc );
1138 return ret;
1141 /* cancel the async procedure call owned by a specific object */
1142 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1144 struct thread_apc *apc;
1145 struct list *queue = get_apc_queue( thread, type );
1147 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1149 if (apc->owner != owner) continue;
1150 list_remove( &apc->entry );
1151 apc->executed = 1;
1152 wake_up( &apc->obj, 0 );
1153 release_object( apc );
1154 return;
1158 /* remove the head apc from the queue; the returned object must be released by the caller */
1159 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
1161 struct thread_apc *apc = NULL;
1162 struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
1164 if (ptr)
1166 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1167 list_remove( ptr );
1169 return apc;
1172 /* clear an APC queue, cancelling all the APCs on it */
1173 static void clear_apc_queue( struct list *queue )
1175 struct list *ptr;
1177 while ((ptr = list_head( queue )))
1179 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1180 list_remove( &apc->entry );
1181 apc->executed = 1;
1182 wake_up( &apc->obj, 0 );
1183 release_object( apc );
1187 /* add an fd to the inflight list */
1188 /* return list index, or -1 on error */
1189 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1191 int i;
1193 if (server == -1) return -1;
1194 if (client == -1)
1196 close( server );
1197 return -1;
1200 /* first check if we already have an entry for this fd */
1201 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1202 if (thread->inflight[i].client == client)
1204 close( thread->inflight[i].server );
1205 thread->inflight[i].server = server;
1206 return i;
1209 /* now find a free spot to store it */
1210 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1211 if (thread->inflight[i].client == -1)
1213 thread->inflight[i].client = client;
1214 thread->inflight[i].server = server;
1215 return i;
1218 close( server );
1219 return -1;
1222 /* get an inflight fd and purge it from the list */
1223 /* the fd must be closed when no longer used */
1224 int thread_get_inflight_fd( struct thread *thread, int client )
1226 int i, ret;
1228 if (client == -1) return -1;
1232 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1234 if (thread->inflight[i].client == client)
1236 ret = thread->inflight[i].server;
1237 thread->inflight[i].server = thread->inflight[i].client = -1;
1238 return ret;
1241 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1242 return -1;
1245 /* kill a thread on the spot */
1246 void kill_thread( struct thread *thread, int violent_death )
1248 if (thread->state == TERMINATED) return; /* already killed */
1249 thread->state = TERMINATED;
1250 thread->exit_time = current_time;
1251 if (current == thread) current = NULL;
1252 if (debug_level)
1253 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1254 thread->id, thread->exit_code );
1255 if (thread->wait)
1257 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1258 send_thread_wakeup( thread, 0, thread->exit_code );
1259 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1260 violent_death = 0;
1262 kill_console_processes( thread, 0 );
1263 debug_exit_thread( thread );
1264 abandon_mutexes( thread );
1265 wake_up( &thread->obj, 0 );
1266 if (violent_death) send_thread_signal( thread, SIGQUIT );
1267 cleanup_thread( thread );
1268 remove_process_thread( thread->process, thread );
1269 release_object( thread );
1272 /* copy parts of a context structure */
1273 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1275 assert( to->cpu == from->cpu );
1276 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1277 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1278 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1279 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1280 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1281 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1284 /* return the context flags that correspond to system regs */
1285 /* (system regs are the ones we can't access on the client side) */
1286 static unsigned int get_context_system_regs( enum cpu_type cpu )
1288 switch (cpu)
1290 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1291 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1292 case CPU_POWERPC: return 0;
1293 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1294 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1296 return 0;
1299 /* take a snapshot of currently running threads */
1300 struct thread_snapshot *thread_snap( int *count )
1302 struct thread_snapshot *snapshot, *ptr;
1303 struct thread *thread;
1304 int total = 0;
1306 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1307 if (thread->state != TERMINATED) total++;
1308 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1309 ptr = snapshot;
1310 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1312 if (thread->state == TERMINATED) continue;
1313 ptr->thread = thread;
1314 ptr->count = thread->obj.refcount;
1315 ptr->priority = thread->priority;
1316 grab_object( thread );
1317 ptr++;
1319 *count = total;
1320 return snapshot;
1323 /* gets the current impersonation token */
1324 struct token *thread_get_impersonation_token( struct thread *thread )
1326 if (thread->token)
1327 return thread->token;
1328 else
1329 return thread->process->token;
1332 /* check if a cpu type can be supported on this server */
1333 int is_cpu_supported( enum cpu_type cpu )
1335 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1337 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1338 if (!(supported_cpus & prefix_cpu_mask))
1339 set_error( STATUS_NOT_SUPPORTED );
1340 else if (supported_cpus & CPU_FLAG(cpu))
1341 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1342 else
1343 set_error( STATUS_INVALID_IMAGE_FORMAT );
1344 return 0;
1347 /* return the cpu mask for supported cpus */
1348 unsigned int get_supported_cpu_mask(void)
1350 return supported_cpus & get_prefix_cpu_mask();
1353 /* create a new thread */
1354 DECL_HANDLER(new_thread)
1356 struct thread *thread;
1357 struct process *process;
1358 struct unicode_str name;
1359 const struct security_descriptor *sd;
1360 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1361 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1363 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1365 if (request_fd != -1) close( request_fd );
1366 return;
1369 if (process != current->process)
1371 if (request_fd != -1) /* can't create a request fd in a different process */
1373 close( request_fd );
1374 set_error( STATUS_INVALID_PARAMETER );
1375 goto done;
1377 if (process->running_threads) /* only the initial thread can be created in another process */
1379 set_error( STATUS_ACCESS_DENIED );
1380 goto done;
1383 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1385 if (request_fd != -1) close( request_fd );
1386 set_error( STATUS_INVALID_HANDLE );
1387 goto done;
1390 if ((thread = create_thread( request_fd, process, sd )))
1392 thread->system_regs = current->system_regs;
1393 if (req->suspend) thread->suspend++;
1394 reply->tid = get_thread_id( thread );
1395 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1396 req->access, objattr->attributes )))
1398 /* thread object will be released when the thread gets killed */
1399 goto done;
1401 kill_thread( thread, 1 );
1403 done:
1404 release_object( process );
1407 /* initialize a new thread */
1408 DECL_HANDLER(init_thread)
1410 struct process *process = current->process;
1411 int wait_fd, reply_fd;
1413 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1415 set_error( STATUS_TOO_MANY_OPENED_FILES );
1416 return;
1418 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1420 set_error( STATUS_TOO_MANY_OPENED_FILES );
1421 goto error;
1424 if (current->reply_fd) /* already initialised */
1426 set_error( STATUS_INVALID_PARAMETER );
1427 goto error;
1430 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1432 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1433 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1434 if (!current->reply_fd || !current->wait_fd) return;
1436 if (!is_valid_address(req->teb))
1438 set_error( STATUS_INVALID_PARAMETER );
1439 return;
1442 current->unix_pid = req->unix_pid;
1443 current->unix_tid = req->unix_tid;
1444 current->teb = req->teb;
1445 current->entry_point = process->peb ? req->entry : 0;
1447 if (!process->peb) /* first thread, initialize the process too */
1449 if (!is_cpu_supported( req->cpu )) return;
1450 process->unix_pid = current->unix_pid;
1451 process->peb = req->entry;
1452 process->cpu = req->cpu;
1453 reply->info_size = init_process( current );
1454 if (!process->parent_id)
1455 process->affinity = current->affinity = get_thread_affinity( current );
1456 else
1457 set_thread_affinity( current, current->affinity );
1459 else
1461 if (req->cpu != process->cpu)
1463 set_error( STATUS_INVALID_PARAMETER );
1464 return;
1466 if (process->unix_pid != current->unix_pid)
1467 process->unix_pid = -1; /* can happen with linuxthreads */
1468 init_thread_context( current );
1469 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1470 set_thread_affinity( current, current->affinity );
1472 debug_level = max( debug_level, req->debug_level );
1474 reply->pid = get_process_id( process );
1475 reply->tid = get_thread_id( current );
1476 reply->version = SERVER_PROTOCOL_VERSION;
1477 reply->server_start = server_start_time;
1478 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1479 reply->suspend = (current->suspend || process->suspend || current->context != NULL);
1480 return;
1482 error:
1483 if (reply_fd != -1) close( reply_fd );
1484 if (wait_fd != -1) close( wait_fd );
1487 /* terminate a thread */
1488 DECL_HANDLER(terminate_thread)
1490 struct thread *thread;
1492 reply->self = 0;
1493 reply->last = 0;
1494 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1496 thread->exit_code = req->exit_code;
1497 if (thread != current) kill_thread( thread, 1 );
1498 else
1500 reply->self = 1;
1501 reply->last = (thread->process->running_threads == 1);
1503 release_object( thread );
1507 /* open a handle to a thread */
1508 DECL_HANDLER(open_thread)
1510 struct thread *thread = get_thread_from_id( req->tid );
1512 reply->handle = 0;
1513 if (thread)
1515 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1516 release_object( thread );
1520 /* fetch information about a thread */
1521 DECL_HANDLER(get_thread_info)
1523 struct thread *thread;
1524 obj_handle_t handle = req->handle;
1526 if (!handle) thread = get_thread_from_id( req->tid_in );
1527 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1529 if (thread)
1531 reply->pid = get_process_id( thread->process );
1532 reply->tid = get_thread_id( thread );
1533 reply->teb = thread->teb;
1534 reply->entry_point = thread->entry_point;
1535 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1536 reply->priority = thread->priority;
1537 reply->affinity = thread->affinity;
1538 reply->last = thread->process->running_threads == 1;
1539 reply->suspend_count = thread->suspend;
1540 reply->desc_len = thread->desc_len;
1542 if (thread->desc && get_reply_max_size())
1544 if (thread->desc_len <= get_reply_max_size())
1545 set_reply_data( thread->desc, thread->desc_len );
1546 else
1547 set_error( STATUS_BUFFER_TOO_SMALL );
1550 release_object( thread );
1554 /* fetch information about thread times */
1555 DECL_HANDLER(get_thread_times)
1557 struct thread *thread;
1559 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1561 reply->creation_time = thread->creation_time;
1562 reply->exit_time = thread->exit_time;
1564 release_object( thread );
1568 /* set information about a thread */
1569 DECL_HANDLER(set_thread_info)
1571 struct thread *thread;
1573 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1575 set_thread_info( thread, req );
1576 release_object( thread );
1580 /* suspend a thread */
1581 DECL_HANDLER(suspend_thread)
1583 struct thread *thread;
1585 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1587 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1588 else reply->count = suspend_thread( thread );
1589 release_object( thread );
1593 /* resume a thread */
1594 DECL_HANDLER(resume_thread)
1596 struct thread *thread;
1598 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1600 reply->count = resume_thread( thread );
1601 release_object( thread );
1605 /* select on a handle list */
1606 DECL_HANDLER(select)
1608 select_op_t select_op;
1609 data_size_t op_size;
1610 struct thread_apc *apc;
1611 const apc_result_t *result = get_req_data();
1613 if (get_req_data_size() < sizeof(*result) ||
1614 get_req_data_size() - sizeof(*result) < req->size ||
1615 req->size & 3)
1617 set_error( STATUS_INVALID_PARAMETER );
1618 return;
1621 if (get_req_data_size() - sizeof(*result) - req->size == sizeof(context_t))
1623 const context_t *context = (const context_t *)((const char *)(result + 1) + req->size);
1624 if ((current->context && current->context->status != STATUS_PENDING) || context->cpu != current->process->cpu)
1626 set_error( STATUS_INVALID_PARAMETER );
1627 return;
1630 if (!current->context && !(current->context = create_thread_context( current ))) return;
1631 copy_context( &current->context->regs, context,
1632 context->flags & ~(current->context->regs.flags | get_context_system_regs(current->process->cpu)) );
1633 current->context->status = STATUS_SUCCESS;
1634 current->suspend_cookie = req->cookie;
1635 wake_up( &current->context->obj, 0 );
1638 if (!req->cookie)
1640 set_error( STATUS_INVALID_PARAMETER );
1641 return;
1644 op_size = min( req->size, sizeof(select_op) );
1645 memset( &select_op, 0, sizeof(select_op) );
1646 memcpy( &select_op, result + 1, op_size );
1648 /* first store results of previous apc */
1649 if (req->prev_apc)
1651 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1652 0, &thread_apc_ops ))) return;
1653 apc->result = *result;
1654 apc->executed = 1;
1655 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1657 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1658 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1659 close_handle( current->process, apc->result.create_thread.handle );
1660 apc->result.create_thread.handle = handle;
1661 clear_error(); /* ignore errors from the above calls */
1663 else if (apc->result.type == APC_ASYNC_IO)
1665 if (apc->owner)
1666 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1668 wake_up( &apc->obj, 0 );
1669 close_handle( current->process, req->prev_apc );
1670 release_object( apc );
1673 select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1675 while (get_error() == STATUS_USER_APC)
1677 if (!(apc = thread_dequeue_apc( current, 0 )))
1678 break;
1679 /* Optimization: ignore APC_NONE calls, they are only used to
1680 * wake up a thread, but since we got here the thread woke up already.
1682 if (apc->call.type != APC_NONE &&
1683 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1685 reply->call = apc->call;
1686 release_object( apc );
1687 break;
1689 apc->executed = 1;
1690 wake_up( &apc->obj, 0 );
1691 release_object( apc );
1694 if (get_error() == STATUS_KERNEL_APC)
1696 apc = thread_dequeue_apc( current, 1 );
1697 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1698 reply->call = apc->call;
1699 else
1701 apc->executed = 1;
1702 wake_up( &apc->obj, 0 );
1704 release_object( apc );
1706 else if (get_error() != STATUS_PENDING && get_reply_max_size() == sizeof(context_t) &&
1707 current->context && current->suspend_cookie == req->cookie)
1709 if (current->context->regs.flags)
1711 unsigned int system_flags = get_context_system_regs(current->process->cpu) &
1712 current->context->regs.flags;
1713 if (system_flags) set_thread_context( current, &current->context->regs, system_flags );
1714 set_reply_data( &current->context->regs, sizeof(context_t) );
1716 release_object( current->context );
1717 current->context = NULL;
1721 /* queue an APC for a thread or process */
1722 DECL_HANDLER(queue_apc)
1724 struct thread *thread = NULL;
1725 struct process *process = NULL;
1726 struct thread_apc *apc;
1728 if (!(apc = create_apc( NULL, &req->call ))) return;
1730 switch (apc->call.type)
1732 case APC_NONE:
1733 case APC_USER:
1734 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1735 break;
1736 case APC_VIRTUAL_ALLOC:
1737 case APC_VIRTUAL_FREE:
1738 case APC_VIRTUAL_PROTECT:
1739 case APC_VIRTUAL_FLUSH:
1740 case APC_VIRTUAL_LOCK:
1741 case APC_VIRTUAL_UNLOCK:
1742 case APC_UNMAP_VIEW:
1743 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1744 break;
1745 case APC_VIRTUAL_QUERY:
1746 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1747 break;
1748 case APC_MAP_VIEW:
1749 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1750 if (process && process != current->process)
1752 /* duplicate the handle into the target process */
1753 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1754 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1755 if (handle) apc->call.map_view.handle = handle;
1756 else
1758 release_object( process );
1759 process = NULL;
1762 break;
1763 case APC_CREATE_THREAD:
1764 case APC_BREAK_PROCESS:
1765 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1766 break;
1767 default:
1768 set_error( STATUS_INVALID_PARAMETER );
1769 break;
1772 if (thread)
1774 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1775 release_object( thread );
1777 else if (process)
1779 reply->self = (process == current->process);
1780 if (!reply->self)
1782 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1783 if (handle)
1785 if (queue_apc( process, NULL, apc ))
1787 apc->caller = (struct thread *)grab_object( current );
1788 reply->handle = handle;
1790 else
1792 close_handle( current->process, handle );
1793 set_error( STATUS_PROCESS_IS_TERMINATING );
1797 release_object( process );
1800 release_object( apc );
1803 /* Get the result of an APC call */
1804 DECL_HANDLER(get_apc_result)
1806 struct thread_apc *apc;
1808 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1809 0, &thread_apc_ops ))) return;
1811 if (apc->executed) reply->result = apc->result;
1812 else set_error( STATUS_PENDING );
1814 /* close the handle directly to avoid an extra round-trip */
1815 close_handle( current->process, req->handle );
1816 release_object( apc );
1819 /* retrieve the current context of a thread */
1820 DECL_HANDLER(get_thread_context)
1822 struct context *thread_context = NULL;
1823 unsigned int system_flags;
1824 struct thread *thread;
1825 context_t *context;
1827 if (get_reply_max_size() < sizeof(context_t))
1829 set_error( STATUS_INVALID_PARAMETER );
1830 return;
1833 if ((thread_context = (struct context *)get_handle_obj( current->process, req->handle, 0, &context_ops )))
1835 close_handle( current->process, req->handle ); /* avoid extra server call */
1836 system_flags = get_context_system_regs( thread_context->regs.cpu );
1838 else if ((thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT )))
1840 clear_error();
1841 system_flags = get_context_system_regs( thread->process->cpu );
1842 if (thread->state == RUNNING)
1844 reply->self = (thread == current);
1845 if (thread != current) stop_thread( thread );
1846 if (thread->context)
1848 /* make sure that system regs are valid in thread context */
1849 if (thread->unix_tid != -1 && (req->flags & system_flags & ~thread->context->regs.flags))
1850 get_thread_context( thread, &thread->context->regs, req->flags & system_flags );
1851 if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
1853 else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
1855 assert( reply->self );
1856 memset( context, 0, sizeof(context_t) );
1857 context->cpu = thread->process->cpu;
1858 if (req->flags & system_flags)
1860 get_thread_context( thread, context, req->flags & system_flags );
1861 context->flags |= req->flags & system_flags;
1865 else set_error( STATUS_UNSUCCESSFUL );
1866 release_object( thread );
1868 if (get_error() || !thread_context) return;
1870 set_error( thread_context->status );
1871 if (!thread_context->status && (context = set_reply_data_size( sizeof(context_t) )))
1873 memset( context, 0, sizeof(context_t) );
1874 context->cpu = thread_context->regs.cpu;
1875 copy_context( context, &thread_context->regs, req->flags );
1876 context->flags = req->flags;
1878 else if (thread_context->status == STATUS_PENDING)
1880 reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
1883 release_object( thread_context );
1886 /* set the current context of a thread */
1887 DECL_HANDLER(set_thread_context)
1889 struct thread *thread;
1890 const context_t *context = get_req_data();
1892 if (get_req_data_size() < sizeof(context_t))
1894 set_error( STATUS_INVALID_PARAMETER );
1895 return;
1897 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1898 reply->self = (thread == current);
1900 if (thread->state == TERMINATED) set_error( STATUS_UNSUCCESSFUL );
1901 else if (context->cpu != thread->process->cpu) set_error( STATUS_INVALID_PARAMETER );
1902 else
1904 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1906 if (thread != current) stop_thread( thread );
1907 else if (system_flags) set_thread_context( thread, context, system_flags );
1908 if (thread->context && !get_error())
1910 copy_context( &thread->context->regs, context, context->flags );
1911 thread->context->regs.flags |= context->flags;
1915 release_object( thread );
1918 /* fetch a selector entry for a thread */
1919 DECL_HANDLER(get_selector_entry)
1921 struct thread *thread;
1922 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1924 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1925 release_object( thread );