dbghelp: Look for .eh_fram if no .eh_frame is found.
[wine.git] / server / thread.c
blob80db41b48d27321f6375df23162534549d01b9fa
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 timeout_t timeout;
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 operations */
131 static void dump_thread( struct object *obj, int verbose );
132 static struct object_type *thread_get_type( struct object *obj );
133 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
134 static unsigned int thread_map_access( struct object *obj, unsigned int access );
135 static void thread_poll_event( struct fd *fd, int event );
136 static struct list *thread_get_kernel_obj_list( struct object *obj );
137 static void destroy_thread( struct object *obj );
139 static const struct object_ops thread_ops =
141 sizeof(struct thread), /* size */
142 dump_thread, /* dump */
143 thread_get_type, /* get_type */
144 add_queue, /* add_queue */
145 remove_queue, /* remove_queue */
146 thread_signaled, /* signaled */
147 no_satisfied, /* satisfied */
148 no_signal, /* signal */
149 no_get_fd, /* get_fd */
150 thread_map_access, /* map_access */
151 default_get_sd, /* get_sd */
152 default_set_sd, /* set_sd */
153 no_lookup_name, /* lookup_name */
154 no_link_name, /* link_name */
155 NULL, /* unlink_name */
156 no_open_file, /* open_file */
157 thread_get_kernel_obj_list, /* get_kernel_obj_list */
158 no_close_handle, /* close_handle */
159 destroy_thread /* destroy */
162 static const struct fd_ops thread_fd_ops =
164 NULL, /* get_poll_events */
165 thread_poll_event, /* poll_event */
166 NULL, /* flush */
167 NULL, /* get_fd_type */
168 NULL, /* ioctl */
169 NULL, /* queue_async */
170 NULL /* reselect_async */
173 static struct list thread_list = LIST_INIT(thread_list);
175 /* initialize the structure for a newly allocated thread */
176 static inline void init_thread_structure( struct thread *thread )
178 int i;
180 thread->unix_pid = -1; /* not known yet */
181 thread->unix_tid = -1; /* not known yet */
182 thread->context = NULL;
183 thread->suspend_context = NULL;
184 thread->teb = 0;
185 thread->entry_point = 0;
186 thread->debug_ctx = NULL;
187 thread->system_regs = 0;
188 thread->queue = NULL;
189 thread->wait = NULL;
190 thread->error = 0;
191 thread->req_data = NULL;
192 thread->req_toread = 0;
193 thread->reply_data = NULL;
194 thread->reply_towrite = 0;
195 thread->request_fd = NULL;
196 thread->reply_fd = NULL;
197 thread->wait_fd = NULL;
198 thread->state = RUNNING;
199 thread->exit_code = 0;
200 thread->priority = 0;
201 thread->suspend = 0;
202 thread->desktop_users = 0;
203 thread->token = NULL;
204 thread->desc = NULL;
205 thread->desc_len = 0;
207 thread->creation_time = current_time;
208 thread->exit_time = 0;
210 list_init( &thread->mutex_list );
211 list_init( &thread->system_apc );
212 list_init( &thread->user_apc );
213 list_init( &thread->kernel_object );
215 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
216 thread->inflight[i].server = thread->inflight[i].client = -1;
219 /* check if address looks valid for a client-side data structure (TEB etc.) */
220 static inline int is_valid_address( client_ptr_t addr )
222 return addr && !(addr % sizeof(int));
225 /* create a new thread */
226 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
228 struct thread *thread;
229 int request_pipe[2];
231 if (fd == -1)
233 if (pipe( request_pipe ) == -1)
235 file_set_error();
236 return NULL;
238 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
240 close( request_pipe[0] );
241 close( request_pipe[1] );
242 return NULL;
244 close( request_pipe[1] );
245 fd = request_pipe[0];
248 if (process->is_terminating)
250 close( fd );
251 set_error( STATUS_PROCESS_IS_TERMINATING );
252 return NULL;
255 if (!(thread = alloc_object( &thread_ops )))
257 close( fd );
258 return NULL;
261 init_thread_structure( thread );
263 thread->process = (struct process *)grab_object( process );
264 thread->desktop = process->desktop;
265 thread->affinity = process->affinity;
266 if (!current) current = thread;
268 list_add_head( &thread_list, &thread->entry );
270 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
271 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
272 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
273 process->token ))
275 close( fd );
276 release_object( thread );
277 return NULL;
279 if (!(thread->id = alloc_ptid( thread )))
281 close( fd );
282 release_object( thread );
283 return NULL;
285 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
287 release_object( thread );
288 return NULL;
291 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
292 add_process_thread( thread->process, thread );
293 return thread;
296 /* handle a client event */
297 static void thread_poll_event( struct fd *fd, int event )
299 struct thread *thread = get_fd_user( fd );
300 assert( thread->obj.ops == &thread_ops );
302 grab_object( thread );
303 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
304 else if (event & POLLIN) read_request( thread );
305 else if (event & POLLOUT) write_reply( thread );
306 release_object( thread );
309 static struct list *thread_get_kernel_obj_list( struct object *obj )
311 struct thread *thread = (struct thread *)obj;
312 return &thread->kernel_object;
315 /* cleanup everything that is no longer needed by a dead thread */
316 /* used by destroy_thread and kill_thread */
317 static void cleanup_thread( struct thread *thread )
319 int i;
321 clear_apc_queue( &thread->system_apc );
322 clear_apc_queue( &thread->user_apc );
323 free( thread->req_data );
324 free( thread->reply_data );
325 if (thread->request_fd) release_object( thread->request_fd );
326 if (thread->reply_fd) release_object( thread->reply_fd );
327 if (thread->wait_fd) release_object( thread->wait_fd );
328 free( thread->suspend_context );
329 cleanup_clipboard_thread(thread);
330 destroy_thread_windows( thread );
331 free_msg_queue( thread );
332 close_thread_desktop( thread );
333 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
335 if (thread->inflight[i].client != -1)
337 close( thread->inflight[i].server );
338 thread->inflight[i].client = thread->inflight[i].server = -1;
341 free( thread->desc );
342 thread->req_data = NULL;
343 thread->reply_data = NULL;
344 thread->request_fd = NULL;
345 thread->reply_fd = NULL;
346 thread->wait_fd = NULL;
347 thread->context = NULL;
348 thread->suspend_context = NULL;
349 thread->desktop = 0;
350 thread->desc = NULL;
351 thread->desc_len = 0;
354 /* destroy a thread when its refcount is 0 */
355 static void destroy_thread( struct object *obj )
357 struct thread *thread = (struct thread *)obj;
358 assert( obj->ops == &thread_ops );
360 assert( !thread->debug_ctx ); /* cannot still be debugging something */
361 list_remove( &thread->entry );
362 cleanup_thread( thread );
363 release_object( thread->process );
364 if (thread->id) free_ptid( thread->id );
365 if (thread->token) release_object( thread->token );
368 /* dump a thread on stdout for debugging purposes */
369 static void dump_thread( struct object *obj, int verbose )
371 struct thread *thread = (struct thread *)obj;
372 assert( obj->ops == &thread_ops );
374 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
375 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
378 static struct object_type *thread_get_type( struct object *obj )
380 static const WCHAR name[] = {'T','h','r','e','a','d'};
381 static const struct unicode_str str = { name, sizeof(name) };
382 return get_object_type( &str );
385 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
387 struct thread *mythread = (struct thread *)obj;
388 return (mythread->state == TERMINATED);
391 static unsigned int thread_map_access( struct object *obj, unsigned int access )
393 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
394 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
395 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
396 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
397 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
399 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
400 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
402 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
405 static void dump_thread_apc( struct object *obj, int verbose )
407 struct thread_apc *apc = (struct thread_apc *)obj;
408 assert( obj->ops == &thread_apc_ops );
410 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
413 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
415 struct thread_apc *apc = (struct thread_apc *)obj;
416 return apc->executed;
419 static void thread_apc_destroy( struct object *obj )
421 struct thread_apc *apc = (struct thread_apc *)obj;
422 if (apc->caller) release_object( apc->caller );
423 if (apc->owner) release_object( apc->owner );
426 /* queue an async procedure call */
427 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
429 struct thread_apc *apc;
431 if ((apc = alloc_object( &thread_apc_ops )))
433 apc->call = *call_data;
434 apc->caller = NULL;
435 apc->owner = owner;
436 apc->executed = 0;
437 apc->result.type = APC_NONE;
438 if (owner) grab_object( owner );
440 return apc;
443 /* get a thread pointer from a thread id (and increment the refcount) */
444 struct thread *get_thread_from_id( thread_id_t id )
446 struct object *obj = get_ptid_entry( id );
448 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
449 set_error( STATUS_INVALID_CID );
450 return NULL;
453 /* get a thread from a handle (and increment the refcount) */
454 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
456 return (struct thread *)get_handle_obj( current->process, handle,
457 access, &thread_ops );
460 /* find a thread from a Unix tid */
461 struct thread *get_thread_from_tid( int tid )
463 struct thread *thread;
465 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
467 if (thread->unix_tid == tid) return thread;
469 return NULL;
472 /* find a thread from a Unix pid */
473 struct thread *get_thread_from_pid( int pid )
475 struct thread *thread;
477 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
479 if (thread->unix_pid == pid) return thread;
481 return NULL;
484 int set_thread_affinity( struct thread *thread, affinity_t affinity )
486 int ret = 0;
487 #ifdef HAVE_SCHED_SETAFFINITY
488 if (thread->unix_tid != -1)
490 cpu_set_t set;
491 int i;
492 affinity_t mask;
494 CPU_ZERO( &set );
495 for (i = 0, mask = 1; mask; i++, mask <<= 1)
496 if (affinity & mask) CPU_SET( i, &set );
498 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
500 #endif
501 if (!ret) thread->affinity = affinity;
502 return ret;
505 affinity_t get_thread_affinity( struct thread *thread )
507 affinity_t mask = 0;
508 #ifdef HAVE_SCHED_SETAFFINITY
509 if (thread->unix_tid != -1)
511 cpu_set_t set;
512 unsigned int i;
514 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
515 for (i = 0; i < 8 * sizeof(mask); i++)
516 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
518 #endif
519 if (!mask) mask = ~(affinity_t)0;
520 return mask;
523 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
524 #define THREAD_PRIORITY_REALTIME_LOWEST -7
526 /* set all information about a thread */
527 static void set_thread_info( struct thread *thread,
528 const struct set_thread_info_request *req )
530 if (req->mask & SET_THREAD_INFO_PRIORITY)
532 int max = THREAD_PRIORITY_HIGHEST;
533 int min = THREAD_PRIORITY_LOWEST;
534 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
536 max = THREAD_PRIORITY_REALTIME_HIGHEST;
537 min = THREAD_PRIORITY_REALTIME_LOWEST;
539 if ((req->priority >= min && req->priority <= max) ||
540 req->priority == THREAD_PRIORITY_IDLE ||
541 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
542 thread->priority = req->priority;
543 else
544 set_error( STATUS_INVALID_PARAMETER );
546 if (req->mask & SET_THREAD_INFO_AFFINITY)
548 if ((req->affinity & thread->process->affinity) != req->affinity)
549 set_error( STATUS_INVALID_PARAMETER );
550 else if (thread->state == TERMINATED)
551 set_error( STATUS_THREAD_IS_TERMINATING );
552 else if (set_thread_affinity( thread, req->affinity ))
553 file_set_error();
555 if (req->mask & SET_THREAD_INFO_TOKEN)
556 security_set_thread_token( thread, req->token );
557 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
558 thread->entry_point = req->entry_point;
559 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
561 WCHAR *desc;
562 data_size_t desc_len = get_req_data_size();
564 if (desc_len)
566 if ((desc = mem_alloc( desc_len )))
568 memcpy( desc, get_req_data(), desc_len );
569 free( thread->desc );
570 thread->desc = desc;
571 thread->desc_len = desc_len;
574 else
576 free( thread->desc );
577 thread->desc = NULL;
578 thread->desc_len = 0;
583 /* stop a thread (at the Unix level) */
584 void stop_thread( struct thread *thread )
586 if (thread->context) return; /* already inside a debug event, no need for a signal */
587 /* can't stop a thread while initialisation is in progress */
588 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
591 /* stop a thread if it's supposed to be suspended */
592 void stop_thread_if_suspended( struct thread *thread )
594 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
597 /* suspend a thread */
598 int suspend_thread( struct thread *thread )
600 int old_count = thread->suspend;
601 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
603 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
605 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
606 return old_count;
609 /* resume a thread */
610 int resume_thread( struct thread *thread )
612 int old_count = thread->suspend;
613 if (thread->suspend > 0)
615 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
617 return old_count;
620 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
621 int add_queue( struct object *obj, struct wait_queue_entry *entry )
623 grab_object( obj );
624 entry->obj = obj;
625 list_add_tail( &obj->wait_queue, &entry->entry );
626 return 1;
629 /* remove a thread from an object wait queue */
630 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
632 list_remove( &entry->entry );
633 release_object( obj );
636 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
638 return entry->wait->thread;
641 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
643 return entry->wait->select;
646 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
648 return entry->wait->key;
651 void make_wait_abandoned( struct wait_queue_entry *entry )
653 entry->wait->abandoned = 1;
656 /* finish waiting */
657 static unsigned int end_wait( struct thread *thread, unsigned int status )
659 struct thread_wait *wait = thread->wait;
660 struct wait_queue_entry *entry;
661 int i;
663 assert( wait );
664 thread->wait = wait->next;
666 if (status < wait->count) /* wait satisfied, tell it to the objects */
668 if (wait->select == SELECT_WAIT_ALL)
670 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
671 entry->obj->ops->satisfied( entry->obj, entry );
673 else
675 entry = wait->queues + status;
676 entry->obj->ops->satisfied( entry->obj, entry );
678 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
680 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
681 entry->obj->ops->remove_queue( entry->obj, entry );
682 if (wait->user) remove_timeout_user( wait->user );
683 free( wait );
684 return status;
687 /* build the thread wait structure */
688 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
689 int flags, timeout_t timeout )
691 struct thread_wait *wait;
692 struct wait_queue_entry *entry;
693 unsigned int i;
695 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
696 wait->next = current->wait;
697 wait->thread = current;
698 wait->count = count;
699 wait->flags = flags;
700 wait->select = select_op->op;
701 wait->cookie = 0;
702 wait->user = NULL;
703 wait->timeout = timeout;
704 wait->abandoned = 0;
705 current->wait = wait;
707 for (i = 0, entry = wait->queues; i < count; i++, entry++)
709 struct object *obj = objects[i];
710 entry->wait = wait;
711 if (!obj->ops->add_queue( obj, entry ))
713 wait->count = i;
714 end_wait( current, get_error() );
715 return 0;
718 return 1;
721 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
722 int flags, timeout_t timeout )
724 struct object *objects[MAXIMUM_WAIT_OBJECTS];
725 unsigned int i;
726 int ret = 0;
728 assert( count <= MAXIMUM_WAIT_OBJECTS );
730 for (i = 0; i < count; i++)
731 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
732 break;
734 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
736 while (i > 0) release_object( objects[--i] );
737 return ret;
740 /* check if the thread waiting condition is satisfied */
741 static int check_wait( struct thread *thread )
743 int i;
744 struct thread_wait *wait = thread->wait;
745 struct wait_queue_entry *entry;
747 assert( wait );
749 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
750 return STATUS_USER_APC;
752 /* Suspended threads may not acquire locks, but they can run system APCs */
753 if (thread->process->suspend + thread->suspend > 0) return -1;
755 if (wait->select == SELECT_WAIT_ALL)
757 int not_ok = 0;
758 /* Note: we must check them all anyway, as some objects may
759 * want to do something when signaled, even if others are not */
760 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
761 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
762 if (!not_ok) return STATUS_WAIT_0;
764 else
766 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
767 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
770 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
771 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
772 return -1;
775 /* send the wakeup signal to a thread */
776 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
778 struct wake_up_reply reply;
779 int ret;
781 memset( &reply, 0, sizeof(reply) );
782 reply.cookie = cookie;
783 reply.signaled = signaled;
784 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
785 return 0;
786 if (ret >= 0)
787 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
788 else if (errno == EPIPE)
789 kill_thread( thread, 0 ); /* normal death */
790 else
791 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
792 return -1;
795 /* attempt to wake up a thread */
796 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
797 int wake_thread( struct thread *thread )
799 int signaled, count;
800 client_ptr_t cookie;
802 for (count = 0; thread->wait; count++)
804 if ((signaled = check_wait( thread )) == -1) break;
806 cookie = thread->wait->cookie;
807 signaled = end_wait( thread, signaled );
808 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
809 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
811 if (!count) count = -1;
812 break;
815 return count;
818 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
819 int wake_thread_queue_entry( struct wait_queue_entry *entry )
821 struct thread_wait *wait = entry->wait;
822 struct thread *thread = wait->thread;
823 int signaled;
824 client_ptr_t cookie;
826 if (thread->wait != wait) return 0; /* not the current wait */
827 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
829 assert( wait->select != SELECT_WAIT_ALL );
831 cookie = wait->cookie;
832 signaled = end_wait( thread, entry - wait->queues );
833 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
835 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
836 wake_thread( thread ); /* check other waits too */
838 return 1;
841 /* thread wait timeout */
842 static void thread_timeout( void *ptr )
844 struct thread_wait *wait = ptr;
845 struct thread *thread = wait->thread;
846 client_ptr_t cookie = wait->cookie;
848 wait->user = NULL;
849 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
850 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
852 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
853 end_wait( thread, STATUS_TIMEOUT );
855 assert( cookie );
856 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
857 /* check if other objects have become signaled in the meantime */
858 wake_thread( thread );
861 /* try signaling an event flag, a semaphore or a mutex */
862 static int signal_object( obj_handle_t handle )
864 struct object *obj;
865 int ret = 0;
867 obj = get_handle_obj( current->process, handle, 0, NULL );
868 if (obj)
870 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
871 release_object( obj );
873 return ret;
876 /* select on a list of handles */
877 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
878 int flags, timeout_t timeout )
880 int ret;
881 unsigned int count;
882 struct object *object;
884 if (timeout <= 0) timeout = current_time - timeout;
886 switch (select_op->op)
888 case SELECT_NONE:
889 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
890 break;
892 case SELECT_WAIT:
893 case SELECT_WAIT_ALL:
894 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
895 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
897 set_error( STATUS_INVALID_PARAMETER );
898 return 0;
900 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
901 return timeout;
902 break;
904 case SELECT_SIGNAL_AND_WAIT:
905 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
906 return timeout;
907 if (select_op->signal_and_wait.signal)
909 if (!signal_object( select_op->signal_and_wait.signal ))
911 end_wait( current, get_error() );
912 return timeout;
914 /* check if we woke ourselves up */
915 if (!current->wait) return timeout;
917 break;
919 case SELECT_KEYED_EVENT_WAIT:
920 case SELECT_KEYED_EVENT_RELEASE:
921 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
922 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
923 if (!object) return timeout;
924 ret = wait_on( select_op, 1, &object, flags, timeout );
925 release_object( object );
926 if (!ret) return timeout;
927 current->wait->key = select_op->keyed_event.key;
928 break;
930 default:
931 set_error( STATUS_INVALID_PARAMETER );
932 return 0;
935 if ((ret = check_wait( current )) != -1)
937 /* condition is already satisfied */
938 set_error( end_wait( current, ret ));
939 return timeout;
942 /* now we need to wait */
943 if (current->wait->timeout != TIMEOUT_INFINITE)
945 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
946 thread_timeout, current->wait )))
948 end_wait( current, get_error() );
949 return timeout;
952 current->wait->cookie = cookie;
953 set_error( STATUS_PENDING );
954 return timeout;
957 /* attempt to wake threads sleeping on the object wait queue */
958 void wake_up( struct object *obj, int max )
960 struct list *ptr;
961 int ret;
963 LIST_FOR_EACH( ptr, &obj->wait_queue )
965 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
966 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
967 if (ret > 0 && max && !--max) break;
968 /* restart at the head of the list since a wake up can change the object wait queue */
969 ptr = &obj->wait_queue;
973 /* return the apc queue to use for a given apc type */
974 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
976 switch(type)
978 case APC_NONE:
979 case APC_USER:
980 case APC_TIMER:
981 return &thread->user_apc;
982 default:
983 return &thread->system_apc;
987 /* check if thread is currently waiting for a (system) apc */
988 static inline int is_in_apc_wait( struct thread *thread )
990 return (thread->process->suspend || thread->suspend ||
991 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
994 /* queue an existing APC to a given thread */
995 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
997 struct list *queue;
999 if (thread && thread->state == TERMINATED && process)
1000 thread = NULL;
1002 if (!thread) /* find a suitable thread inside the process */
1004 struct thread *candidate;
1006 /* first try to find a waiting thread */
1007 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1009 if (candidate->state == TERMINATED) continue;
1010 if (is_in_apc_wait( candidate ))
1012 thread = candidate;
1013 break;
1016 if (!thread)
1018 /* then use the first one that accepts a signal */
1019 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1021 if (send_thread_signal( candidate, SIGUSR1 ))
1023 thread = candidate;
1024 break;
1028 if (!thread) return 0; /* nothing found */
1029 queue = get_apc_queue( thread, apc->call.type );
1031 else
1033 if (thread->state == TERMINATED) return 0;
1034 queue = get_apc_queue( thread, apc->call.type );
1035 /* send signal for system APCs if needed */
1036 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1038 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1040 /* cancel a possible previous APC with the same owner */
1041 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1044 grab_object( apc );
1045 list_add_tail( queue, &apc->entry );
1046 if (!list_prev( queue, &apc->entry )) /* first one */
1047 wake_thread( thread );
1049 return 1;
1052 /* queue an async procedure call */
1053 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1055 struct thread_apc *apc;
1056 int ret = 0;
1058 if ((apc = create_apc( owner, call_data )))
1060 ret = queue_apc( process, thread, apc );
1061 release_object( apc );
1063 return ret;
1066 /* cancel the async procedure call owned by a specific object */
1067 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1069 struct thread_apc *apc;
1070 struct list *queue = get_apc_queue( thread, type );
1072 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1074 if (apc->owner != owner) continue;
1075 list_remove( &apc->entry );
1076 apc->executed = 1;
1077 wake_up( &apc->obj, 0 );
1078 release_object( apc );
1079 return;
1083 /* remove the head apc from the queue; the returned object must be released by the caller */
1084 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1086 struct thread_apc *apc = NULL;
1087 struct list *ptr = list_head( &thread->system_apc );
1089 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1090 if (ptr)
1092 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1093 list_remove( ptr );
1095 return apc;
1098 /* clear an APC queue, cancelling all the APCs on it */
1099 static void clear_apc_queue( struct list *queue )
1101 struct list *ptr;
1103 while ((ptr = list_head( queue )))
1105 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1106 list_remove( &apc->entry );
1107 apc->executed = 1;
1108 wake_up( &apc->obj, 0 );
1109 release_object( apc );
1113 /* add an fd to the inflight list */
1114 /* return list index, or -1 on error */
1115 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1117 int i;
1119 if (server == -1) return -1;
1120 if (client == -1)
1122 close( server );
1123 return -1;
1126 /* first check if we already have an entry for this fd */
1127 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1128 if (thread->inflight[i].client == client)
1130 close( thread->inflight[i].server );
1131 thread->inflight[i].server = server;
1132 return i;
1135 /* now find a free spot to store it */
1136 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1137 if (thread->inflight[i].client == -1)
1139 thread->inflight[i].client = client;
1140 thread->inflight[i].server = server;
1141 return i;
1144 close( server );
1145 return -1;
1148 /* get an inflight fd and purge it from the list */
1149 /* the fd must be closed when no longer used */
1150 int thread_get_inflight_fd( struct thread *thread, int client )
1152 int i, ret;
1154 if (client == -1) return -1;
1158 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1160 if (thread->inflight[i].client == client)
1162 ret = thread->inflight[i].server;
1163 thread->inflight[i].server = thread->inflight[i].client = -1;
1164 return ret;
1167 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1168 return -1;
1171 /* kill a thread on the spot */
1172 void kill_thread( struct thread *thread, int violent_death )
1174 if (thread->state == TERMINATED) return; /* already killed */
1175 thread->state = TERMINATED;
1176 thread->exit_time = current_time;
1177 if (current == thread) current = NULL;
1178 if (debug_level)
1179 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1180 thread->id, thread->exit_code );
1181 if (thread->wait)
1183 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1184 send_thread_wakeup( thread, 0, thread->exit_code );
1185 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1186 violent_death = 0;
1188 kill_console_processes( thread, 0 );
1189 debug_exit_thread( thread );
1190 abandon_mutexes( thread );
1191 wake_up( &thread->obj, 0 );
1192 if (violent_death) send_thread_signal( thread, SIGQUIT );
1193 cleanup_thread( thread );
1194 remove_process_thread( thread->process, thread );
1195 release_object( thread );
1198 /* copy parts of a context structure */
1199 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1201 assert( to->cpu == from->cpu );
1202 to->flags |= flags;
1203 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1204 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1205 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1206 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1207 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1208 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1211 /* return the context flags that correspond to system regs */
1212 /* (system regs are the ones we can't access on the client side) */
1213 static unsigned int get_context_system_regs( enum cpu_type cpu )
1215 switch (cpu)
1217 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1218 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1219 case CPU_POWERPC: return 0;
1220 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1221 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1223 return 0;
1226 /* take a snapshot of currently running threads */
1227 struct thread_snapshot *thread_snap( int *count )
1229 struct thread_snapshot *snapshot, *ptr;
1230 struct thread *thread;
1231 int total = 0;
1233 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1234 if (thread->state != TERMINATED) total++;
1235 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1236 ptr = snapshot;
1237 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1239 if (thread->state == TERMINATED) continue;
1240 ptr->thread = thread;
1241 ptr->count = thread->obj.refcount;
1242 ptr->priority = thread->priority;
1243 grab_object( thread );
1244 ptr++;
1246 *count = total;
1247 return snapshot;
1250 /* gets the current impersonation token */
1251 struct token *thread_get_impersonation_token( struct thread *thread )
1253 if (thread->token)
1254 return thread->token;
1255 else
1256 return thread->process->token;
1259 /* check if a cpu type can be supported on this server */
1260 int is_cpu_supported( enum cpu_type cpu )
1262 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1264 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1265 if (!(supported_cpus & prefix_cpu_mask))
1266 set_error( STATUS_NOT_SUPPORTED );
1267 else if (supported_cpus & CPU_FLAG(cpu))
1268 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1269 else
1270 set_error( STATUS_INVALID_IMAGE_FORMAT );
1271 return 0;
1274 /* return the cpu mask for supported cpus */
1275 unsigned int get_supported_cpu_mask(void)
1277 return supported_cpus & get_prefix_cpu_mask();
1280 /* create a new thread */
1281 DECL_HANDLER(new_thread)
1283 struct thread *thread;
1284 struct process *process;
1285 struct unicode_str name;
1286 const struct security_descriptor *sd;
1287 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1288 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1290 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1292 if (request_fd != -1) close( request_fd );
1293 return;
1296 if (process != current->process)
1298 if (request_fd != -1) /* can't create a request fd in a different process */
1300 close( request_fd );
1301 set_error( STATUS_INVALID_PARAMETER );
1302 goto done;
1304 if (process->running_threads) /* only the initial thread can be created in another process */
1306 set_error( STATUS_ACCESS_DENIED );
1307 goto done;
1310 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1312 if (request_fd != -1) close( request_fd );
1313 set_error( STATUS_INVALID_HANDLE );
1314 goto done;
1317 if ((thread = create_thread( request_fd, process, sd )))
1319 thread->system_regs = current->system_regs;
1320 if (req->suspend) thread->suspend++;
1321 reply->tid = get_thread_id( thread );
1322 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1323 req->access, objattr->attributes )))
1325 /* thread object will be released when the thread gets killed */
1326 goto done;
1328 kill_thread( thread, 1 );
1330 done:
1331 release_object( process );
1334 /* initialize a new thread */
1335 DECL_HANDLER(init_thread)
1337 struct process *process = current->process;
1338 int wait_fd, reply_fd;
1340 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1342 set_error( STATUS_TOO_MANY_OPENED_FILES );
1343 return;
1345 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1347 set_error( STATUS_TOO_MANY_OPENED_FILES );
1348 goto error;
1351 if (current->reply_fd) /* already initialised */
1353 set_error( STATUS_INVALID_PARAMETER );
1354 goto error;
1357 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1359 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1360 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1361 if (!current->reply_fd || !current->wait_fd) return;
1363 if (!is_valid_address(req->teb))
1365 set_error( STATUS_INVALID_PARAMETER );
1366 return;
1369 current->unix_pid = req->unix_pid;
1370 current->unix_tid = req->unix_tid;
1371 current->teb = req->teb;
1372 current->entry_point = process->peb ? req->entry : 0;
1374 if (!process->peb) /* first thread, initialize the process too */
1376 if (!is_cpu_supported( req->cpu )) return;
1377 process->unix_pid = current->unix_pid;
1378 process->peb = req->entry;
1379 process->cpu = req->cpu;
1380 reply->info_size = init_process( current );
1381 if (!process->parent_id)
1382 process->affinity = current->affinity = get_thread_affinity( current );
1383 else
1384 set_thread_affinity( current, current->affinity );
1386 else
1388 if (req->cpu != process->cpu)
1390 set_error( STATUS_INVALID_PARAMETER );
1391 return;
1393 if (process->unix_pid != current->unix_pid)
1394 process->unix_pid = -1; /* can happen with linuxthreads */
1395 init_thread_context( current );
1396 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1397 set_thread_affinity( current, current->affinity );
1399 debug_level = max( debug_level, req->debug_level );
1401 reply->pid = get_process_id( process );
1402 reply->tid = get_thread_id( current );
1403 reply->version = SERVER_PROTOCOL_VERSION;
1404 reply->server_start = server_start_time;
1405 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1406 reply->suspend = (current->suspend || process->suspend);
1407 return;
1409 error:
1410 if (reply_fd != -1) close( reply_fd );
1411 if (wait_fd != -1) close( wait_fd );
1414 /* terminate a thread */
1415 DECL_HANDLER(terminate_thread)
1417 struct thread *thread;
1419 reply->self = 0;
1420 reply->last = 0;
1421 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1423 thread->exit_code = req->exit_code;
1424 if (thread != current) kill_thread( thread, 1 );
1425 else
1427 reply->self = 1;
1428 reply->last = (thread->process->running_threads == 1);
1430 release_object( thread );
1434 /* open a handle to a thread */
1435 DECL_HANDLER(open_thread)
1437 struct thread *thread = get_thread_from_id( req->tid );
1439 reply->handle = 0;
1440 if (thread)
1442 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1443 release_object( thread );
1447 /* fetch information about a thread */
1448 DECL_HANDLER(get_thread_info)
1450 struct thread *thread;
1451 obj_handle_t handle = req->handle;
1453 if (!handle) thread = get_thread_from_id( req->tid_in );
1454 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1456 if (thread)
1458 reply->pid = get_process_id( thread->process );
1459 reply->tid = get_thread_id( thread );
1460 reply->teb = thread->teb;
1461 reply->entry_point = thread->entry_point;
1462 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1463 reply->priority = thread->priority;
1464 reply->affinity = thread->affinity;
1465 reply->last = thread->process->running_threads == 1;
1466 reply->suspend_count = thread->suspend;
1467 reply->desc_len = thread->desc_len;
1469 if (thread->desc && get_reply_max_size())
1471 if (thread->desc_len <= get_reply_max_size())
1472 set_reply_data( thread->desc, thread->desc_len );
1473 else
1474 set_error( STATUS_BUFFER_TOO_SMALL );
1477 release_object( thread );
1481 /* fetch information about thread times */
1482 DECL_HANDLER(get_thread_times)
1484 struct thread *thread;
1486 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1488 reply->creation_time = thread->creation_time;
1489 reply->exit_time = thread->exit_time;
1491 release_object( thread );
1495 /* set information about a thread */
1496 DECL_HANDLER(set_thread_info)
1498 struct thread *thread;
1500 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1502 set_thread_info( thread, req );
1503 release_object( thread );
1507 /* suspend a thread */
1508 DECL_HANDLER(suspend_thread)
1510 struct thread *thread;
1512 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1514 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1515 else reply->count = suspend_thread( thread );
1516 release_object( thread );
1520 /* resume a thread */
1521 DECL_HANDLER(resume_thread)
1523 struct thread *thread;
1525 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1527 reply->count = resume_thread( thread );
1528 release_object( thread );
1532 /* select on a handle list */
1533 DECL_HANDLER(select)
1535 select_op_t select_op;
1536 data_size_t op_size;
1537 struct thread_apc *apc;
1538 const apc_result_t *result = get_req_data();
1540 if (get_req_data_size() < sizeof(*result))
1542 set_error( STATUS_INVALID_PARAMETER );
1543 return;
1545 if (!req->cookie)
1547 set_error( STATUS_INVALID_PARAMETER );
1548 return;
1551 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1552 memset( &select_op, 0, sizeof(select_op) );
1553 memcpy( &select_op, result + 1, op_size );
1555 /* first store results of previous apc */
1556 if (req->prev_apc)
1558 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1559 0, &thread_apc_ops ))) return;
1560 apc->result = *result;
1561 apc->executed = 1;
1562 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1564 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1565 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1566 close_handle( current->process, apc->result.create_thread.handle );
1567 apc->result.create_thread.handle = handle;
1568 clear_error(); /* ignore errors from the above calls */
1570 else if (apc->result.type == APC_ASYNC_IO)
1572 if (apc->owner)
1573 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1575 wake_up( &apc->obj, 0 );
1576 close_handle( current->process, req->prev_apc );
1577 release_object( apc );
1580 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1582 if (get_error() == STATUS_USER_APC)
1584 for (;;)
1586 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1587 break;
1588 /* Optimization: ignore APC_NONE calls, they are only used to
1589 * wake up a thread, but since we got here the thread woke up already.
1591 if (apc->call.type != APC_NONE &&
1592 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1594 reply->call = apc->call;
1595 release_object( apc );
1596 break;
1598 apc->executed = 1;
1599 wake_up( &apc->obj, 0 );
1600 release_object( apc );
1605 /* queue an APC for a thread or process */
1606 DECL_HANDLER(queue_apc)
1608 struct thread *thread = NULL;
1609 struct process *process = NULL;
1610 struct thread_apc *apc;
1612 if (!(apc = create_apc( NULL, &req->call ))) return;
1614 switch (apc->call.type)
1616 case APC_NONE:
1617 case APC_USER:
1618 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1619 break;
1620 case APC_VIRTUAL_ALLOC:
1621 case APC_VIRTUAL_FREE:
1622 case APC_VIRTUAL_PROTECT:
1623 case APC_VIRTUAL_FLUSH:
1624 case APC_VIRTUAL_LOCK:
1625 case APC_VIRTUAL_UNLOCK:
1626 case APC_UNMAP_VIEW:
1627 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1628 break;
1629 case APC_VIRTUAL_QUERY:
1630 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1631 break;
1632 case APC_MAP_VIEW:
1633 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1634 if (process && process != current->process)
1636 /* duplicate the handle into the target process */
1637 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1638 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1639 if (handle) apc->call.map_view.handle = handle;
1640 else
1642 release_object( process );
1643 process = NULL;
1646 break;
1647 case APC_CREATE_THREAD:
1648 case APC_BREAK_PROCESS:
1649 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1650 break;
1651 default:
1652 set_error( STATUS_INVALID_PARAMETER );
1653 break;
1656 if (thread)
1658 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1659 release_object( thread );
1661 else if (process)
1663 reply->self = (process == current->process);
1664 if (!reply->self)
1666 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1667 if (handle)
1669 if (queue_apc( process, NULL, apc ))
1671 apc->caller = (struct thread *)grab_object( current );
1672 reply->handle = handle;
1674 else
1676 close_handle( current->process, handle );
1677 set_error( STATUS_PROCESS_IS_TERMINATING );
1681 release_object( process );
1684 release_object( apc );
1687 /* Get the result of an APC call */
1688 DECL_HANDLER(get_apc_result)
1690 struct thread_apc *apc;
1692 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1693 0, &thread_apc_ops ))) return;
1695 if (apc->executed) reply->result = apc->result;
1696 else set_error( STATUS_PENDING );
1698 /* close the handle directly to avoid an extra round-trip */
1699 close_handle( current->process, req->handle );
1700 release_object( apc );
1703 /* retrieve the current context of a thread */
1704 DECL_HANDLER(get_thread_context)
1706 struct thread *thread;
1707 context_t *context;
1709 if (get_reply_max_size() < sizeof(context_t))
1711 set_error( STATUS_INVALID_PARAMETER );
1712 return;
1714 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1715 reply->self = (thread == current);
1717 if (thread != current && !thread->context)
1719 /* thread is not suspended, retry (if it's still running) */
1720 if (thread->state == RUNNING)
1722 set_error( STATUS_PENDING );
1723 if (req->suspend)
1725 release_object( thread );
1726 /* make sure we have suspend access */
1727 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1728 suspend_thread( thread );
1731 else set_error( STATUS_UNSUCCESSFUL );
1733 else if ((context = set_reply_data_size( sizeof(context_t) )))
1735 unsigned int flags = get_context_system_regs( thread->process->cpu );
1737 memset( context, 0, sizeof(context_t) );
1738 context->cpu = thread->process->cpu;
1739 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1740 if (req->flags & flags) get_thread_context( thread, context, req->flags & flags );
1742 release_object( thread );
1745 /* set the current context of a thread */
1746 DECL_HANDLER(set_thread_context)
1748 struct thread *thread;
1749 const context_t *context = get_req_data();
1751 if (get_req_data_size() < sizeof(context_t))
1753 set_error( STATUS_INVALID_PARAMETER );
1754 return;
1756 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1757 reply->self = (thread == current);
1759 if (thread != current && !thread->context)
1761 /* thread is not suspended, retry (if it's still running) */
1762 if (thread->state == RUNNING)
1764 set_error( STATUS_PENDING );
1765 if (req->suspend)
1767 release_object( thread );
1768 /* make sure we have suspend access */
1769 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1770 suspend_thread( thread );
1773 else set_error( STATUS_UNSUCCESSFUL );
1775 else if (context->cpu == thread->process->cpu)
1777 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1778 unsigned int client_flags = context->flags & ~system_flags;
1780 if (system_flags) set_thread_context( thread, context, system_flags );
1781 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1783 else set_error( STATUS_INVALID_PARAMETER );
1785 release_object( thread );
1788 /* retrieve the suspended context of a thread */
1789 DECL_HANDLER(get_suspend_context)
1791 if (get_reply_max_size() < sizeof(context_t))
1793 set_error( STATUS_INVALID_PARAMETER );
1794 return;
1797 if (current->suspend_context)
1799 if (current->suspend_context->flags)
1800 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1801 else
1802 free( current->suspend_context );
1803 if (current->context == current->suspend_context)
1805 current->context = NULL;
1806 stop_thread_if_suspended( current );
1808 current->suspend_context = NULL;
1810 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1813 /* store the suspended context of a thread */
1814 DECL_HANDLER(set_suspend_context)
1816 const context_t *context = get_req_data();
1818 if (get_req_data_size() < sizeof(context_t))
1820 set_error( STATUS_INVALID_PARAMETER );
1821 return;
1824 if (current->context || context->cpu != current->process->cpu)
1826 /* nested suspend or exception, shouldn't happen */
1827 set_error( STATUS_INVALID_PARAMETER );
1829 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1831 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1832 current->suspend_context->flags = 0; /* to keep track of what is modified */
1833 current->context = current->suspend_context;
1837 /* fetch a selector entry for a thread */
1838 DECL_HANDLER(get_selector_entry)
1840 struct thread *thread;
1841 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1843 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1844 release_object( thread );