wordpad: New high resolution cursor generated from SVG.
[wine.git] / server / thread.c
blob70f5f28739ef564c87af5257302a34f1744d7696
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SCHED_H
39 #include <sched.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
47 #include "file.h"
48 #include "handle.h"
49 #include "process.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "user.h"
53 #include "security.h"
56 #ifdef __i386__
57 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
60 #elif defined(__powerpc__)
61 static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
66 #else
67 #error Unsupported CPU
68 #endif
70 /* thread queues */
72 struct thread_wait
74 struct thread_wait *next; /* next wait structure for this thread */
75 struct thread *thread; /* owner thread */
76 int count; /* count of objects */
77 int flags;
78 int abandoned;
79 enum select_op select;
80 client_ptr_t key; /* wait key for keyed events */
81 client_ptr_t cookie; /* magic cookie to return to client */
82 timeout_t timeout;
83 struct timeout_user *user;
84 struct wait_queue_entry queues[1];
87 /* asynchronous procedure calls */
89 struct thread_apc
91 struct object obj; /* object header */
92 struct list entry; /* queue linked list */
93 struct thread *caller; /* thread that queued this apc */
94 struct object *owner; /* object that queued this apc */
95 int executed; /* has it been executed by the client? */
96 apc_call_t call; /* call arguments */
97 apc_result_t result; /* call results once executed */
100 static void dump_thread_apc( struct object *obj, int verbose );
101 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
102 static void thread_apc_destroy( struct object *obj );
103 static void clear_apc_queue( struct list *queue );
105 static const struct object_ops thread_apc_ops =
107 sizeof(struct thread_apc), /* size */
108 dump_thread_apc, /* dump */
109 no_get_type, /* get_type */
110 add_queue, /* add_queue */
111 remove_queue, /* remove_queue */
112 thread_apc_signaled, /* signaled */
113 no_satisfied, /* satisfied */
114 no_signal, /* signal */
115 no_get_fd, /* get_fd */
116 no_map_access, /* map_access */
117 default_get_sd, /* get_sd */
118 default_set_sd, /* set_sd */
119 no_lookup_name, /* lookup_name */
120 no_link_name, /* link_name */
121 NULL, /* unlink_name */
122 no_open_file, /* open_file */
123 no_close_handle, /* close_handle */
124 thread_apc_destroy /* destroy */
128 /* thread operations */
130 static void dump_thread( struct object *obj, int verbose );
131 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
132 static unsigned int thread_map_access( struct object *obj, unsigned int access );
133 static void thread_poll_event( struct fd *fd, int event );
134 static void destroy_thread( struct object *obj );
136 static const struct object_ops thread_ops =
138 sizeof(struct thread), /* size */
139 dump_thread, /* dump */
140 no_get_type, /* get_type */
141 add_queue, /* add_queue */
142 remove_queue, /* remove_queue */
143 thread_signaled, /* signaled */
144 no_satisfied, /* satisfied */
145 no_signal, /* signal */
146 no_get_fd, /* get_fd */
147 thread_map_access, /* map_access */
148 default_get_sd, /* get_sd */
149 default_set_sd, /* set_sd */
150 no_lookup_name, /* lookup_name */
151 no_link_name, /* link_name */
152 NULL, /* unlink_name */
153 no_open_file, /* open_file */
154 no_close_handle, /* close_handle */
155 destroy_thread /* destroy */
158 static const struct fd_ops thread_fd_ops =
160 NULL, /* get_poll_events */
161 thread_poll_event, /* poll_event */
162 NULL, /* flush */
163 NULL, /* get_fd_type */
164 NULL, /* ioctl */
165 NULL, /* queue_async */
166 NULL /* reselect_async */
169 static struct list thread_list = LIST_INIT(thread_list);
171 /* initialize the structure for a newly allocated thread */
172 static inline void init_thread_structure( struct thread *thread )
174 int i;
176 thread->unix_pid = -1; /* not known yet */
177 thread->unix_tid = -1; /* not known yet */
178 thread->context = NULL;
179 thread->suspend_context = NULL;
180 thread->teb = 0;
181 thread->entry_point = 0;
182 thread->debug_ctx = NULL;
183 thread->debug_event = NULL;
184 thread->debug_break = 0;
185 thread->queue = NULL;
186 thread->wait = NULL;
187 thread->error = 0;
188 thread->req_data = NULL;
189 thread->req_toread = 0;
190 thread->reply_data = NULL;
191 thread->reply_towrite = 0;
192 thread->request_fd = NULL;
193 thread->reply_fd = NULL;
194 thread->wait_fd = NULL;
195 thread->state = RUNNING;
196 thread->exit_code = 0;
197 thread->priority = 0;
198 thread->suspend = 0;
199 thread->desktop_users = 0;
200 thread->token = NULL;
202 thread->creation_time = current_time;
203 thread->exit_time = 0;
205 list_init( &thread->mutex_list );
206 list_init( &thread->system_apc );
207 list_init( &thread->user_apc );
209 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
210 thread->inflight[i].server = thread->inflight[i].client = -1;
213 /* check if address looks valid for a client-side data structure (TEB etc.) */
214 static inline int is_valid_address( client_ptr_t addr )
216 return addr && !(addr % sizeof(int));
219 /* create a new thread */
220 struct thread *create_thread( int fd, struct process *process )
222 struct thread *thread;
224 if (process->is_terminating)
226 close( fd );
227 set_error( STATUS_PROCESS_IS_TERMINATING );
228 return NULL;
231 if (!(thread = alloc_object( &thread_ops )))
233 close( fd );
234 return NULL;
237 init_thread_structure( thread );
239 thread->process = (struct process *)grab_object( process );
240 thread->desktop = process->desktop;
241 thread->affinity = process->affinity;
242 if (!current) current = thread;
244 list_add_head( &thread_list, &thread->entry );
246 if (!(thread->id = alloc_ptid( thread )))
248 close( fd );
249 release_object( thread );
250 return NULL;
252 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
254 release_object( thread );
255 return NULL;
258 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
259 add_process_thread( thread->process, thread );
260 return thread;
263 /* handle a client event */
264 static void thread_poll_event( struct fd *fd, int event )
266 struct thread *thread = get_fd_user( fd );
267 assert( thread->obj.ops == &thread_ops );
269 grab_object( thread );
270 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
271 else if (event & POLLIN) read_request( thread );
272 else if (event & POLLOUT) write_reply( thread );
273 release_object( thread );
276 /* cleanup everything that is no longer needed by a dead thread */
277 /* used by destroy_thread and kill_thread */
278 static void cleanup_thread( struct thread *thread )
280 int i;
282 clear_apc_queue( &thread->system_apc );
283 clear_apc_queue( &thread->user_apc );
284 free( thread->req_data );
285 free( thread->reply_data );
286 if (thread->request_fd) release_object( thread->request_fd );
287 if (thread->reply_fd) release_object( thread->reply_fd );
288 if (thread->wait_fd) release_object( thread->wait_fd );
289 free( thread->suspend_context );
290 cleanup_clipboard_thread(thread);
291 destroy_thread_windows( thread );
292 free_msg_queue( thread );
293 close_thread_desktop( thread );
294 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
296 if (thread->inflight[i].client != -1)
298 close( thread->inflight[i].server );
299 thread->inflight[i].client = thread->inflight[i].server = -1;
302 thread->req_data = NULL;
303 thread->reply_data = NULL;
304 thread->request_fd = NULL;
305 thread->reply_fd = NULL;
306 thread->wait_fd = NULL;
307 thread->context = NULL;
308 thread->suspend_context = NULL;
309 thread->desktop = 0;
312 /* destroy a thread when its refcount is 0 */
313 static void destroy_thread( struct object *obj )
315 struct thread *thread = (struct thread *)obj;
316 assert( obj->ops == &thread_ops );
318 assert( !thread->debug_ctx ); /* cannot still be debugging something */
319 list_remove( &thread->entry );
320 cleanup_thread( thread );
321 release_object( thread->process );
322 if (thread->id) free_ptid( thread->id );
323 if (thread->token) release_object( thread->token );
326 /* dump a thread on stdout for debugging purposes */
327 static void dump_thread( struct object *obj, int verbose )
329 struct thread *thread = (struct thread *)obj;
330 assert( obj->ops == &thread_ops );
332 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
333 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
336 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
338 struct thread *mythread = (struct thread *)obj;
339 return (mythread->state == TERMINATED);
342 static unsigned int thread_map_access( struct object *obj, unsigned int access )
344 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
345 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
346 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
347 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
348 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
350 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
351 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
353 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
356 static void dump_thread_apc( struct object *obj, int verbose )
358 struct thread_apc *apc = (struct thread_apc *)obj;
359 assert( obj->ops == &thread_apc_ops );
361 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
364 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
366 struct thread_apc *apc = (struct thread_apc *)obj;
367 return apc->executed;
370 static void thread_apc_destroy( struct object *obj )
372 struct thread_apc *apc = (struct thread_apc *)obj;
373 if (apc->caller) release_object( apc->caller );
374 if (apc->owner) release_object( apc->owner );
377 /* queue an async procedure call */
378 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
380 struct thread_apc *apc;
382 if ((apc = alloc_object( &thread_apc_ops )))
384 apc->call = *call_data;
385 apc->caller = NULL;
386 apc->owner = owner;
387 apc->executed = 0;
388 apc->result.type = APC_NONE;
389 if (owner) grab_object( owner );
391 return apc;
394 /* get a thread pointer from a thread id (and increment the refcount) */
395 struct thread *get_thread_from_id( thread_id_t id )
397 struct object *obj = get_ptid_entry( id );
399 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
400 set_error( STATUS_INVALID_CID );
401 return NULL;
404 /* get a thread from a handle (and increment the refcount) */
405 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
407 return (struct thread *)get_handle_obj( current->process, handle,
408 access, &thread_ops );
411 /* find a thread from a Unix tid */
412 struct thread *get_thread_from_tid( int tid )
414 struct thread *thread;
416 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
418 if (thread->unix_tid == tid) return thread;
420 return NULL;
423 /* find a thread from a Unix pid */
424 struct thread *get_thread_from_pid( int pid )
426 struct thread *thread;
428 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
430 if (thread->unix_pid == pid) return thread;
432 return NULL;
435 int set_thread_affinity( struct thread *thread, affinity_t affinity )
437 int ret = 0;
438 #ifdef HAVE_SCHED_SETAFFINITY
439 if (thread->unix_tid != -1)
441 cpu_set_t set;
442 int i;
443 affinity_t mask;
445 CPU_ZERO( &set );
446 for (i = 0, mask = 1; mask; i++, mask <<= 1)
447 if (affinity & mask) CPU_SET( i, &set );
449 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
451 #endif
452 if (!ret) thread->affinity = affinity;
453 return ret;
456 affinity_t get_thread_affinity( struct thread *thread )
458 affinity_t mask = 0;
459 #ifdef HAVE_SCHED_SETAFFINITY
460 if (thread->unix_tid != -1)
462 cpu_set_t set;
463 unsigned int i;
465 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
466 for (i = 0; i < 8 * sizeof(mask); i++)
467 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
469 #endif
470 if (!mask) mask = ~(affinity_t)0;
471 return mask;
474 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
475 #define THREAD_PRIORITY_REALTIME_LOWEST -7
477 /* set all information about a thread */
478 static void set_thread_info( struct thread *thread,
479 const struct set_thread_info_request *req )
481 if (req->mask & SET_THREAD_INFO_PRIORITY)
483 int max = THREAD_PRIORITY_HIGHEST;
484 int min = THREAD_PRIORITY_LOWEST;
485 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
487 max = THREAD_PRIORITY_REALTIME_HIGHEST;
488 min = THREAD_PRIORITY_REALTIME_LOWEST;
490 if ((req->priority >= min && req->priority <= max) ||
491 req->priority == THREAD_PRIORITY_IDLE ||
492 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
493 thread->priority = req->priority;
494 else
495 set_error( STATUS_INVALID_PARAMETER );
497 if (req->mask & SET_THREAD_INFO_AFFINITY)
499 if ((req->affinity & thread->process->affinity) != req->affinity)
500 set_error( STATUS_INVALID_PARAMETER );
501 else if (thread->state == TERMINATED)
502 set_error( STATUS_THREAD_IS_TERMINATING );
503 else if (set_thread_affinity( thread, req->affinity ))
504 file_set_error();
506 if (req->mask & SET_THREAD_INFO_TOKEN)
507 security_set_thread_token( thread, req->token );
508 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
509 thread->entry_point = req->entry_point;
512 /* stop a thread (at the Unix level) */
513 void stop_thread( struct thread *thread )
515 if (thread->context) return; /* already inside a debug event, no need for a signal */
516 /* can't stop a thread while initialisation is in progress */
517 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
520 /* stop a thread if it's supposed to be suspended */
521 void stop_thread_if_suspended( struct thread *thread )
523 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
526 /* suspend a thread */
527 static int suspend_thread( struct thread *thread )
529 int old_count = thread->suspend;
530 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
532 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
534 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
535 return old_count;
538 /* resume a thread */
539 static int resume_thread( struct thread *thread )
541 int old_count = thread->suspend;
542 if (thread->suspend > 0)
544 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
546 return old_count;
549 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
550 int add_queue( struct object *obj, struct wait_queue_entry *entry )
552 grab_object( obj );
553 entry->obj = obj;
554 list_add_tail( &obj->wait_queue, &entry->entry );
555 return 1;
558 /* remove a thread from an object wait queue */
559 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
561 list_remove( &entry->entry );
562 release_object( obj );
565 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
567 return entry->wait->thread;
570 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
572 return entry->wait->select;
575 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
577 return entry->wait->key;
580 void make_wait_abandoned( struct wait_queue_entry *entry )
582 entry->wait->abandoned = 1;
585 /* finish waiting */
586 static unsigned int end_wait( struct thread *thread, unsigned int status )
588 struct thread_wait *wait = thread->wait;
589 struct wait_queue_entry *entry;
590 int i;
592 assert( wait );
593 thread->wait = wait->next;
595 if (status < wait->count) /* wait satisfied, tell it to the objects */
597 if (wait->select == SELECT_WAIT_ALL)
599 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
600 entry->obj->ops->satisfied( entry->obj, entry );
602 else
604 entry = wait->queues + status;
605 entry->obj->ops->satisfied( entry->obj, entry );
607 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
609 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
610 entry->obj->ops->remove_queue( entry->obj, entry );
611 if (wait->user) remove_timeout_user( wait->user );
612 free( wait );
613 return status;
616 /* build the thread wait structure */
617 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
618 int flags, timeout_t timeout )
620 struct thread_wait *wait;
621 struct wait_queue_entry *entry;
622 unsigned int i;
624 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
625 wait->next = current->wait;
626 wait->thread = current;
627 wait->count = count;
628 wait->flags = flags;
629 wait->select = select_op->op;
630 wait->cookie = 0;
631 wait->user = NULL;
632 wait->timeout = timeout;
633 wait->abandoned = 0;
634 current->wait = wait;
636 for (i = 0, entry = wait->queues; i < count; i++, entry++)
638 struct object *obj = objects[i];
639 entry->wait = wait;
640 if (!obj->ops->add_queue( obj, entry ))
642 wait->count = i;
643 end_wait( current, get_error() );
644 return 0;
647 return 1;
650 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
651 int flags, timeout_t timeout )
653 struct object *objects[MAXIMUM_WAIT_OBJECTS];
654 unsigned int i;
655 int ret = 0;
657 assert( count <= MAXIMUM_WAIT_OBJECTS );
659 for (i = 0; i < count; i++)
660 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
661 break;
663 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
665 while (i > 0) release_object( objects[--i] );
666 return ret;
669 /* check if the thread waiting condition is satisfied */
670 static int check_wait( struct thread *thread )
672 int i;
673 struct thread_wait *wait = thread->wait;
674 struct wait_queue_entry *entry;
676 assert( wait );
678 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
679 return STATUS_USER_APC;
681 /* Suspended threads may not acquire locks, but they can run system APCs */
682 if (thread->process->suspend + thread->suspend > 0) return -1;
684 if (wait->select == SELECT_WAIT_ALL)
686 int not_ok = 0;
687 /* Note: we must check them all anyway, as some objects may
688 * want to do something when signaled, even if others are not */
689 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
690 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
691 if (!not_ok) return STATUS_WAIT_0;
693 else
695 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
696 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
699 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
700 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
701 return -1;
704 /* send the wakeup signal to a thread */
705 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
707 struct wake_up_reply reply;
708 int ret;
710 memset( &reply, 0, sizeof(reply) );
711 reply.cookie = cookie;
712 reply.signaled = signaled;
713 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
714 return 0;
715 if (ret >= 0)
716 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
717 else if (errno == EPIPE)
718 kill_thread( thread, 0 ); /* normal death */
719 else
720 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
721 return -1;
724 /* attempt to wake up a thread */
725 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
726 int wake_thread( struct thread *thread )
728 int signaled, count;
729 client_ptr_t cookie;
731 for (count = 0; thread->wait; count++)
733 if ((signaled = check_wait( thread )) == -1) break;
735 cookie = thread->wait->cookie;
736 signaled = end_wait( thread, signaled );
737 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
738 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
740 if (!count) count = -1;
741 break;
744 return count;
747 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
748 int wake_thread_queue_entry( struct wait_queue_entry *entry )
750 struct thread_wait *wait = entry->wait;
751 struct thread *thread = wait->thread;
752 int signaled;
753 client_ptr_t cookie;
755 if (thread->wait != wait) return 0; /* not the current wait */
756 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
758 assert( wait->select != SELECT_WAIT_ALL );
760 cookie = wait->cookie;
761 signaled = end_wait( thread, entry - wait->queues );
762 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
764 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
765 wake_thread( thread ); /* check other waits too */
767 return 1;
770 /* thread wait timeout */
771 static void thread_timeout( void *ptr )
773 struct thread_wait *wait = ptr;
774 struct thread *thread = wait->thread;
775 client_ptr_t cookie = wait->cookie;
777 wait->user = NULL;
778 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
779 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
781 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
782 end_wait( thread, STATUS_TIMEOUT );
784 assert( cookie );
785 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
786 /* check if other objects have become signaled in the meantime */
787 wake_thread( thread );
790 /* try signaling an event flag, a semaphore or a mutex */
791 static int signal_object( obj_handle_t handle )
793 struct object *obj;
794 int ret = 0;
796 obj = get_handle_obj( current->process, handle, 0, NULL );
797 if (obj)
799 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
800 release_object( obj );
802 return ret;
805 /* select on a list of handles */
806 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
807 int flags, timeout_t timeout )
809 int ret;
810 unsigned int count;
811 struct object *object;
813 if (timeout <= 0) timeout = current_time - timeout;
815 switch (select_op->op)
817 case SELECT_NONE:
818 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
819 break;
821 case SELECT_WAIT:
822 case SELECT_WAIT_ALL:
823 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
824 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
826 set_error( STATUS_INVALID_PARAMETER );
827 return 0;
829 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
830 return timeout;
831 break;
833 case SELECT_SIGNAL_AND_WAIT:
834 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
835 return timeout;
836 if (select_op->signal_and_wait.signal)
838 if (!signal_object( select_op->signal_and_wait.signal ))
840 end_wait( current, get_error() );
841 return timeout;
843 /* check if we woke ourselves up */
844 if (!current->wait) return timeout;
846 break;
848 case SELECT_KEYED_EVENT_WAIT:
849 case SELECT_KEYED_EVENT_RELEASE:
850 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
851 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
852 if (!object) return timeout;
853 ret = wait_on( select_op, 1, &object, flags, timeout );
854 release_object( object );
855 if (!ret) return timeout;
856 current->wait->key = select_op->keyed_event.key;
857 break;
859 default:
860 set_error( STATUS_INVALID_PARAMETER );
861 return 0;
864 if ((ret = check_wait( current )) != -1)
866 /* condition is already satisfied */
867 set_error( end_wait( current, ret ));
868 return timeout;
871 /* now we need to wait */
872 if (current->wait->timeout != TIMEOUT_INFINITE)
874 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
875 thread_timeout, current->wait )))
877 end_wait( current, get_error() );
878 return timeout;
881 current->wait->cookie = cookie;
882 set_error( STATUS_PENDING );
883 return timeout;
886 /* attempt to wake threads sleeping on the object wait queue */
887 void wake_up( struct object *obj, int max )
889 struct list *ptr;
890 int ret;
892 LIST_FOR_EACH( ptr, &obj->wait_queue )
894 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
895 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
896 if (ret > 0 && max && !--max) break;
897 /* restart at the head of the list since a wake up can change the object wait queue */
898 ptr = &obj->wait_queue;
902 /* return the apc queue to use for a given apc type */
903 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
905 switch(type)
907 case APC_NONE:
908 case APC_USER:
909 case APC_TIMER:
910 return &thread->user_apc;
911 default:
912 return &thread->system_apc;
916 /* check if thread is currently waiting for a (system) apc */
917 static inline int is_in_apc_wait( struct thread *thread )
919 return (thread->process->suspend || thread->suspend ||
920 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
923 /* queue an existing APC to a given thread */
924 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
926 struct list *queue;
928 if (!thread) /* find a suitable thread inside the process */
930 struct thread *candidate;
932 /* first try to find a waiting thread */
933 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
935 if (candidate->state == TERMINATED) continue;
936 if (is_in_apc_wait( candidate ))
938 thread = candidate;
939 break;
942 if (!thread)
944 /* then use the first one that accepts a signal */
945 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
947 if (send_thread_signal( candidate, SIGUSR1 ))
949 thread = candidate;
950 break;
954 if (!thread) return 0; /* nothing found */
955 queue = get_apc_queue( thread, apc->call.type );
957 else
959 if (thread->state == TERMINATED) return 0;
960 queue = get_apc_queue( thread, apc->call.type );
961 /* send signal for system APCs if needed */
962 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
964 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
966 /* cancel a possible previous APC with the same owner */
967 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
970 grab_object( apc );
971 list_add_tail( queue, &apc->entry );
972 if (!list_prev( queue, &apc->entry )) /* first one */
973 wake_thread( thread );
975 return 1;
978 /* queue an async procedure call */
979 int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data )
981 struct thread_apc *apc;
982 int ret = 0;
984 if ((apc = create_apc( owner, call_data )))
986 ret = queue_apc( NULL, thread, apc );
987 release_object( apc );
989 return ret;
992 /* cancel the async procedure call owned by a specific object */
993 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
995 struct thread_apc *apc;
996 struct list *queue = get_apc_queue( thread, type );
998 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1000 if (apc->owner != owner) continue;
1001 list_remove( &apc->entry );
1002 apc->executed = 1;
1003 wake_up( &apc->obj, 0 );
1004 release_object( apc );
1005 return;
1009 /* remove the head apc from the queue; the returned object must be released by the caller */
1010 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1012 struct thread_apc *apc = NULL;
1013 struct list *ptr = list_head( &thread->system_apc );
1015 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1016 if (ptr)
1018 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1019 list_remove( ptr );
1021 return apc;
1024 /* clear an APC queue, cancelling all the APCs on it */
1025 static void clear_apc_queue( struct list *queue )
1027 struct list *ptr;
1029 while ((ptr = list_head( queue )))
1031 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1032 list_remove( &apc->entry );
1033 apc->executed = 1;
1034 wake_up( &apc->obj, 0 );
1035 release_object( apc );
1039 /* add an fd to the inflight list */
1040 /* return list index, or -1 on error */
1041 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1043 int i;
1045 if (server == -1) return -1;
1046 if (client == -1)
1048 close( server );
1049 return -1;
1052 /* first check if we already have an entry for this fd */
1053 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1054 if (thread->inflight[i].client == client)
1056 close( thread->inflight[i].server );
1057 thread->inflight[i].server = server;
1058 return i;
1061 /* now find a free spot to store it */
1062 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1063 if (thread->inflight[i].client == -1)
1065 thread->inflight[i].client = client;
1066 thread->inflight[i].server = server;
1067 return i;
1070 close( server );
1071 return -1;
1074 /* get an inflight fd and purge it from the list */
1075 /* the fd must be closed when no longer used */
1076 int thread_get_inflight_fd( struct thread *thread, int client )
1078 int i, ret;
1080 if (client == -1) return -1;
1084 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1086 if (thread->inflight[i].client == client)
1088 ret = thread->inflight[i].server;
1089 thread->inflight[i].server = thread->inflight[i].client = -1;
1090 return ret;
1093 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1094 return -1;
1097 /* kill a thread on the spot */
1098 void kill_thread( struct thread *thread, int violent_death )
1100 if (thread->state == TERMINATED) return; /* already killed */
1101 thread->state = TERMINATED;
1102 thread->exit_time = current_time;
1103 if (current == thread) current = NULL;
1104 if (debug_level)
1105 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1106 thread->id, thread->exit_code );
1107 if (thread->wait)
1109 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1110 send_thread_wakeup( thread, 0, thread->exit_code );
1111 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1112 violent_death = 0;
1114 kill_console_processes( thread, 0 );
1115 debug_exit_thread( thread );
1116 abandon_mutexes( thread );
1117 wake_up( &thread->obj, 0 );
1118 if (violent_death) send_thread_signal( thread, SIGQUIT );
1119 cleanup_thread( thread );
1120 remove_process_thread( thread->process, thread );
1121 release_object( thread );
1124 /* copy parts of a context structure */
1125 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1127 assert( to->cpu == from->cpu );
1128 to->flags |= flags;
1129 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1130 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1131 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1132 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1133 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1134 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1137 /* return the context flags that correspond to system regs */
1138 /* (system regs are the ones we can't access on the client side) */
1139 static unsigned int get_context_system_regs( enum cpu_type cpu )
1141 switch (cpu)
1143 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1144 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1145 case CPU_POWERPC: return 0;
1146 case CPU_ARM: return 0;
1147 case CPU_ARM64: return 0;
1149 return 0;
1152 /* trigger a breakpoint event in a given thread */
1153 void break_thread( struct thread *thread )
1155 debug_event_t data;
1157 assert( thread->context );
1159 memset( &data, 0, sizeof(data) );
1160 data.exception.first = 1;
1161 data.exception.exc_code = STATUS_BREAKPOINT;
1162 data.exception.flags = EXCEPTION_CONTINUABLE;
1163 switch (thread->context->cpu)
1165 case CPU_x86:
1166 data.exception.address = thread->context->ctl.i386_regs.eip;
1167 break;
1168 case CPU_x86_64:
1169 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1170 break;
1171 case CPU_POWERPC:
1172 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1173 break;
1174 case CPU_ARM:
1175 data.exception.address = thread->context->ctl.arm_regs.pc;
1176 break;
1177 case CPU_ARM64:
1178 data.exception.address = thread->context->ctl.arm64_regs.pc;
1179 break;
1181 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1182 thread->debug_break = 0;
1185 /* take a snapshot of currently running threads */
1186 struct thread_snapshot *thread_snap( int *count )
1188 struct thread_snapshot *snapshot, *ptr;
1189 struct thread *thread;
1190 int total = 0;
1192 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1193 if (thread->state != TERMINATED) total++;
1194 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1195 ptr = snapshot;
1196 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1198 if (thread->state == TERMINATED) continue;
1199 ptr->thread = thread;
1200 ptr->count = thread->obj.refcount;
1201 ptr->priority = thread->priority;
1202 grab_object( thread );
1203 ptr++;
1205 *count = total;
1206 return snapshot;
1209 /* gets the current impersonation token */
1210 struct token *thread_get_impersonation_token( struct thread *thread )
1212 if (thread->token)
1213 return thread->token;
1214 else
1215 return thread->process->token;
1218 /* check if a cpu type can be supported on this server */
1219 int is_cpu_supported( enum cpu_type cpu )
1221 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1223 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1224 if (!(supported_cpus & prefix_cpu_mask))
1225 set_error( STATUS_NOT_SUPPORTED );
1226 else if (supported_cpus & CPU_FLAG(cpu))
1227 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1228 else
1229 set_error( STATUS_INVALID_IMAGE_FORMAT );
1230 return 0;
1233 /* create a new thread */
1234 DECL_HANDLER(new_thread)
1236 struct thread *thread;
1237 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1239 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1241 if (request_fd != -1) close( request_fd );
1242 set_error( STATUS_INVALID_HANDLE );
1243 return;
1246 if ((thread = create_thread( request_fd, current->process )))
1248 if (req->suspend) thread->suspend++;
1249 reply->tid = get_thread_id( thread );
1250 if ((reply->handle = alloc_handle( current->process, thread, req->access, req->attributes )))
1252 /* thread object will be released when the thread gets killed */
1253 return;
1255 kill_thread( thread, 1 );
1259 /* initialize a new thread */
1260 DECL_HANDLER(init_thread)
1262 struct process *process = current->process;
1263 int wait_fd, reply_fd;
1265 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1267 set_error( STATUS_TOO_MANY_OPENED_FILES );
1268 return;
1270 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1272 set_error( STATUS_TOO_MANY_OPENED_FILES );
1273 goto error;
1276 if (current->reply_fd) /* already initialised */
1278 set_error( STATUS_INVALID_PARAMETER );
1279 goto error;
1282 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1284 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1285 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1286 if (!current->reply_fd || !current->wait_fd) return;
1288 if (!is_valid_address(req->teb))
1290 set_error( STATUS_INVALID_PARAMETER );
1291 return;
1294 current->unix_pid = req->unix_pid;
1295 current->unix_tid = req->unix_tid;
1296 current->teb = req->teb;
1297 current->entry_point = process->peb ? req->entry : 0;
1299 if (!process->peb) /* first thread, initialize the process too */
1301 if (!is_cpu_supported( req->cpu )) return;
1302 process->unix_pid = current->unix_pid;
1303 process->peb = req->entry;
1304 process->cpu = req->cpu;
1305 reply->info_size = init_process( current );
1306 if (!process->parent_id)
1307 process->affinity = current->affinity = get_thread_affinity( current );
1308 else
1309 set_thread_affinity( current, current->affinity );
1311 else
1313 if (req->cpu != process->cpu)
1315 set_error( STATUS_INVALID_PARAMETER );
1316 return;
1318 if (process->unix_pid != current->unix_pid)
1319 process->unix_pid = -1; /* can happen with linuxthreads */
1320 stop_thread_if_suspended( current );
1321 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1322 set_thread_affinity( current, current->affinity );
1324 debug_level = max( debug_level, req->debug_level );
1326 reply->pid = get_process_id( process );
1327 reply->tid = get_thread_id( current );
1328 reply->version = SERVER_PROTOCOL_VERSION;
1329 reply->server_start = server_start_time;
1330 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1331 return;
1333 error:
1334 if (reply_fd != -1) close( reply_fd );
1335 if (wait_fd != -1) close( wait_fd );
1338 /* terminate a thread */
1339 DECL_HANDLER(terminate_thread)
1341 struct thread *thread;
1343 reply->self = 0;
1344 reply->last = 0;
1345 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1347 thread->exit_code = req->exit_code;
1348 if (thread != current) kill_thread( thread, 1 );
1349 else
1351 reply->self = 1;
1352 reply->last = (thread->process->running_threads == 1);
1354 release_object( thread );
1358 /* open a handle to a thread */
1359 DECL_HANDLER(open_thread)
1361 struct thread *thread = get_thread_from_id( req->tid );
1363 reply->handle = 0;
1364 if (thread)
1366 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1367 release_object( thread );
1371 /* fetch information about a thread */
1372 DECL_HANDLER(get_thread_info)
1374 struct thread *thread;
1375 obj_handle_t handle = req->handle;
1377 if (!handle) thread = get_thread_from_id( req->tid_in );
1378 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1380 if (thread)
1382 reply->pid = get_process_id( thread->process );
1383 reply->tid = get_thread_id( thread );
1384 reply->teb = thread->teb;
1385 reply->entry_point = thread->entry_point;
1386 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1387 reply->priority = thread->priority;
1388 reply->affinity = thread->affinity;
1389 reply->last = thread->process->running_threads == 1;
1391 release_object( thread );
1395 /* fetch information about thread times */
1396 DECL_HANDLER(get_thread_times)
1398 struct thread *thread;
1400 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1402 reply->creation_time = thread->creation_time;
1403 reply->exit_time = thread->exit_time;
1405 release_object( thread );
1409 /* set information about a thread */
1410 DECL_HANDLER(set_thread_info)
1412 struct thread *thread;
1414 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1416 set_thread_info( thread, req );
1417 release_object( thread );
1421 /* suspend a thread */
1422 DECL_HANDLER(suspend_thread)
1424 struct thread *thread;
1426 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1428 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1429 else reply->count = suspend_thread( thread );
1430 release_object( thread );
1434 /* resume a thread */
1435 DECL_HANDLER(resume_thread)
1437 struct thread *thread;
1439 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1441 reply->count = resume_thread( thread );
1442 release_object( thread );
1446 /* select on a handle list */
1447 DECL_HANDLER(select)
1449 select_op_t select_op;
1450 data_size_t op_size;
1451 struct thread_apc *apc;
1452 const apc_result_t *result = get_req_data();
1454 if (get_req_data_size() < sizeof(*result))
1456 set_error( STATUS_INVALID_PARAMETER );
1457 return;
1459 if (!req->cookie)
1461 set_error( STATUS_INVALID_PARAMETER );
1462 return;
1465 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1466 memset( &select_op, 0, sizeof(select_op) );
1467 memcpy( &select_op, result + 1, op_size );
1469 /* first store results of previous apc */
1470 if (req->prev_apc)
1472 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1473 0, &thread_apc_ops ))) return;
1474 apc->result = *result;
1475 apc->executed = 1;
1476 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1478 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1479 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1480 close_handle( current->process, apc->result.create_thread.handle );
1481 apc->result.create_thread.handle = handle;
1482 clear_error(); /* ignore errors from the above calls */
1484 else if (apc->result.type == APC_ASYNC_IO)
1486 if (apc->owner)
1487 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1489 wake_up( &apc->obj, 0 );
1490 close_handle( current->process, req->prev_apc );
1491 release_object( apc );
1494 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1496 if (get_error() == STATUS_USER_APC)
1498 for (;;)
1500 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1501 break;
1502 /* Optimization: ignore APC_NONE calls, they are only used to
1503 * wake up a thread, but since we got here the thread woke up already.
1505 if (apc->call.type != APC_NONE &&
1506 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1508 reply->call = apc->call;
1509 release_object( apc );
1510 break;
1512 apc->executed = 1;
1513 wake_up( &apc->obj, 0 );
1514 release_object( apc );
1519 /* queue an APC for a thread or process */
1520 DECL_HANDLER(queue_apc)
1522 struct thread *thread = NULL;
1523 struct process *process = NULL;
1524 struct thread_apc *apc;
1526 if (!(apc = create_apc( NULL, &req->call ))) return;
1528 switch (apc->call.type)
1530 case APC_NONE:
1531 case APC_USER:
1532 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1533 break;
1534 case APC_VIRTUAL_ALLOC:
1535 case APC_VIRTUAL_FREE:
1536 case APC_VIRTUAL_PROTECT:
1537 case APC_VIRTUAL_FLUSH:
1538 case APC_VIRTUAL_LOCK:
1539 case APC_VIRTUAL_UNLOCK:
1540 case APC_UNMAP_VIEW:
1541 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1542 break;
1543 case APC_VIRTUAL_QUERY:
1544 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1545 break;
1546 case APC_MAP_VIEW:
1547 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1548 if (process && process != current->process)
1550 /* duplicate the handle into the target process */
1551 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1552 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1553 if (handle) apc->call.map_view.handle = handle;
1554 else
1556 release_object( process );
1557 process = NULL;
1560 break;
1561 case APC_CREATE_THREAD:
1562 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1563 break;
1564 default:
1565 set_error( STATUS_INVALID_PARAMETER );
1566 break;
1569 if (thread)
1571 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1572 release_object( thread );
1574 else if (process)
1576 reply->self = (process == current->process);
1577 if (!reply->self)
1579 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1580 if (handle)
1582 if (queue_apc( process, NULL, apc ))
1584 apc->caller = (struct thread *)grab_object( current );
1585 reply->handle = handle;
1587 else
1589 close_handle( current->process, handle );
1590 set_error( STATUS_PROCESS_IS_TERMINATING );
1594 release_object( process );
1597 release_object( apc );
1600 /* Get the result of an APC call */
1601 DECL_HANDLER(get_apc_result)
1603 struct thread_apc *apc;
1605 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1606 0, &thread_apc_ops ))) return;
1608 if (apc->executed) reply->result = apc->result;
1609 else set_error( STATUS_PENDING );
1611 /* close the handle directly to avoid an extra round-trip */
1612 close_handle( current->process, req->handle );
1613 release_object( apc );
1616 /* retrieve the current context of a thread */
1617 DECL_HANDLER(get_thread_context)
1619 struct thread *thread;
1620 context_t *context;
1622 if (get_reply_max_size() < sizeof(context_t))
1624 set_error( STATUS_INVALID_PARAMETER );
1625 return;
1627 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1628 reply->self = (thread == current);
1630 if (thread != current && !thread->context)
1632 /* thread is not suspended, retry (if it's still running) */
1633 if (thread->state == RUNNING)
1635 set_error( STATUS_PENDING );
1636 if (req->suspend)
1638 release_object( thread );
1639 /* make sure we have suspend access */
1640 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1641 suspend_thread( thread );
1644 else set_error( STATUS_UNSUCCESSFUL );
1646 else if ((context = set_reply_data_size( sizeof(context_t) )))
1648 unsigned int flags = get_context_system_regs( thread->process->cpu );
1650 memset( context, 0, sizeof(context_t) );
1651 context->cpu = thread->process->cpu;
1652 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1653 if (flags) get_thread_context( thread, context, flags );
1655 release_object( thread );
1658 /* set the current context of a thread */
1659 DECL_HANDLER(set_thread_context)
1661 struct thread *thread;
1662 const context_t *context = get_req_data();
1664 if (get_req_data_size() < sizeof(context_t))
1666 set_error( STATUS_INVALID_PARAMETER );
1667 return;
1669 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1670 reply->self = (thread == current);
1672 if (thread != current && !thread->context)
1674 /* thread is not suspended, retry (if it's still running) */
1675 if (thread->state == RUNNING)
1677 set_error( STATUS_PENDING );
1678 if (req->suspend)
1680 release_object( thread );
1681 /* make sure we have suspend access */
1682 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1683 suspend_thread( thread );
1686 else set_error( STATUS_UNSUCCESSFUL );
1688 else if (context->cpu == thread->process->cpu)
1690 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1691 unsigned int client_flags = context->flags & ~system_flags;
1693 if (system_flags) set_thread_context( thread, context, system_flags );
1694 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1696 else set_error( STATUS_INVALID_PARAMETER );
1698 release_object( thread );
1701 /* retrieve the suspended context of a thread */
1702 DECL_HANDLER(get_suspend_context)
1704 if (get_reply_max_size() < sizeof(context_t))
1706 set_error( STATUS_INVALID_PARAMETER );
1707 return;
1710 if (current->suspend_context)
1712 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1713 if (current->context == current->suspend_context)
1715 current->context = NULL;
1716 stop_thread_if_suspended( current );
1718 current->suspend_context = NULL;
1720 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1723 /* store the suspended context of a thread */
1724 DECL_HANDLER(set_suspend_context)
1726 const context_t *context = get_req_data();
1728 if (get_req_data_size() < sizeof(context_t))
1730 set_error( STATUS_INVALID_PARAMETER );
1731 return;
1734 if (current->context || context->cpu != current->process->cpu)
1736 /* nested suspend or exception, shouldn't happen */
1737 set_error( STATUS_INVALID_PARAMETER );
1739 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1741 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1742 current->context = current->suspend_context;
1743 if (current->debug_break) break_thread( current );
1747 /* fetch a selector entry for a thread */
1748 DECL_HANDLER(get_selector_entry)
1750 struct thread *thread;
1751 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1753 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1754 release_object( thread );