setupapi: SetupDiCreateDeviceInfoListEx returns INVALID_HANDLE_VALUE on error.
[wine/multimedia.git] / server / thread.c
blob981bcc1e74ee1b3d9f4a65bfd530ba84e0f17499
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_open_file, /* open_file */
121 no_close_handle, /* close_handle */
122 thread_apc_destroy /* destroy */
126 /* thread operations */
128 static void dump_thread( struct object *obj, int verbose );
129 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
130 static unsigned int thread_map_access( struct object *obj, unsigned int access );
131 static void thread_poll_event( struct fd *fd, int event );
132 static void destroy_thread( struct object *obj );
134 static const struct object_ops thread_ops =
136 sizeof(struct thread), /* size */
137 dump_thread, /* dump */
138 no_get_type, /* get_type */
139 add_queue, /* add_queue */
140 remove_queue, /* remove_queue */
141 thread_signaled, /* signaled */
142 no_satisfied, /* satisfied */
143 no_signal, /* signal */
144 no_get_fd, /* get_fd */
145 thread_map_access, /* map_access */
146 default_get_sd, /* get_sd */
147 default_set_sd, /* set_sd */
148 no_lookup_name, /* lookup_name */
149 no_open_file, /* open_file */
150 no_close_handle, /* close_handle */
151 destroy_thread /* destroy */
154 static const struct fd_ops thread_fd_ops =
156 NULL, /* get_poll_events */
157 thread_poll_event, /* poll_event */
158 NULL, /* flush */
159 NULL, /* get_fd_type */
160 NULL, /* ioctl */
161 NULL, /* queue_async */
162 NULL, /* reselect_async */
163 NULL /* cancel_async */
166 static struct list thread_list = LIST_INIT(thread_list);
168 /* initialize the structure for a newly allocated thread */
169 static inline void init_thread_structure( struct thread *thread )
171 int i;
173 thread->unix_pid = -1; /* not known yet */
174 thread->unix_tid = -1; /* not known yet */
175 thread->context = NULL;
176 thread->suspend_context = NULL;
177 thread->teb = 0;
178 thread->entry_point = 0;
179 thread->debug_ctx = NULL;
180 thread->debug_event = NULL;
181 thread->debug_break = 0;
182 thread->queue = NULL;
183 thread->wait = NULL;
184 thread->error = 0;
185 thread->req_data = NULL;
186 thread->req_toread = 0;
187 thread->reply_data = NULL;
188 thread->reply_towrite = 0;
189 thread->request_fd = NULL;
190 thread->reply_fd = NULL;
191 thread->wait_fd = NULL;
192 thread->state = RUNNING;
193 thread->exit_code = 0;
194 thread->priority = 0;
195 thread->suspend = 0;
196 thread->desktop_users = 0;
197 thread->token = NULL;
199 thread->creation_time = current_time;
200 thread->exit_time = 0;
202 list_init( &thread->mutex_list );
203 list_init( &thread->system_apc );
204 list_init( &thread->user_apc );
206 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
207 thread->inflight[i].server = thread->inflight[i].client = -1;
210 /* check if address looks valid for a client-side data structure (TEB etc.) */
211 static inline int is_valid_address( client_ptr_t addr )
213 return addr && !(addr % sizeof(int));
216 /* create a new thread */
217 struct thread *create_thread( int fd, struct process *process )
219 struct thread *thread;
221 if (process->is_terminating)
223 close( fd );
224 set_error( STATUS_PROCESS_IS_TERMINATING );
225 return NULL;
228 if (!(thread = alloc_object( &thread_ops )))
230 close( fd );
231 return NULL;
234 init_thread_structure( thread );
236 thread->process = (struct process *)grab_object( process );
237 thread->desktop = process->desktop;
238 thread->affinity = process->affinity;
239 if (!current) current = thread;
241 list_add_head( &thread_list, &thread->entry );
243 if (!(thread->id = alloc_ptid( thread )))
245 close( fd );
246 release_object( thread );
247 return NULL;
249 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
251 release_object( thread );
252 return NULL;
255 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
256 add_process_thread( thread->process, thread );
257 return thread;
260 /* handle a client event */
261 static void thread_poll_event( struct fd *fd, int event )
263 struct thread *thread = get_fd_user( fd );
264 assert( thread->obj.ops == &thread_ops );
266 grab_object( thread );
267 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
268 else if (event & POLLIN) read_request( thread );
269 else if (event & POLLOUT) write_reply( thread );
270 release_object( thread );
273 /* cleanup everything that is no longer needed by a dead thread */
274 /* used by destroy_thread and kill_thread */
275 static void cleanup_thread( struct thread *thread )
277 int i;
279 clear_apc_queue( &thread->system_apc );
280 clear_apc_queue( &thread->user_apc );
281 free( thread->req_data );
282 free( thread->reply_data );
283 if (thread->request_fd) release_object( thread->request_fd );
284 if (thread->reply_fd) release_object( thread->reply_fd );
285 if (thread->wait_fd) release_object( thread->wait_fd );
286 free( thread->suspend_context );
287 cleanup_clipboard_thread(thread);
288 destroy_thread_windows( thread );
289 free_msg_queue( thread );
290 close_thread_desktop( thread );
291 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
293 if (thread->inflight[i].client != -1)
295 close( thread->inflight[i].server );
296 thread->inflight[i].client = thread->inflight[i].server = -1;
299 thread->req_data = NULL;
300 thread->reply_data = NULL;
301 thread->request_fd = NULL;
302 thread->reply_fd = NULL;
303 thread->wait_fd = NULL;
304 thread->context = NULL;
305 thread->suspend_context = NULL;
306 thread->desktop = 0;
309 /* destroy a thread when its refcount is 0 */
310 static void destroy_thread( struct object *obj )
312 struct thread *thread = (struct thread *)obj;
313 assert( obj->ops == &thread_ops );
315 assert( !thread->debug_ctx ); /* cannot still be debugging something */
316 list_remove( &thread->entry );
317 cleanup_thread( thread );
318 release_object( thread->process );
319 if (thread->id) free_ptid( thread->id );
320 if (thread->token) release_object( thread->token );
323 /* dump a thread on stdout for debugging purposes */
324 static void dump_thread( struct object *obj, int verbose )
326 struct thread *thread = (struct thread *)obj;
327 assert( obj->ops == &thread_ops );
329 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
330 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
333 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
335 struct thread *mythread = (struct thread *)obj;
336 return (mythread->state == TERMINATED);
339 static unsigned int thread_map_access( struct object *obj, unsigned int access )
341 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT | THREAD_QUERY_LIMITED_INFORMATION;
342 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
343 THREAD_TERMINATE | THREAD_SUSPEND_RESUME | THREAD_SET_LIMITED_INFORMATION;
344 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
345 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
346 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
349 static void dump_thread_apc( struct object *obj, int verbose )
351 struct thread_apc *apc = (struct thread_apc *)obj;
352 assert( obj->ops == &thread_apc_ops );
354 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
357 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
359 struct thread_apc *apc = (struct thread_apc *)obj;
360 return apc->executed;
363 static void thread_apc_destroy( struct object *obj )
365 struct thread_apc *apc = (struct thread_apc *)obj;
366 if (apc->caller) release_object( apc->caller );
367 if (apc->owner) release_object( apc->owner );
370 /* queue an async procedure call */
371 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
373 struct thread_apc *apc;
375 if ((apc = alloc_object( &thread_apc_ops )))
377 apc->call = *call_data;
378 apc->caller = NULL;
379 apc->owner = owner;
380 apc->executed = 0;
381 apc->result.type = APC_NONE;
382 if (owner) grab_object( owner );
384 return apc;
387 /* get a thread pointer from a thread id (and increment the refcount) */
388 struct thread *get_thread_from_id( thread_id_t id )
390 struct object *obj = get_ptid_entry( id );
392 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
393 set_error( STATUS_INVALID_CID );
394 return NULL;
397 /* get a thread from a handle (and increment the refcount) */
398 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
400 return (struct thread *)get_handle_obj( current->process, handle,
401 access, &thread_ops );
404 /* find a thread from a Unix tid */
405 struct thread *get_thread_from_tid( int tid )
407 struct thread *thread;
409 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
411 if (thread->unix_tid == tid) return thread;
413 return NULL;
416 /* find a thread from a Unix pid */
417 struct thread *get_thread_from_pid( int pid )
419 struct thread *thread;
421 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
423 if (thread->unix_pid == pid) return thread;
425 return NULL;
428 int set_thread_affinity( struct thread *thread, affinity_t affinity )
430 int ret = 0;
431 #ifdef HAVE_SCHED_SETAFFINITY
432 if (thread->unix_tid != -1)
434 cpu_set_t set;
435 int i;
436 affinity_t mask;
438 CPU_ZERO( &set );
439 for (i = 0, mask = 1; mask; i++, mask <<= 1)
440 if (affinity & mask) CPU_SET( i, &set );
442 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
444 #endif
445 if (!ret) thread->affinity = affinity;
446 return ret;
449 affinity_t get_thread_affinity( struct thread *thread )
451 affinity_t mask = 0;
452 #ifdef HAVE_SCHED_SETAFFINITY
453 if (thread->unix_tid != -1)
455 cpu_set_t set;
456 unsigned int i;
458 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
459 for (i = 0; i < 8 * sizeof(mask); i++)
460 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
462 #endif
463 if (!mask) mask = ~(affinity_t)0;
464 return mask;
467 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
468 #define THREAD_PRIORITY_REALTIME_LOWEST -7
470 /* set all information about a thread */
471 static void set_thread_info( struct thread *thread,
472 const struct set_thread_info_request *req )
474 if (req->mask & SET_THREAD_INFO_PRIORITY)
476 int max = THREAD_PRIORITY_HIGHEST;
477 int min = THREAD_PRIORITY_LOWEST;
478 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
480 max = THREAD_PRIORITY_REALTIME_HIGHEST;
481 min = THREAD_PRIORITY_REALTIME_LOWEST;
483 if ((req->priority >= min && req->priority <= max) ||
484 req->priority == THREAD_PRIORITY_IDLE ||
485 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
486 thread->priority = req->priority;
487 else
488 set_error( STATUS_INVALID_PARAMETER );
490 if (req->mask & SET_THREAD_INFO_AFFINITY)
492 if ((req->affinity & thread->process->affinity) != req->affinity)
493 set_error( STATUS_INVALID_PARAMETER );
494 else if (thread->state == TERMINATED)
495 set_error( STATUS_THREAD_IS_TERMINATING );
496 else if (set_thread_affinity( thread, req->affinity ))
497 file_set_error();
499 if (req->mask & SET_THREAD_INFO_TOKEN)
500 security_set_thread_token( thread, req->token );
501 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
502 thread->entry_point = req->entry_point;
505 /* stop a thread (at the Unix level) */
506 void stop_thread( struct thread *thread )
508 if (thread->context) return; /* already inside a debug event, no need for a signal */
509 /* can't stop a thread while initialisation is in progress */
510 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
513 /* stop a thread if it's supposed to be suspended */
514 void stop_thread_if_suspended( struct thread *thread )
516 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
519 /* suspend a thread */
520 static int suspend_thread( struct thread *thread )
522 int old_count = thread->suspend;
523 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
525 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
527 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
528 return old_count;
531 /* resume a thread */
532 static int resume_thread( struct thread *thread )
534 int old_count = thread->suspend;
535 if (thread->suspend > 0)
537 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
539 return old_count;
542 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
543 int add_queue( struct object *obj, struct wait_queue_entry *entry )
545 grab_object( obj );
546 entry->obj = obj;
547 list_add_tail( &obj->wait_queue, &entry->entry );
548 return 1;
551 /* remove a thread from an object wait queue */
552 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
554 list_remove( &entry->entry );
555 release_object( obj );
558 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
560 return entry->wait->thread;
563 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
565 return entry->wait->select;
568 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
570 return entry->wait->key;
573 void make_wait_abandoned( struct wait_queue_entry *entry )
575 entry->wait->abandoned = 1;
578 /* finish waiting */
579 static void end_wait( struct thread *thread )
581 struct thread_wait *wait = thread->wait;
582 struct wait_queue_entry *entry;
583 int i;
585 assert( wait );
586 thread->wait = wait->next;
587 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
588 entry->obj->ops->remove_queue( entry->obj, entry );
589 if (wait->user) remove_timeout_user( wait->user );
590 free( wait );
593 /* build the thread wait structure */
594 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
595 int flags, timeout_t timeout )
597 struct thread_wait *wait;
598 struct wait_queue_entry *entry;
599 unsigned int i;
601 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
602 wait->next = current->wait;
603 wait->thread = current;
604 wait->count = count;
605 wait->flags = flags;
606 wait->select = select_op->op;
607 wait->cookie = 0;
608 wait->user = NULL;
609 wait->timeout = timeout;
610 wait->abandoned = 0;
611 current->wait = wait;
613 for (i = 0, entry = wait->queues; i < count; i++, entry++)
615 struct object *obj = objects[i];
616 entry->wait = wait;
617 if (!obj->ops->add_queue( obj, entry ))
619 wait->count = i;
620 end_wait( current );
621 return 0;
624 return 1;
627 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
628 int flags, timeout_t timeout )
630 struct object *objects[MAXIMUM_WAIT_OBJECTS];
631 unsigned int i;
632 int ret = 0;
634 assert( count <= MAXIMUM_WAIT_OBJECTS );
636 for (i = 0; i < count; i++)
637 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
638 break;
640 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
642 while (i > 0) release_object( objects[--i] );
643 return ret;
646 /* check if the thread waiting condition is satisfied */
647 static int check_wait( struct thread *thread )
649 int i;
650 struct thread_wait *wait = thread->wait;
651 struct wait_queue_entry *entry;
653 assert( wait );
655 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
656 return STATUS_USER_APC;
658 /* Suspended threads may not acquire locks, but they can run system APCs */
659 if (thread->process->suspend + thread->suspend > 0) return -1;
661 if (wait->select == SELECT_WAIT_ALL)
663 int not_ok = 0;
664 /* Note: we must check them all anyway, as some objects may
665 * want to do something when signaled, even if others are not */
666 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
667 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
668 if (not_ok) goto other_checks;
669 /* Wait satisfied: tell it to all objects */
670 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
671 entry->obj->ops->satisfied( entry->obj, entry );
672 return wait->abandoned ? STATUS_ABANDONED_WAIT_0 : STATUS_WAIT_0;
674 else
676 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
678 if (!entry->obj->ops->signaled( entry->obj, entry )) continue;
679 /* Wait satisfied: tell it to the object */
680 entry->obj->ops->satisfied( entry->obj, entry );
681 if (wait->abandoned) i += STATUS_ABANDONED_WAIT_0;
682 return i;
686 other_checks:
687 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
688 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
689 return -1;
692 /* send the wakeup signal to a thread */
693 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
695 struct wake_up_reply reply;
696 int ret;
698 memset( &reply, 0, sizeof(reply) );
699 reply.cookie = cookie;
700 reply.signaled = signaled;
701 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
702 return 0;
703 if (ret >= 0)
704 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
705 else if (errno == EPIPE)
706 kill_thread( thread, 0 ); /* normal death */
707 else
708 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
709 return -1;
712 /* attempt to wake up a thread */
713 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
714 int wake_thread( struct thread *thread )
716 int signaled, count;
717 client_ptr_t cookie;
719 for (count = 0; thread->wait; count++)
721 if ((signaled = check_wait( thread )) == -1) break;
723 cookie = thread->wait->cookie;
724 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
725 end_wait( thread );
726 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
728 if (!count) count = -1;
729 break;
732 return count;
735 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
736 int wake_thread_queue_entry( struct wait_queue_entry *entry )
738 struct thread_wait *wait = entry->wait;
739 struct thread *thread = wait->thread;
740 int signaled;
741 client_ptr_t cookie;
743 if (thread->wait != wait) return 0; /* not the current wait */
744 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
746 assert( wait->select != SELECT_WAIT_ALL );
748 signaled = entry - wait->queues;
749 entry->obj->ops->satisfied( entry->obj, entry );
750 if (wait->abandoned) signaled += STATUS_ABANDONED_WAIT_0;
752 cookie = wait->cookie;
753 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
754 end_wait( thread );
756 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
757 wake_thread( thread ); /* check other waits too */
759 return 1;
762 /* thread wait timeout */
763 static void thread_timeout( void *ptr )
765 struct thread_wait *wait = ptr;
766 struct thread *thread = wait->thread;
767 client_ptr_t cookie = wait->cookie;
769 wait->user = NULL;
770 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
771 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
773 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
774 end_wait( thread );
776 assert( cookie );
777 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
778 /* check if other objects have become signaled in the meantime */
779 wake_thread( thread );
782 /* try signaling an event flag, a semaphore or a mutex */
783 static int signal_object( obj_handle_t handle )
785 struct object *obj;
786 int ret = 0;
788 obj = get_handle_obj( current->process, handle, 0, NULL );
789 if (obj)
791 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
792 release_object( obj );
794 return ret;
797 /* select on a list of handles */
798 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
799 int flags, timeout_t timeout )
801 int ret;
802 unsigned int count;
803 struct object *object;
805 if (timeout <= 0) timeout = current_time - timeout;
807 switch (select_op->op)
809 case SELECT_NONE:
810 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
811 break;
813 case SELECT_WAIT:
814 case SELECT_WAIT_ALL:
815 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
816 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
818 set_error( STATUS_INVALID_PARAMETER );
819 return 0;
821 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
822 return timeout;
823 break;
825 case SELECT_SIGNAL_AND_WAIT:
826 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
827 return timeout;
828 if (select_op->signal_and_wait.signal)
830 if (!signal_object( select_op->signal_and_wait.signal ))
832 end_wait( current );
833 return timeout;
835 /* check if we woke ourselves up */
836 if (!current->wait) return timeout;
838 break;
840 case SELECT_KEYED_EVENT_WAIT:
841 case SELECT_KEYED_EVENT_RELEASE:
842 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
843 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
844 if (!object) return timeout;
845 ret = wait_on( select_op, 1, &object, flags, timeout );
846 release_object( object );
847 if (!ret) return timeout;
848 current->wait->key = select_op->keyed_event.key;
849 break;
851 default:
852 set_error( STATUS_INVALID_PARAMETER );
853 return 0;
856 if ((ret = check_wait( current )) != -1)
858 /* condition is already satisfied */
859 end_wait( current );
860 set_error( ret );
861 return timeout;
864 /* now we need to wait */
865 if (current->wait->timeout != TIMEOUT_INFINITE)
867 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
868 thread_timeout, current->wait )))
870 end_wait( current );
871 return timeout;
874 current->wait->cookie = cookie;
875 set_error( STATUS_PENDING );
876 return timeout;
879 /* attempt to wake threads sleeping on the object wait queue */
880 void wake_up( struct object *obj, int max )
882 struct list *ptr;
883 int ret;
885 LIST_FOR_EACH( ptr, &obj->wait_queue )
887 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
888 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
889 if (ret > 0 && max && !--max) break;
890 /* restart at the head of the list since a wake up can change the object wait queue */
891 ptr = &obj->wait_queue;
895 /* return the apc queue to use for a given apc type */
896 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
898 switch(type)
900 case APC_NONE:
901 case APC_USER:
902 case APC_TIMER:
903 return &thread->user_apc;
904 default:
905 return &thread->system_apc;
909 /* check if thread is currently waiting for a (system) apc */
910 static inline int is_in_apc_wait( struct thread *thread )
912 return (thread->process->suspend || thread->suspend ||
913 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
916 /* queue an existing APC to a given thread */
917 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
919 struct list *queue;
921 if (!thread) /* find a suitable thread inside the process */
923 struct thread *candidate;
925 /* first try to find a waiting thread */
926 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
928 if (candidate->state == TERMINATED) continue;
929 if (is_in_apc_wait( candidate ))
931 thread = candidate;
932 break;
935 if (!thread)
937 /* then use the first one that accepts a signal */
938 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
940 if (send_thread_signal( candidate, SIGUSR1 ))
942 thread = candidate;
943 break;
947 if (!thread) return 0; /* nothing found */
948 queue = get_apc_queue( thread, apc->call.type );
950 else
952 if (thread->state == TERMINATED) return 0;
953 queue = get_apc_queue( thread, apc->call.type );
954 /* send signal for system APCs if needed */
955 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
957 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
959 /* cancel a possible previous APC with the same owner */
960 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
963 grab_object( apc );
964 list_add_tail( queue, &apc->entry );
965 if (!list_prev( queue, &apc->entry )) /* first one */
966 wake_thread( thread );
968 return 1;
971 /* queue an async procedure call */
972 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
974 struct thread_apc *apc;
975 int ret = 0;
977 if ((apc = create_apc( owner, call_data )))
979 ret = queue_apc( NULL, thread, apc );
980 release_object( apc );
982 return ret;
985 /* cancel the async procedure call owned by a specific object */
986 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
988 struct thread_apc *apc;
989 struct list *queue = get_apc_queue( thread, type );
991 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
993 if (apc->owner != owner) continue;
994 list_remove( &apc->entry );
995 apc->executed = 1;
996 wake_up( &apc->obj, 0 );
997 release_object( apc );
998 return;
1002 /* remove the head apc from the queue; the returned object must be released by the caller */
1003 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1005 struct thread_apc *apc = NULL;
1006 struct list *ptr = list_head( &thread->system_apc );
1008 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1009 if (ptr)
1011 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1012 list_remove( ptr );
1014 return apc;
1017 /* clear an APC queue, cancelling all the APCs on it */
1018 static void clear_apc_queue( struct list *queue )
1020 struct list *ptr;
1022 while ((ptr = list_head( queue )))
1024 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1025 list_remove( &apc->entry );
1026 apc->executed = 1;
1027 wake_up( &apc->obj, 0 );
1028 release_object( apc );
1032 /* add an fd to the inflight list */
1033 /* return list index, or -1 on error */
1034 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1036 int i;
1038 if (server == -1) return -1;
1039 if (client == -1)
1041 close( server );
1042 return -1;
1045 /* first check if we already have an entry for this fd */
1046 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1047 if (thread->inflight[i].client == client)
1049 close( thread->inflight[i].server );
1050 thread->inflight[i].server = server;
1051 return i;
1054 /* now find a free spot to store it */
1055 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1056 if (thread->inflight[i].client == -1)
1058 thread->inflight[i].client = client;
1059 thread->inflight[i].server = server;
1060 return i;
1063 close( server );
1064 return -1;
1067 /* get an inflight fd and purge it from the list */
1068 /* the fd must be closed when no longer used */
1069 int thread_get_inflight_fd( struct thread *thread, int client )
1071 int i, ret;
1073 if (client == -1) return -1;
1077 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1079 if (thread->inflight[i].client == client)
1081 ret = thread->inflight[i].server;
1082 thread->inflight[i].server = thread->inflight[i].client = -1;
1083 return ret;
1086 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1087 return -1;
1090 /* kill a thread on the spot */
1091 void kill_thread( struct thread *thread, int violent_death )
1093 if (thread->state == TERMINATED) return; /* already killed */
1094 thread->state = TERMINATED;
1095 thread->exit_time = current_time;
1096 if (current == thread) current = NULL;
1097 if (debug_level)
1098 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1099 thread->id, thread->exit_code );
1100 if (thread->wait)
1102 while (thread->wait) end_wait( thread );
1103 send_thread_wakeup( thread, 0, thread->exit_code );
1104 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1105 violent_death = 0;
1107 kill_console_processes( thread, 0 );
1108 debug_exit_thread( thread );
1109 abandon_mutexes( thread );
1110 wake_up( &thread->obj, 0 );
1111 if (violent_death) send_thread_signal( thread, SIGQUIT );
1112 cleanup_thread( thread );
1113 remove_process_thread( thread->process, thread );
1114 release_object( thread );
1117 /* copy parts of a context structure */
1118 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1120 assert( to->cpu == from->cpu );
1121 to->flags |= flags;
1122 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1123 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1124 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1125 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1126 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1127 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1130 /* return the context flags that correspond to system regs */
1131 /* (system regs are the ones we can't access on the client side) */
1132 static unsigned int get_context_system_regs( enum cpu_type cpu )
1134 switch (cpu)
1136 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1137 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1138 case CPU_POWERPC: return 0;
1139 case CPU_ARM: return 0;
1140 case CPU_ARM64: return 0;
1142 return 0;
1145 /* trigger a breakpoint event in a given thread */
1146 void break_thread( struct thread *thread )
1148 debug_event_t data;
1150 assert( thread->context );
1152 memset( &data, 0, sizeof(data) );
1153 data.exception.first = 1;
1154 data.exception.exc_code = STATUS_BREAKPOINT;
1155 data.exception.flags = EXCEPTION_CONTINUABLE;
1156 switch (thread->context->cpu)
1158 case CPU_x86:
1159 data.exception.address = thread->context->ctl.i386_regs.eip;
1160 break;
1161 case CPU_x86_64:
1162 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1163 break;
1164 case CPU_POWERPC:
1165 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1166 break;
1167 case CPU_ARM:
1168 data.exception.address = thread->context->ctl.arm_regs.pc;
1169 break;
1170 case CPU_ARM64:
1171 data.exception.address = thread->context->ctl.arm64_regs.pc;
1172 break;
1174 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1175 thread->debug_break = 0;
1178 /* take a snapshot of currently running threads */
1179 struct thread_snapshot *thread_snap( int *count )
1181 struct thread_snapshot *snapshot, *ptr;
1182 struct thread *thread;
1183 int total = 0;
1185 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1186 if (thread->state != TERMINATED) total++;
1187 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1188 ptr = snapshot;
1189 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1191 if (thread->state == TERMINATED) continue;
1192 ptr->thread = thread;
1193 ptr->count = thread->obj.refcount;
1194 ptr->priority = thread->priority;
1195 grab_object( thread );
1196 ptr++;
1198 *count = total;
1199 return snapshot;
1202 /* gets the current impersonation token */
1203 struct token *thread_get_impersonation_token( struct thread *thread )
1205 if (thread->token)
1206 return thread->token;
1207 else
1208 return thread->process->token;
1211 /* check if a cpu type can be supported on this server */
1212 int is_cpu_supported( enum cpu_type cpu )
1214 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1216 if (CPU_FLAG(cpu) && (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu))) return 1;
1217 if (!(supported_cpus & prefix_cpu_mask))
1218 set_error( STATUS_NOT_SUPPORTED );
1219 else if (supported_cpus & CPU_FLAG(cpu))
1220 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1221 else
1222 set_error( STATUS_INVALID_IMAGE_FORMAT );
1223 return 0;
1226 /* create a new thread */
1227 DECL_HANDLER(new_thread)
1229 struct thread *thread;
1230 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1232 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1234 if (request_fd != -1) close( request_fd );
1235 set_error( STATUS_INVALID_HANDLE );
1236 return;
1239 if ((thread = create_thread( request_fd, current->process )))
1241 if (req->suspend) thread->suspend++;
1242 reply->tid = get_thread_id( thread );
1243 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1245 /* thread object will be released when the thread gets killed */
1246 return;
1248 kill_thread( thread, 1 );
1252 /* initialize a new thread */
1253 DECL_HANDLER(init_thread)
1255 struct process *process = current->process;
1256 int wait_fd, reply_fd;
1258 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1260 set_error( STATUS_TOO_MANY_OPENED_FILES );
1261 return;
1263 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1265 set_error( STATUS_TOO_MANY_OPENED_FILES );
1266 goto error;
1269 if (current->reply_fd) /* already initialised */
1271 set_error( STATUS_INVALID_PARAMETER );
1272 goto error;
1275 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1277 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1278 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1279 if (!current->reply_fd || !current->wait_fd) return;
1281 if (!is_valid_address(req->teb))
1283 set_error( STATUS_INVALID_PARAMETER );
1284 return;
1287 current->unix_pid = req->unix_pid;
1288 current->unix_tid = req->unix_tid;
1289 current->teb = req->teb;
1290 current->entry_point = req->entry;
1292 if (!process->peb) /* first thread, initialize the process too */
1294 if (!is_cpu_supported( req->cpu )) return;
1295 process->unix_pid = current->unix_pid;
1296 process->peb = req->entry;
1297 process->cpu = req->cpu;
1298 reply->info_size = init_process( current );
1299 if (!process->parent)
1300 process->affinity = current->affinity = get_thread_affinity( current );
1301 else
1302 set_thread_affinity( current, current->affinity );
1304 else
1306 if (req->cpu != process->cpu)
1308 set_error( STATUS_INVALID_PARAMETER );
1309 return;
1311 if (process->unix_pid != current->unix_pid)
1312 process->unix_pid = -1; /* can happen with linuxthreads */
1313 stop_thread_if_suspended( current );
1314 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1315 set_thread_affinity( current, current->affinity );
1317 debug_level = max( debug_level, req->debug_level );
1319 reply->pid = get_process_id( process );
1320 reply->tid = get_thread_id( current );
1321 reply->version = SERVER_PROTOCOL_VERSION;
1322 reply->server_start = server_start_time;
1323 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1324 return;
1326 error:
1327 if (reply_fd != -1) close( reply_fd );
1328 if (wait_fd != -1) close( wait_fd );
1331 /* terminate a thread */
1332 DECL_HANDLER(terminate_thread)
1334 struct thread *thread;
1336 reply->self = 0;
1337 reply->last = 0;
1338 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1340 thread->exit_code = req->exit_code;
1341 if (thread != current) kill_thread( thread, 1 );
1342 else
1344 reply->self = 1;
1345 reply->last = (thread->process->running_threads == 1);
1347 release_object( thread );
1351 /* open a handle to a thread */
1352 DECL_HANDLER(open_thread)
1354 struct thread *thread = get_thread_from_id( req->tid );
1356 reply->handle = 0;
1357 if (thread)
1359 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1360 release_object( thread );
1364 /* fetch information about a thread */
1365 DECL_HANDLER(get_thread_info)
1367 struct thread *thread;
1368 obj_handle_t handle = req->handle;
1370 if (!handle) thread = get_thread_from_id( req->tid_in );
1371 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
1373 if (thread)
1375 reply->pid = get_process_id( thread->process );
1376 reply->tid = get_thread_id( thread );
1377 reply->teb = thread->teb;
1378 reply->entry_point = thread->entry_point;
1379 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1380 reply->priority = thread->priority;
1381 reply->affinity = thread->affinity;
1382 reply->last = thread->process->running_threads == 1;
1384 release_object( thread );
1388 /* fetch information about thread times */
1389 DECL_HANDLER(get_thread_times)
1391 struct thread *thread;
1393 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1395 reply->creation_time = thread->creation_time;
1396 reply->exit_time = thread->exit_time;
1398 release_object( thread );
1402 /* set information about a thread */
1403 DECL_HANDLER(set_thread_info)
1405 struct thread *thread;
1407 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1409 set_thread_info( thread, req );
1410 release_object( thread );
1414 /* suspend a thread */
1415 DECL_HANDLER(suspend_thread)
1417 struct thread *thread;
1419 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1421 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1422 else reply->count = suspend_thread( thread );
1423 release_object( thread );
1427 /* resume a thread */
1428 DECL_HANDLER(resume_thread)
1430 struct thread *thread;
1432 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1434 reply->count = resume_thread( thread );
1435 release_object( thread );
1439 /* select on a handle list */
1440 DECL_HANDLER(select)
1442 select_op_t select_op;
1443 data_size_t op_size;
1444 struct thread_apc *apc;
1445 const apc_result_t *result = get_req_data();
1447 if (get_req_data_size() < sizeof(*result))
1449 set_error( STATUS_INVALID_PARAMETER );
1450 return;
1452 if (!req->cookie)
1454 set_error( STATUS_INVALID_PARAMETER );
1455 return;
1458 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1459 memset( &select_op, 0, sizeof(select_op) );
1460 memcpy( &select_op, result + 1, op_size );
1462 /* first store results of previous apc */
1463 if (req->prev_apc)
1465 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1466 0, &thread_apc_ops ))) return;
1467 apc->result = *result;
1468 apc->executed = 1;
1469 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1471 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1472 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1473 close_handle( current->process, apc->result.create_thread.handle );
1474 apc->result.create_thread.handle = handle;
1475 clear_error(); /* ignore errors from the above calls */
1477 else if (apc->result.type == APC_ASYNC_IO)
1479 if (apc->owner)
1480 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total,
1481 apc->result.async_io.apc, apc->result.async_io.arg );
1483 wake_up( &apc->obj, 0 );
1484 close_handle( current->process, req->prev_apc );
1485 release_object( apc );
1488 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1490 if (get_error() == STATUS_USER_APC)
1492 for (;;)
1494 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1495 break;
1496 /* Optimization: ignore APC_NONE calls, they are only used to
1497 * wake up a thread, but since we got here the thread woke up already.
1499 if (apc->call.type != APC_NONE)
1501 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1502 reply->call = apc->call;
1503 release_object( apc );
1504 break;
1506 apc->executed = 1;
1507 wake_up( &apc->obj, 0 );
1508 release_object( apc );
1513 /* queue an APC for a thread or process */
1514 DECL_HANDLER(queue_apc)
1516 struct thread *thread = NULL;
1517 struct process *process = NULL;
1518 struct thread_apc *apc;
1520 if (!(apc = create_apc( NULL, &req->call ))) return;
1522 switch (apc->call.type)
1524 case APC_NONE:
1525 case APC_USER:
1526 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1527 break;
1528 case APC_VIRTUAL_ALLOC:
1529 case APC_VIRTUAL_FREE:
1530 case APC_VIRTUAL_PROTECT:
1531 case APC_VIRTUAL_FLUSH:
1532 case APC_VIRTUAL_LOCK:
1533 case APC_VIRTUAL_UNLOCK:
1534 case APC_UNMAP_VIEW:
1535 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1536 break;
1537 case APC_VIRTUAL_QUERY:
1538 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1539 break;
1540 case APC_MAP_VIEW:
1541 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1542 if (process && process != current->process)
1544 /* duplicate the handle into the target process */
1545 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1546 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1547 if (handle) apc->call.map_view.handle = handle;
1548 else
1550 release_object( process );
1551 process = NULL;
1554 break;
1555 case APC_CREATE_THREAD:
1556 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1557 break;
1558 default:
1559 set_error( STATUS_INVALID_PARAMETER );
1560 break;
1563 if (thread)
1565 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1566 release_object( thread );
1568 else if (process)
1570 reply->self = (process == current->process);
1571 if (!reply->self)
1573 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1574 if (handle)
1576 if (queue_apc( process, NULL, apc ))
1578 apc->caller = (struct thread *)grab_object( current );
1579 reply->handle = handle;
1581 else
1583 close_handle( current->process, handle );
1584 set_error( STATUS_PROCESS_IS_TERMINATING );
1588 release_object( process );
1591 release_object( apc );
1594 /* Get the result of an APC call */
1595 DECL_HANDLER(get_apc_result)
1597 struct thread_apc *apc;
1599 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1600 0, &thread_apc_ops ))) return;
1601 if (!apc->executed) set_error( STATUS_PENDING );
1602 else
1604 reply->result = apc->result;
1605 /* close the handle directly to avoid an extra round-trip */
1606 close_handle( current->process, req->handle );
1608 release_object( apc );
1611 /* retrieve the current context of a thread */
1612 DECL_HANDLER(get_thread_context)
1614 struct thread *thread;
1615 context_t *context;
1617 if (get_reply_max_size() < sizeof(context_t))
1619 set_error( STATUS_INVALID_PARAMETER );
1620 return;
1622 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1623 reply->self = (thread == current);
1625 if (thread != current && !thread->context)
1627 /* thread is not suspended, retry (if it's still running) */
1628 if (thread->state == RUNNING)
1630 set_error( STATUS_PENDING );
1631 if (req->suspend)
1633 release_object( thread );
1634 /* make sure we have suspend access */
1635 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1636 suspend_thread( thread );
1639 else set_error( STATUS_UNSUCCESSFUL );
1641 else if ((context = set_reply_data_size( sizeof(context_t) )))
1643 unsigned int flags = get_context_system_regs( thread->process->cpu );
1645 memset( context, 0, sizeof(context_t) );
1646 context->cpu = thread->process->cpu;
1647 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1648 if (flags) get_thread_context( thread, context, flags );
1650 release_object( thread );
1653 /* set the current context of a thread */
1654 DECL_HANDLER(set_thread_context)
1656 struct thread *thread;
1657 const context_t *context = get_req_data();
1659 if (get_req_data_size() < sizeof(context_t))
1661 set_error( STATUS_INVALID_PARAMETER );
1662 return;
1664 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1665 reply->self = (thread == current);
1667 if (thread != current && !thread->context)
1669 /* thread is not suspended, retry (if it's still running) */
1670 if (thread->state == RUNNING)
1672 set_error( STATUS_PENDING );
1673 if (req->suspend)
1675 release_object( thread );
1676 /* make sure we have suspend access */
1677 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1678 suspend_thread( thread );
1681 else set_error( STATUS_UNSUCCESSFUL );
1683 else if (context->cpu == thread->process->cpu)
1685 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1686 unsigned int client_flags = context->flags & ~system_flags;
1688 if (system_flags) set_thread_context( thread, context, system_flags );
1689 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1691 else set_error( STATUS_INVALID_PARAMETER );
1693 release_object( thread );
1696 /* retrieve the suspended context of a thread */
1697 DECL_HANDLER(get_suspend_context)
1699 if (get_reply_max_size() < sizeof(context_t))
1701 set_error( STATUS_INVALID_PARAMETER );
1702 return;
1705 if (current->suspend_context)
1707 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1708 if (current->context == current->suspend_context)
1710 current->context = NULL;
1711 stop_thread_if_suspended( current );
1713 current->suspend_context = NULL;
1715 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1718 /* store the suspended context of a thread */
1719 DECL_HANDLER(set_suspend_context)
1721 const context_t *context = get_req_data();
1723 if (get_req_data_size() < sizeof(context_t))
1725 set_error( STATUS_INVALID_PARAMETER );
1726 return;
1729 if (current->context || context->cpu != current->process->cpu)
1731 /* nested suspend or exception, shouldn't happen */
1732 set_error( STATUS_INVALID_PARAMETER );
1734 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1736 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1737 current->context = current->suspend_context;
1738 if (current->debug_break) break_thread( current );
1742 /* fetch a selector entry for a thread */
1743 DECL_HANDLER(get_selector_entry)
1745 struct thread *thread;
1746 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1748 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1749 release_object( thread );