winhttp/tests: Add an authentication test using IWinHttpRequest.
[wine.git] / server / thread.c
blobbad22311b56ca70bcc8f53bf6ea7a40ab6011504
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;
342 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
343 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
344 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
345 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
347 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
348 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
350 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
353 static void dump_thread_apc( struct object *obj, int verbose )
355 struct thread_apc *apc = (struct thread_apc *)obj;
356 assert( obj->ops == &thread_apc_ops );
358 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
361 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
363 struct thread_apc *apc = (struct thread_apc *)obj;
364 return apc->executed;
367 static void thread_apc_destroy( struct object *obj )
369 struct thread_apc *apc = (struct thread_apc *)obj;
370 if (apc->caller) release_object( apc->caller );
371 if (apc->owner) release_object( apc->owner );
374 /* queue an async procedure call */
375 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
377 struct thread_apc *apc;
379 if ((apc = alloc_object( &thread_apc_ops )))
381 apc->call = *call_data;
382 apc->caller = NULL;
383 apc->owner = owner;
384 apc->executed = 0;
385 apc->result.type = APC_NONE;
386 if (owner) grab_object( owner );
388 return apc;
391 /* get a thread pointer from a thread id (and increment the refcount) */
392 struct thread *get_thread_from_id( thread_id_t id )
394 struct object *obj = get_ptid_entry( id );
396 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
397 set_error( STATUS_INVALID_CID );
398 return NULL;
401 /* get a thread from a handle (and increment the refcount) */
402 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
404 return (struct thread *)get_handle_obj( current->process, handle,
405 access, &thread_ops );
408 /* find a thread from a Unix tid */
409 struct thread *get_thread_from_tid( int tid )
411 struct thread *thread;
413 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
415 if (thread->unix_tid == tid) return thread;
417 return NULL;
420 /* find a thread from a Unix pid */
421 struct thread *get_thread_from_pid( int pid )
423 struct thread *thread;
425 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
427 if (thread->unix_pid == pid) return thread;
429 return NULL;
432 int set_thread_affinity( struct thread *thread, affinity_t affinity )
434 int ret = 0;
435 #ifdef HAVE_SCHED_SETAFFINITY
436 if (thread->unix_tid != -1)
438 cpu_set_t set;
439 int i;
440 affinity_t mask;
442 CPU_ZERO( &set );
443 for (i = 0, mask = 1; mask; i++, mask <<= 1)
444 if (affinity & mask) CPU_SET( i, &set );
446 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
448 #endif
449 if (!ret) thread->affinity = affinity;
450 return ret;
453 affinity_t get_thread_affinity( struct thread *thread )
455 affinity_t mask = 0;
456 #ifdef HAVE_SCHED_SETAFFINITY
457 if (thread->unix_tid != -1)
459 cpu_set_t set;
460 unsigned int i;
462 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
463 for (i = 0; i < 8 * sizeof(mask); i++)
464 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
466 #endif
467 if (!mask) mask = ~(affinity_t)0;
468 return mask;
471 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
472 #define THREAD_PRIORITY_REALTIME_LOWEST -7
474 /* set all information about a thread */
475 static void set_thread_info( struct thread *thread,
476 const struct set_thread_info_request *req )
478 if (req->mask & SET_THREAD_INFO_PRIORITY)
480 int max = THREAD_PRIORITY_HIGHEST;
481 int min = THREAD_PRIORITY_LOWEST;
482 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
484 max = THREAD_PRIORITY_REALTIME_HIGHEST;
485 min = THREAD_PRIORITY_REALTIME_LOWEST;
487 if ((req->priority >= min && req->priority <= max) ||
488 req->priority == THREAD_PRIORITY_IDLE ||
489 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
490 thread->priority = req->priority;
491 else
492 set_error( STATUS_INVALID_PARAMETER );
494 if (req->mask & SET_THREAD_INFO_AFFINITY)
496 if ((req->affinity & thread->process->affinity) != req->affinity)
497 set_error( STATUS_INVALID_PARAMETER );
498 else if (thread->state == TERMINATED)
499 set_error( STATUS_THREAD_IS_TERMINATING );
500 else if (set_thread_affinity( thread, req->affinity ))
501 file_set_error();
503 if (req->mask & SET_THREAD_INFO_TOKEN)
504 security_set_thread_token( thread, req->token );
505 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
506 thread->entry_point = req->entry_point;
509 /* stop a thread (at the Unix level) */
510 void stop_thread( struct thread *thread )
512 if (thread->context) return; /* already inside a debug event, no need for a signal */
513 /* can't stop a thread while initialisation is in progress */
514 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
517 /* stop a thread if it's supposed to be suspended */
518 void stop_thread_if_suspended( struct thread *thread )
520 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
523 /* suspend a thread */
524 static int suspend_thread( struct thread *thread )
526 int old_count = thread->suspend;
527 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
529 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
531 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
532 return old_count;
535 /* resume a thread */
536 static int resume_thread( struct thread *thread )
538 int old_count = thread->suspend;
539 if (thread->suspend > 0)
541 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
543 return old_count;
546 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
547 int add_queue( struct object *obj, struct wait_queue_entry *entry )
549 grab_object( obj );
550 entry->obj = obj;
551 list_add_tail( &obj->wait_queue, &entry->entry );
552 return 1;
555 /* remove a thread from an object wait queue */
556 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
558 list_remove( &entry->entry );
559 release_object( obj );
562 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
564 return entry->wait->thread;
567 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
569 return entry->wait->select;
572 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
574 return entry->wait->key;
577 void make_wait_abandoned( struct wait_queue_entry *entry )
579 entry->wait->abandoned = 1;
582 /* finish waiting */
583 static void end_wait( struct thread *thread )
585 struct thread_wait *wait = thread->wait;
586 struct wait_queue_entry *entry;
587 int i;
589 assert( wait );
590 thread->wait = wait->next;
591 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
592 entry->obj->ops->remove_queue( entry->obj, entry );
593 if (wait->user) remove_timeout_user( wait->user );
594 free( wait );
597 /* build the thread wait structure */
598 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
599 int flags, timeout_t timeout )
601 struct thread_wait *wait;
602 struct wait_queue_entry *entry;
603 unsigned int i;
605 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
606 wait->next = current->wait;
607 wait->thread = current;
608 wait->count = count;
609 wait->flags = flags;
610 wait->select = select_op->op;
611 wait->cookie = 0;
612 wait->user = NULL;
613 wait->timeout = timeout;
614 wait->abandoned = 0;
615 current->wait = wait;
617 for (i = 0, entry = wait->queues; i < count; i++, entry++)
619 struct object *obj = objects[i];
620 entry->wait = wait;
621 if (!obj->ops->add_queue( obj, entry ))
623 wait->count = i;
624 end_wait( current );
625 return 0;
628 return 1;
631 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
632 int flags, timeout_t timeout )
634 struct object *objects[MAXIMUM_WAIT_OBJECTS];
635 unsigned int i;
636 int ret = 0;
638 assert( count <= MAXIMUM_WAIT_OBJECTS );
640 for (i = 0; i < count; i++)
641 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
642 break;
644 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
646 while (i > 0) release_object( objects[--i] );
647 return ret;
650 /* check if the thread waiting condition is satisfied */
651 static int check_wait( struct thread *thread )
653 int i;
654 struct thread_wait *wait = thread->wait;
655 struct wait_queue_entry *entry;
657 assert( wait );
659 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
660 return STATUS_USER_APC;
662 /* Suspended threads may not acquire locks, but they can run system APCs */
663 if (thread->process->suspend + thread->suspend > 0) return -1;
665 if (wait->select == SELECT_WAIT_ALL)
667 int not_ok = 0;
668 /* Note: we must check them all anyway, as some objects may
669 * want to do something when signaled, even if others are not */
670 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
671 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
672 if (not_ok) goto other_checks;
673 /* Wait satisfied: tell it to all objects */
674 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
675 entry->obj->ops->satisfied( entry->obj, entry );
676 return wait->abandoned ? STATUS_ABANDONED_WAIT_0 : STATUS_WAIT_0;
678 else
680 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
682 if (!entry->obj->ops->signaled( entry->obj, entry )) continue;
683 /* Wait satisfied: tell it to the object */
684 entry->obj->ops->satisfied( entry->obj, entry );
685 if (wait->abandoned) i += STATUS_ABANDONED_WAIT_0;
686 return i;
690 other_checks:
691 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
692 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
693 return -1;
696 /* send the wakeup signal to a thread */
697 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
699 struct wake_up_reply reply;
700 int ret;
702 memset( &reply, 0, sizeof(reply) );
703 reply.cookie = cookie;
704 reply.signaled = signaled;
705 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
706 return 0;
707 if (ret >= 0)
708 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
709 else if (errno == EPIPE)
710 kill_thread( thread, 0 ); /* normal death */
711 else
712 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
713 return -1;
716 /* attempt to wake up a thread */
717 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
718 int wake_thread( struct thread *thread )
720 int signaled, count;
721 client_ptr_t cookie;
723 for (count = 0; thread->wait; count++)
725 if ((signaled = check_wait( thread )) == -1) break;
727 cookie = thread->wait->cookie;
728 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
729 end_wait( thread );
730 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
732 if (!count) count = -1;
733 break;
736 return count;
739 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
740 int wake_thread_queue_entry( struct wait_queue_entry *entry )
742 struct thread_wait *wait = entry->wait;
743 struct thread *thread = wait->thread;
744 int signaled;
745 client_ptr_t cookie;
747 if (thread->wait != wait) return 0; /* not the current wait */
748 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
750 assert( wait->select != SELECT_WAIT_ALL );
752 signaled = entry - wait->queues;
753 entry->obj->ops->satisfied( entry->obj, entry );
754 if (wait->abandoned) signaled += STATUS_ABANDONED_WAIT_0;
756 cookie = wait->cookie;
757 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
758 end_wait( thread );
760 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
761 wake_thread( thread ); /* check other waits too */
763 return 1;
766 /* thread wait timeout */
767 static void thread_timeout( void *ptr )
769 struct thread_wait *wait = ptr;
770 struct thread *thread = wait->thread;
771 client_ptr_t cookie = wait->cookie;
773 wait->user = NULL;
774 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
775 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
777 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
778 end_wait( thread );
780 assert( cookie );
781 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
782 /* check if other objects have become signaled in the meantime */
783 wake_thread( thread );
786 /* try signaling an event flag, a semaphore or a mutex */
787 static int signal_object( obj_handle_t handle )
789 struct object *obj;
790 int ret = 0;
792 obj = get_handle_obj( current->process, handle, 0, NULL );
793 if (obj)
795 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
796 release_object( obj );
798 return ret;
801 /* select on a list of handles */
802 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
803 int flags, timeout_t timeout )
805 int ret;
806 unsigned int count;
807 struct object *object;
809 if (timeout <= 0) timeout = current_time - timeout;
811 switch (select_op->op)
813 case SELECT_NONE:
814 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
815 break;
817 case SELECT_WAIT:
818 case SELECT_WAIT_ALL:
819 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
820 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
822 set_error( STATUS_INVALID_PARAMETER );
823 return 0;
825 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
826 return timeout;
827 break;
829 case SELECT_SIGNAL_AND_WAIT:
830 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
831 return timeout;
832 if (select_op->signal_and_wait.signal)
834 if (!signal_object( select_op->signal_and_wait.signal ))
836 end_wait( current );
837 return timeout;
839 /* check if we woke ourselves up */
840 if (!current->wait) return timeout;
842 break;
844 case SELECT_KEYED_EVENT_WAIT:
845 case SELECT_KEYED_EVENT_RELEASE:
846 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
847 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
848 if (!object) return timeout;
849 ret = wait_on( select_op, 1, &object, flags, timeout );
850 release_object( object );
851 if (!ret) return timeout;
852 current->wait->key = select_op->keyed_event.key;
853 break;
855 default:
856 set_error( STATUS_INVALID_PARAMETER );
857 return 0;
860 if ((ret = check_wait( current )) != -1)
862 /* condition is already satisfied */
863 end_wait( current );
864 set_error( ret );
865 return timeout;
868 /* now we need to wait */
869 if (current->wait->timeout != TIMEOUT_INFINITE)
871 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
872 thread_timeout, current->wait )))
874 end_wait( current );
875 return timeout;
878 current->wait->cookie = cookie;
879 set_error( STATUS_PENDING );
880 return timeout;
883 /* attempt to wake threads sleeping on the object wait queue */
884 void wake_up( struct object *obj, int max )
886 struct list *ptr;
887 int ret;
889 LIST_FOR_EACH( ptr, &obj->wait_queue )
891 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
892 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
893 if (ret > 0 && max && !--max) break;
894 /* restart at the head of the list since a wake up can change the object wait queue */
895 ptr = &obj->wait_queue;
899 /* return the apc queue to use for a given apc type */
900 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
902 switch(type)
904 case APC_NONE:
905 case APC_USER:
906 case APC_TIMER:
907 return &thread->user_apc;
908 default:
909 return &thread->system_apc;
913 /* check if thread is currently waiting for a (system) apc */
914 static inline int is_in_apc_wait( struct thread *thread )
916 return (thread->process->suspend || thread->suspend ||
917 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
920 /* queue an existing APC to a given thread */
921 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
923 struct list *queue;
925 if (!thread) /* find a suitable thread inside the process */
927 struct thread *candidate;
929 /* first try to find a waiting thread */
930 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
932 if (candidate->state == TERMINATED) continue;
933 if (is_in_apc_wait( candidate ))
935 thread = candidate;
936 break;
939 if (!thread)
941 /* then use the first one that accepts a signal */
942 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
944 if (send_thread_signal( candidate, SIGUSR1 ))
946 thread = candidate;
947 break;
951 if (!thread) return 0; /* nothing found */
952 queue = get_apc_queue( thread, apc->call.type );
954 else
956 if (thread->state == TERMINATED) return 0;
957 queue = get_apc_queue( thread, apc->call.type );
958 /* send signal for system APCs if needed */
959 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
961 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
963 /* cancel a possible previous APC with the same owner */
964 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
967 grab_object( apc );
968 list_add_tail( queue, &apc->entry );
969 if (!list_prev( queue, &apc->entry )) /* first one */
970 wake_thread( thread );
972 return 1;
975 /* queue an async procedure call */
976 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
978 struct thread_apc *apc;
979 int ret = 0;
981 if ((apc = create_apc( owner, call_data )))
983 ret = queue_apc( NULL, thread, apc );
984 release_object( apc );
986 return ret;
989 /* cancel the async procedure call owned by a specific object */
990 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
992 struct thread_apc *apc;
993 struct list *queue = get_apc_queue( thread, type );
995 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
997 if (apc->owner != owner) continue;
998 list_remove( &apc->entry );
999 apc->executed = 1;
1000 wake_up( &apc->obj, 0 );
1001 release_object( apc );
1002 return;
1006 /* remove the head apc from the queue; the returned object must be released by the caller */
1007 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1009 struct thread_apc *apc = NULL;
1010 struct list *ptr = list_head( &thread->system_apc );
1012 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1013 if (ptr)
1015 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1016 list_remove( ptr );
1018 return apc;
1021 /* clear an APC queue, cancelling all the APCs on it */
1022 static void clear_apc_queue( struct list *queue )
1024 struct list *ptr;
1026 while ((ptr = list_head( queue )))
1028 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1029 list_remove( &apc->entry );
1030 apc->executed = 1;
1031 wake_up( &apc->obj, 0 );
1032 release_object( apc );
1036 /* add an fd to the inflight list */
1037 /* return list index, or -1 on error */
1038 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1040 int i;
1042 if (server == -1) return -1;
1043 if (client == -1)
1045 close( server );
1046 return -1;
1049 /* first check if we already have an entry for this fd */
1050 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1051 if (thread->inflight[i].client == client)
1053 close( thread->inflight[i].server );
1054 thread->inflight[i].server = server;
1055 return i;
1058 /* now find a free spot to store it */
1059 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1060 if (thread->inflight[i].client == -1)
1062 thread->inflight[i].client = client;
1063 thread->inflight[i].server = server;
1064 return i;
1067 close( server );
1068 return -1;
1071 /* get an inflight fd and purge it from the list */
1072 /* the fd must be closed when no longer used */
1073 int thread_get_inflight_fd( struct thread *thread, int client )
1075 int i, ret;
1077 if (client == -1) return -1;
1081 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1083 if (thread->inflight[i].client == client)
1085 ret = thread->inflight[i].server;
1086 thread->inflight[i].server = thread->inflight[i].client = -1;
1087 return ret;
1090 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1091 return -1;
1094 /* kill a thread on the spot */
1095 void kill_thread( struct thread *thread, int violent_death )
1097 if (thread->state == TERMINATED) return; /* already killed */
1098 thread->state = TERMINATED;
1099 thread->exit_time = current_time;
1100 if (current == thread) current = NULL;
1101 if (debug_level)
1102 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1103 thread->id, thread->exit_code );
1104 if (thread->wait)
1106 while (thread->wait) end_wait( thread );
1107 send_thread_wakeup( thread, 0, thread->exit_code );
1108 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1109 violent_death = 0;
1111 kill_console_processes( thread, 0 );
1112 debug_exit_thread( thread );
1113 abandon_mutexes( thread );
1114 wake_up( &thread->obj, 0 );
1115 if (violent_death) send_thread_signal( thread, SIGQUIT );
1116 cleanup_thread( thread );
1117 remove_process_thread( thread->process, thread );
1118 release_object( thread );
1121 /* copy parts of a context structure */
1122 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1124 assert( to->cpu == from->cpu );
1125 to->flags |= flags;
1126 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1127 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1128 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1129 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1130 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1131 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1134 /* return the context flags that correspond to system regs */
1135 /* (system regs are the ones we can't access on the client side) */
1136 static unsigned int get_context_system_regs( enum cpu_type cpu )
1138 switch (cpu)
1140 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1141 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1142 case CPU_POWERPC: return 0;
1143 case CPU_ARM: return 0;
1144 case CPU_ARM64: return 0;
1146 return 0;
1149 /* trigger a breakpoint event in a given thread */
1150 void break_thread( struct thread *thread )
1152 debug_event_t data;
1154 assert( thread->context );
1156 memset( &data, 0, sizeof(data) );
1157 data.exception.first = 1;
1158 data.exception.exc_code = STATUS_BREAKPOINT;
1159 data.exception.flags = EXCEPTION_CONTINUABLE;
1160 switch (thread->context->cpu)
1162 case CPU_x86:
1163 data.exception.address = thread->context->ctl.i386_regs.eip;
1164 break;
1165 case CPU_x86_64:
1166 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1167 break;
1168 case CPU_POWERPC:
1169 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1170 break;
1171 case CPU_ARM:
1172 data.exception.address = thread->context->ctl.arm_regs.pc;
1173 break;
1174 case CPU_ARM64:
1175 data.exception.address = thread->context->ctl.arm64_regs.pc;
1176 break;
1178 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1179 thread->debug_break = 0;
1182 /* take a snapshot of currently running threads */
1183 struct thread_snapshot *thread_snap( int *count )
1185 struct thread_snapshot *snapshot, *ptr;
1186 struct thread *thread;
1187 int total = 0;
1189 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1190 if (thread->state != TERMINATED) total++;
1191 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1192 ptr = snapshot;
1193 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1195 if (thread->state == TERMINATED) continue;
1196 ptr->thread = thread;
1197 ptr->count = thread->obj.refcount;
1198 ptr->priority = thread->priority;
1199 grab_object( thread );
1200 ptr++;
1202 *count = total;
1203 return snapshot;
1206 /* gets the current impersonation token */
1207 struct token *thread_get_impersonation_token( struct thread *thread )
1209 if (thread->token)
1210 return thread->token;
1211 else
1212 return thread->process->token;
1215 /* check if a cpu type can be supported on this server */
1216 int is_cpu_supported( enum cpu_type cpu )
1218 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1220 if (CPU_FLAG(cpu) && (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu))) return 1;
1221 if (!(supported_cpus & prefix_cpu_mask))
1222 set_error( STATUS_NOT_SUPPORTED );
1223 else if (supported_cpus & CPU_FLAG(cpu))
1224 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1225 else
1226 set_error( STATUS_INVALID_IMAGE_FORMAT );
1227 return 0;
1230 /* create a new thread */
1231 DECL_HANDLER(new_thread)
1233 struct thread *thread;
1234 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1236 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1238 if (request_fd != -1) close( request_fd );
1239 set_error( STATUS_INVALID_HANDLE );
1240 return;
1243 if ((thread = create_thread( request_fd, current->process )))
1245 if (req->suspend) thread->suspend++;
1246 reply->tid = get_thread_id( thread );
1247 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1249 /* thread object will be released when the thread gets killed */
1250 return;
1252 kill_thread( thread, 1 );
1256 /* initialize a new thread */
1257 DECL_HANDLER(init_thread)
1259 struct process *process = current->process;
1260 int wait_fd, reply_fd;
1262 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1264 set_error( STATUS_TOO_MANY_OPENED_FILES );
1265 return;
1267 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1269 set_error( STATUS_TOO_MANY_OPENED_FILES );
1270 goto error;
1273 if (current->reply_fd) /* already initialised */
1275 set_error( STATUS_INVALID_PARAMETER );
1276 goto error;
1279 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1281 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1282 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1283 if (!current->reply_fd || !current->wait_fd) return;
1285 if (!is_valid_address(req->teb))
1287 set_error( STATUS_INVALID_PARAMETER );
1288 return;
1291 current->unix_pid = req->unix_pid;
1292 current->unix_tid = req->unix_tid;
1293 current->teb = req->teb;
1294 current->entry_point = process->peb ? req->entry : 0;
1296 if (!process->peb) /* first thread, initialize the process too */
1298 if (!is_cpu_supported( req->cpu )) return;
1299 process->unix_pid = current->unix_pid;
1300 process->peb = req->entry;
1301 process->cpu = req->cpu;
1302 reply->info_size = init_process( current );
1303 if (!process->parent)
1304 process->affinity = current->affinity = get_thread_affinity( current );
1305 else
1306 set_thread_affinity( current, current->affinity );
1308 else
1310 if (req->cpu != process->cpu)
1312 set_error( STATUS_INVALID_PARAMETER );
1313 return;
1315 if (process->unix_pid != current->unix_pid)
1316 process->unix_pid = -1; /* can happen with linuxthreads */
1317 stop_thread_if_suspended( current );
1318 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1319 set_thread_affinity( current, current->affinity );
1321 debug_level = max( debug_level, req->debug_level );
1323 reply->pid = get_process_id( process );
1324 reply->tid = get_thread_id( current );
1325 reply->version = SERVER_PROTOCOL_VERSION;
1326 reply->server_start = server_start_time;
1327 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1328 return;
1330 error:
1331 if (reply_fd != -1) close( reply_fd );
1332 if (wait_fd != -1) close( wait_fd );
1335 /* terminate a thread */
1336 DECL_HANDLER(terminate_thread)
1338 struct thread *thread;
1340 reply->self = 0;
1341 reply->last = 0;
1342 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1344 thread->exit_code = req->exit_code;
1345 if (thread != current) kill_thread( thread, 1 );
1346 else
1348 reply->self = 1;
1349 reply->last = (thread->process->running_threads == 1);
1351 release_object( thread );
1355 /* open a handle to a thread */
1356 DECL_HANDLER(open_thread)
1358 struct thread *thread = get_thread_from_id( req->tid );
1360 reply->handle = 0;
1361 if (thread)
1363 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1364 release_object( thread );
1368 /* fetch information about a thread */
1369 DECL_HANDLER(get_thread_info)
1371 struct thread *thread;
1372 obj_handle_t handle = req->handle;
1374 if (!handle) thread = get_thread_from_id( req->tid_in );
1375 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1377 if (thread)
1379 reply->pid = get_process_id( thread->process );
1380 reply->tid = get_thread_id( thread );
1381 reply->teb = thread->teb;
1382 reply->entry_point = thread->entry_point;
1383 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1384 reply->priority = thread->priority;
1385 reply->affinity = thread->affinity;
1386 reply->last = thread->process->running_threads == 1;
1388 release_object( thread );
1392 /* fetch information about thread times */
1393 DECL_HANDLER(get_thread_times)
1395 struct thread *thread;
1397 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1399 reply->creation_time = thread->creation_time;
1400 reply->exit_time = thread->exit_time;
1402 release_object( thread );
1406 /* set information about a thread */
1407 DECL_HANDLER(set_thread_info)
1409 struct thread *thread;
1411 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1413 set_thread_info( thread, req );
1414 release_object( thread );
1418 /* suspend a thread */
1419 DECL_HANDLER(suspend_thread)
1421 struct thread *thread;
1423 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1425 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1426 else reply->count = suspend_thread( thread );
1427 release_object( thread );
1431 /* resume a thread */
1432 DECL_HANDLER(resume_thread)
1434 struct thread *thread;
1436 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1438 reply->count = resume_thread( thread );
1439 release_object( thread );
1443 /* select on a handle list */
1444 DECL_HANDLER(select)
1446 select_op_t select_op;
1447 data_size_t op_size;
1448 struct thread_apc *apc;
1449 const apc_result_t *result = get_req_data();
1451 if (get_req_data_size() < sizeof(*result))
1453 set_error( STATUS_INVALID_PARAMETER );
1454 return;
1456 if (!req->cookie)
1458 set_error( STATUS_INVALID_PARAMETER );
1459 return;
1462 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1463 memset( &select_op, 0, sizeof(select_op) );
1464 memcpy( &select_op, result + 1, op_size );
1466 /* first store results of previous apc */
1467 if (req->prev_apc)
1469 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1470 0, &thread_apc_ops ))) return;
1471 apc->result = *result;
1472 apc->executed = 1;
1473 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1475 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1476 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1477 close_handle( current->process, apc->result.create_thread.handle );
1478 apc->result.create_thread.handle = handle;
1479 clear_error(); /* ignore errors from the above calls */
1481 else if (apc->result.type == APC_ASYNC_IO)
1483 if (apc->owner)
1484 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total,
1485 apc->result.async_io.apc, apc->result.async_io.arg );
1487 wake_up( &apc->obj, 0 );
1488 close_handle( current->process, req->prev_apc );
1489 release_object( apc );
1492 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1494 if (get_error() == STATUS_USER_APC)
1496 for (;;)
1498 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1499 break;
1500 /* Optimization: ignore APC_NONE calls, they are only used to
1501 * wake up a thread, but since we got here the thread woke up already.
1503 if (apc->call.type != APC_NONE &&
1504 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1506 reply->call = apc->call;
1507 release_object( apc );
1508 break;
1510 apc->executed = 1;
1511 wake_up( &apc->obj, 0 );
1512 release_object( apc );
1517 /* queue an APC for a thread or process */
1518 DECL_HANDLER(queue_apc)
1520 struct thread *thread = NULL;
1521 struct process *process = NULL;
1522 struct thread_apc *apc;
1524 if (!(apc = create_apc( NULL, &req->call ))) return;
1526 switch (apc->call.type)
1528 case APC_NONE:
1529 case APC_USER:
1530 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1531 break;
1532 case APC_VIRTUAL_ALLOC:
1533 case APC_VIRTUAL_FREE:
1534 case APC_VIRTUAL_PROTECT:
1535 case APC_VIRTUAL_FLUSH:
1536 case APC_VIRTUAL_LOCK:
1537 case APC_VIRTUAL_UNLOCK:
1538 case APC_UNMAP_VIEW:
1539 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1540 break;
1541 case APC_VIRTUAL_QUERY:
1542 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1543 break;
1544 case APC_MAP_VIEW:
1545 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1546 if (process && process != current->process)
1548 /* duplicate the handle into the target process */
1549 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1550 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1551 if (handle) apc->call.map_view.handle = handle;
1552 else
1554 release_object( process );
1555 process = NULL;
1558 break;
1559 case APC_CREATE_THREAD:
1560 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1561 break;
1562 default:
1563 set_error( STATUS_INVALID_PARAMETER );
1564 break;
1567 if (thread)
1569 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1570 release_object( thread );
1572 else if (process)
1574 reply->self = (process == current->process);
1575 if (!reply->self)
1577 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1578 if (handle)
1580 if (queue_apc( process, NULL, apc ))
1582 apc->caller = (struct thread *)grab_object( current );
1583 reply->handle = handle;
1585 else
1587 close_handle( current->process, handle );
1588 set_error( STATUS_PROCESS_IS_TERMINATING );
1592 release_object( process );
1595 release_object( apc );
1598 /* Get the result of an APC call */
1599 DECL_HANDLER(get_apc_result)
1601 struct thread_apc *apc;
1603 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1604 0, &thread_apc_ops ))) return;
1606 if (apc->executed) reply->result = apc->result;
1607 else set_error( STATUS_PENDING );
1609 /* close the handle directly to avoid an extra round-trip */
1610 close_handle( current->process, req->handle );
1611 release_object( apc );
1614 /* retrieve the current context of a thread */
1615 DECL_HANDLER(get_thread_context)
1617 struct thread *thread;
1618 context_t *context;
1620 if (get_reply_max_size() < sizeof(context_t))
1622 set_error( STATUS_INVALID_PARAMETER );
1623 return;
1625 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1626 reply->self = (thread == current);
1628 if (thread != current && !thread->context)
1630 /* thread is not suspended, retry (if it's still running) */
1631 if (thread->state == RUNNING)
1633 set_error( STATUS_PENDING );
1634 if (req->suspend)
1636 release_object( thread );
1637 /* make sure we have suspend access */
1638 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1639 suspend_thread( thread );
1642 else set_error( STATUS_UNSUCCESSFUL );
1644 else if ((context = set_reply_data_size( sizeof(context_t) )))
1646 unsigned int flags = get_context_system_regs( thread->process->cpu );
1648 memset( context, 0, sizeof(context_t) );
1649 context->cpu = thread->process->cpu;
1650 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1651 if (flags) get_thread_context( thread, context, flags );
1653 release_object( thread );
1656 /* set the current context of a thread */
1657 DECL_HANDLER(set_thread_context)
1659 struct thread *thread;
1660 const context_t *context = get_req_data();
1662 if (get_req_data_size() < sizeof(context_t))
1664 set_error( STATUS_INVALID_PARAMETER );
1665 return;
1667 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1668 reply->self = (thread == current);
1670 if (thread != current && !thread->context)
1672 /* thread is not suspended, retry (if it's still running) */
1673 if (thread->state == RUNNING)
1675 set_error( STATUS_PENDING );
1676 if (req->suspend)
1678 release_object( thread );
1679 /* make sure we have suspend access */
1680 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1681 suspend_thread( thread );
1684 else set_error( STATUS_UNSUCCESSFUL );
1686 else if (context->cpu == thread->process->cpu)
1688 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1689 unsigned int client_flags = context->flags & ~system_flags;
1691 if (system_flags) set_thread_context( thread, context, system_flags );
1692 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1694 else set_error( STATUS_INVALID_PARAMETER );
1696 release_object( thread );
1699 /* retrieve the suspended context of a thread */
1700 DECL_HANDLER(get_suspend_context)
1702 if (get_reply_max_size() < sizeof(context_t))
1704 set_error( STATUS_INVALID_PARAMETER );
1705 return;
1708 if (current->suspend_context)
1710 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1711 if (current->context == current->suspend_context)
1713 current->context = NULL;
1714 stop_thread_if_suspended( current );
1716 current->suspend_context = NULL;
1718 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1721 /* store the suspended context of a thread */
1722 DECL_HANDLER(set_suspend_context)
1724 const context_t *context = get_req_data();
1726 if (get_req_data_size() < sizeof(context_t))
1728 set_error( STATUS_INVALID_PARAMETER );
1729 return;
1732 if (current->context || context->cpu != current->process->cpu)
1734 /* nested suspend or exception, shouldn't happen */
1735 set_error( STATUS_INVALID_PARAMETER );
1737 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1739 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1740 current->context = current->suspend_context;
1741 if (current->debug_break) break_thread( current );
1745 /* fetch a selector entry for a thread */
1746 DECL_HANDLER(get_selector_entry)
1748 struct thread *thread;
1749 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1751 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1752 release_object( thread );