wined3d: Scale transformed position in compute_light().
[wine.git] / server / thread.c
blob7057c9bbd0cc7e380d6cec3ba7753767cd881621
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SCHED_H
39 #include <sched.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
47 #include "file.h"
48 #include "handle.h"
49 #include "process.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "user.h"
53 #include "security.h"
56 #ifdef __i386__
57 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
60 #elif defined(__powerpc__)
61 static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
66 #else
67 #error Unsupported CPU
68 #endif
70 /* thread queues */
72 struct thread_wait
74 struct thread_wait *next; /* next wait structure for this thread */
75 struct thread *thread; /* owner thread */
76 int count; /* count of objects */
77 int flags;
78 int abandoned;
79 enum select_op select;
80 client_ptr_t key; /* wait key for keyed events */
81 client_ptr_t cookie; /* magic cookie to return to client */
82 timeout_t timeout;
83 struct timeout_user *user;
84 struct wait_queue_entry queues[1];
87 /* asynchronous procedure calls */
89 struct thread_apc
91 struct object obj; /* object header */
92 struct list entry; /* queue linked list */
93 struct thread *caller; /* thread that queued this apc */
94 struct object *owner; /* object that queued this apc */
95 int executed; /* has it been executed by the client? */
96 apc_call_t call; /* call arguments */
97 apc_result_t result; /* call results once executed */
100 static void dump_thread_apc( struct object *obj, int verbose );
101 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
102 static void thread_apc_destroy( struct object *obj );
103 static void clear_apc_queue( struct list *queue );
105 static const struct object_ops thread_apc_ops =
107 sizeof(struct thread_apc), /* size */
108 dump_thread_apc, /* dump */
109 no_get_type, /* get_type */
110 add_queue, /* add_queue */
111 remove_queue, /* remove_queue */
112 thread_apc_signaled, /* signaled */
113 no_satisfied, /* satisfied */
114 no_signal, /* signal */
115 no_get_fd, /* get_fd */
116 no_map_access, /* map_access */
117 default_get_sd, /* get_sd */
118 default_set_sd, /* set_sd */
119 no_lookup_name, /* lookup_name */
120 no_link_name, /* link_name */
121 NULL, /* unlink_name */
122 no_open_file, /* open_file */
123 no_kernel_obj_list, /* get_kernel_obj_list */
124 no_close_handle, /* close_handle */
125 thread_apc_destroy /* destroy */
129 /* thread operations */
131 static void dump_thread( struct object *obj, int verbose );
132 static struct object_type *thread_get_type( struct object *obj );
133 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
134 static unsigned int thread_map_access( struct object *obj, unsigned int access );
135 static void thread_poll_event( struct fd *fd, int event );
136 static struct list *thread_get_kernel_obj_list( struct object *obj );
137 static void destroy_thread( struct object *obj );
139 static const struct object_ops thread_ops =
141 sizeof(struct thread), /* size */
142 dump_thread, /* dump */
143 thread_get_type, /* get_type */
144 add_queue, /* add_queue */
145 remove_queue, /* remove_queue */
146 thread_signaled, /* signaled */
147 no_satisfied, /* satisfied */
148 no_signal, /* signal */
149 no_get_fd, /* get_fd */
150 thread_map_access, /* map_access */
151 default_get_sd, /* get_sd */
152 default_set_sd, /* set_sd */
153 no_lookup_name, /* lookup_name */
154 no_link_name, /* link_name */
155 NULL, /* unlink_name */
156 no_open_file, /* open_file */
157 thread_get_kernel_obj_list, /* get_kernel_obj_list */
158 no_close_handle, /* close_handle */
159 destroy_thread /* destroy */
162 static const struct fd_ops thread_fd_ops =
164 NULL, /* get_poll_events */
165 thread_poll_event, /* poll_event */
166 NULL, /* flush */
167 NULL, /* get_fd_type */
168 NULL, /* ioctl */
169 NULL, /* queue_async */
170 NULL /* reselect_async */
173 static struct list thread_list = LIST_INIT(thread_list);
175 /* initialize the structure for a newly allocated thread */
176 static inline void init_thread_structure( struct thread *thread )
178 int i;
180 thread->unix_pid = -1; /* not known yet */
181 thread->unix_tid = -1; /* not known yet */
182 thread->context = NULL;
183 thread->suspend_context = NULL;
184 thread->teb = 0;
185 thread->entry_point = 0;
186 thread->debug_ctx = NULL;
187 thread->debug_event = NULL;
188 thread->debug_break = 0;
189 thread->system_regs = 0;
190 thread->queue = NULL;
191 thread->wait = NULL;
192 thread->error = 0;
193 thread->req_data = NULL;
194 thread->req_toread = 0;
195 thread->reply_data = NULL;
196 thread->reply_towrite = 0;
197 thread->request_fd = NULL;
198 thread->reply_fd = NULL;
199 thread->wait_fd = NULL;
200 thread->state = RUNNING;
201 thread->exit_code = 0;
202 thread->priority = 0;
203 thread->suspend = 0;
204 thread->desktop_users = 0;
205 thread->token = NULL;
207 thread->creation_time = current_time;
208 thread->exit_time = 0;
210 list_init( &thread->mutex_list );
211 list_init( &thread->system_apc );
212 list_init( &thread->user_apc );
213 list_init( &thread->kernel_object );
215 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
216 thread->inflight[i].server = thread->inflight[i].client = -1;
219 /* check if address looks valid for a client-side data structure (TEB etc.) */
220 static inline int is_valid_address( client_ptr_t addr )
222 return addr && !(addr % sizeof(int));
225 /* create a new thread */
226 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
228 struct thread *thread;
229 int request_pipe[2];
231 if (fd == -1)
233 if (pipe( request_pipe ) == -1)
235 file_set_error();
236 return NULL;
238 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
240 close( request_pipe[0] );
241 close( request_pipe[1] );
242 return NULL;
244 close( request_pipe[1] );
245 fd = request_pipe[0];
248 if (process->is_terminating)
250 close( fd );
251 set_error( STATUS_PROCESS_IS_TERMINATING );
252 return NULL;
255 if (!(thread = alloc_object( &thread_ops )))
257 close( fd );
258 return NULL;
261 init_thread_structure( thread );
263 thread->process = (struct process *)grab_object( process );
264 thread->desktop = process->desktop;
265 thread->affinity = process->affinity;
266 if (!current) current = thread;
268 list_add_head( &thread_list, &thread->entry );
270 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
271 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
272 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
273 process->token ))
275 close( fd );
276 release_object( thread );
277 return NULL;
279 if (!(thread->id = alloc_ptid( thread )))
281 close( fd );
282 release_object( thread );
283 return NULL;
285 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
287 release_object( thread );
288 return NULL;
291 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
292 add_process_thread( thread->process, thread );
293 return thread;
296 /* handle a client event */
297 static void thread_poll_event( struct fd *fd, int event )
299 struct thread *thread = get_fd_user( fd );
300 assert( thread->obj.ops == &thread_ops );
302 grab_object( thread );
303 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
304 else if (event & POLLIN) read_request( thread );
305 else if (event & POLLOUT) write_reply( thread );
306 release_object( thread );
309 static struct list *thread_get_kernel_obj_list( struct object *obj )
311 struct thread *thread = (struct thread *)obj;
312 return &thread->kernel_object;
315 /* cleanup everything that is no longer needed by a dead thread */
316 /* used by destroy_thread and kill_thread */
317 static void cleanup_thread( struct thread *thread )
319 int i;
321 clear_apc_queue( &thread->system_apc );
322 clear_apc_queue( &thread->user_apc );
323 free( thread->req_data );
324 free( thread->reply_data );
325 if (thread->request_fd) release_object( thread->request_fd );
326 if (thread->reply_fd) release_object( thread->reply_fd );
327 if (thread->wait_fd) release_object( thread->wait_fd );
328 free( thread->suspend_context );
329 cleanup_clipboard_thread(thread);
330 destroy_thread_windows( thread );
331 free_msg_queue( thread );
332 close_thread_desktop( thread );
333 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
335 if (thread->inflight[i].client != -1)
337 close( thread->inflight[i].server );
338 thread->inflight[i].client = thread->inflight[i].server = -1;
341 thread->req_data = NULL;
342 thread->reply_data = NULL;
343 thread->request_fd = NULL;
344 thread->reply_fd = NULL;
345 thread->wait_fd = NULL;
346 thread->context = NULL;
347 thread->suspend_context = NULL;
348 thread->desktop = 0;
351 /* destroy a thread when its refcount is 0 */
352 static void destroy_thread( struct object *obj )
354 struct thread *thread = (struct thread *)obj;
355 assert( obj->ops == &thread_ops );
357 assert( !thread->debug_ctx ); /* cannot still be debugging something */
358 list_remove( &thread->entry );
359 cleanup_thread( thread );
360 release_object( thread->process );
361 if (thread->id) free_ptid( thread->id );
362 if (thread->token) release_object( thread->token );
365 /* dump a thread on stdout for debugging purposes */
366 static void dump_thread( struct object *obj, int verbose )
368 struct thread *thread = (struct thread *)obj;
369 assert( obj->ops == &thread_ops );
371 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
372 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
375 static struct object_type *thread_get_type( struct object *obj )
377 static const WCHAR name[] = {'T','h','r','e','a','d'};
378 static const struct unicode_str str = { name, sizeof(name) };
379 return get_object_type( &str );
382 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
384 struct thread *mythread = (struct thread *)obj;
385 return (mythread->state == TERMINATED);
388 static unsigned int thread_map_access( struct object *obj, unsigned int access )
390 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
391 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
392 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
393 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
394 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
396 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
397 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
399 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
402 static void dump_thread_apc( struct object *obj, int verbose )
404 struct thread_apc *apc = (struct thread_apc *)obj;
405 assert( obj->ops == &thread_apc_ops );
407 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
410 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
412 struct thread_apc *apc = (struct thread_apc *)obj;
413 return apc->executed;
416 static void thread_apc_destroy( struct object *obj )
418 struct thread_apc *apc = (struct thread_apc *)obj;
419 if (apc->caller) release_object( apc->caller );
420 if (apc->owner) release_object( apc->owner );
423 /* queue an async procedure call */
424 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
426 struct thread_apc *apc;
428 if ((apc = alloc_object( &thread_apc_ops )))
430 apc->call = *call_data;
431 apc->caller = NULL;
432 apc->owner = owner;
433 apc->executed = 0;
434 apc->result.type = APC_NONE;
435 if (owner) grab_object( owner );
437 return apc;
440 /* get a thread pointer from a thread id (and increment the refcount) */
441 struct thread *get_thread_from_id( thread_id_t id )
443 struct object *obj = get_ptid_entry( id );
445 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
446 set_error( STATUS_INVALID_CID );
447 return NULL;
450 /* get a thread from a handle (and increment the refcount) */
451 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
453 return (struct thread *)get_handle_obj( current->process, handle,
454 access, &thread_ops );
457 /* find a thread from a Unix tid */
458 struct thread *get_thread_from_tid( int tid )
460 struct thread *thread;
462 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
464 if (thread->unix_tid == tid) return thread;
466 return NULL;
469 /* find a thread from a Unix pid */
470 struct thread *get_thread_from_pid( int pid )
472 struct thread *thread;
474 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
476 if (thread->unix_pid == pid) return thread;
478 return NULL;
481 int set_thread_affinity( struct thread *thread, affinity_t affinity )
483 int ret = 0;
484 #ifdef HAVE_SCHED_SETAFFINITY
485 if (thread->unix_tid != -1)
487 cpu_set_t set;
488 int i;
489 affinity_t mask;
491 CPU_ZERO( &set );
492 for (i = 0, mask = 1; mask; i++, mask <<= 1)
493 if (affinity & mask) CPU_SET( i, &set );
495 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
497 #endif
498 if (!ret) thread->affinity = affinity;
499 return ret;
502 affinity_t get_thread_affinity( struct thread *thread )
504 affinity_t mask = 0;
505 #ifdef HAVE_SCHED_SETAFFINITY
506 if (thread->unix_tid != -1)
508 cpu_set_t set;
509 unsigned int i;
511 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
512 for (i = 0; i < 8 * sizeof(mask); i++)
513 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
515 #endif
516 if (!mask) mask = ~(affinity_t)0;
517 return mask;
520 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
521 #define THREAD_PRIORITY_REALTIME_LOWEST -7
523 /* set all information about a thread */
524 static void set_thread_info( struct thread *thread,
525 const struct set_thread_info_request *req )
527 if (req->mask & SET_THREAD_INFO_PRIORITY)
529 int max = THREAD_PRIORITY_HIGHEST;
530 int min = THREAD_PRIORITY_LOWEST;
531 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
533 max = THREAD_PRIORITY_REALTIME_HIGHEST;
534 min = THREAD_PRIORITY_REALTIME_LOWEST;
536 if ((req->priority >= min && req->priority <= max) ||
537 req->priority == THREAD_PRIORITY_IDLE ||
538 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
539 thread->priority = req->priority;
540 else
541 set_error( STATUS_INVALID_PARAMETER );
543 if (req->mask & SET_THREAD_INFO_AFFINITY)
545 if ((req->affinity & thread->process->affinity) != req->affinity)
546 set_error( STATUS_INVALID_PARAMETER );
547 else if (thread->state == TERMINATED)
548 set_error( STATUS_THREAD_IS_TERMINATING );
549 else if (set_thread_affinity( thread, req->affinity ))
550 file_set_error();
552 if (req->mask & SET_THREAD_INFO_TOKEN)
553 security_set_thread_token( thread, req->token );
554 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
555 thread->entry_point = req->entry_point;
558 /* stop a thread (at the Unix level) */
559 void stop_thread( struct thread *thread )
561 if (thread->context) return; /* already inside a debug event, no need for a signal */
562 /* can't stop a thread while initialisation is in progress */
563 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
566 /* stop a thread if it's supposed to be suspended */
567 void stop_thread_if_suspended( struct thread *thread )
569 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
572 /* suspend a thread */
573 int suspend_thread( struct thread *thread )
575 int old_count = thread->suspend;
576 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
578 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
580 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
581 return old_count;
584 /* resume a thread */
585 int resume_thread( struct thread *thread )
587 int old_count = thread->suspend;
588 if (thread->suspend > 0)
590 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
592 return old_count;
595 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
596 int add_queue( struct object *obj, struct wait_queue_entry *entry )
598 grab_object( obj );
599 entry->obj = obj;
600 list_add_tail( &obj->wait_queue, &entry->entry );
601 return 1;
604 /* remove a thread from an object wait queue */
605 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
607 list_remove( &entry->entry );
608 release_object( obj );
611 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
613 return entry->wait->thread;
616 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
618 return entry->wait->select;
621 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
623 return entry->wait->key;
626 void make_wait_abandoned( struct wait_queue_entry *entry )
628 entry->wait->abandoned = 1;
631 /* finish waiting */
632 static unsigned int end_wait( struct thread *thread, unsigned int status )
634 struct thread_wait *wait = thread->wait;
635 struct wait_queue_entry *entry;
636 int i;
638 assert( wait );
639 thread->wait = wait->next;
641 if (status < wait->count) /* wait satisfied, tell it to the objects */
643 if (wait->select == SELECT_WAIT_ALL)
645 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
646 entry->obj->ops->satisfied( entry->obj, entry );
648 else
650 entry = wait->queues + status;
651 entry->obj->ops->satisfied( entry->obj, entry );
653 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
655 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
656 entry->obj->ops->remove_queue( entry->obj, entry );
657 if (wait->user) remove_timeout_user( wait->user );
658 free( wait );
659 return status;
662 /* build the thread wait structure */
663 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
664 int flags, timeout_t timeout )
666 struct thread_wait *wait;
667 struct wait_queue_entry *entry;
668 unsigned int i;
670 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
671 wait->next = current->wait;
672 wait->thread = current;
673 wait->count = count;
674 wait->flags = flags;
675 wait->select = select_op->op;
676 wait->cookie = 0;
677 wait->user = NULL;
678 wait->timeout = timeout;
679 wait->abandoned = 0;
680 current->wait = wait;
682 for (i = 0, entry = wait->queues; i < count; i++, entry++)
684 struct object *obj = objects[i];
685 entry->wait = wait;
686 if (!obj->ops->add_queue( obj, entry ))
688 wait->count = i;
689 end_wait( current, get_error() );
690 return 0;
693 return 1;
696 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
697 int flags, timeout_t timeout )
699 struct object *objects[MAXIMUM_WAIT_OBJECTS];
700 unsigned int i;
701 int ret = 0;
703 assert( count <= MAXIMUM_WAIT_OBJECTS );
705 for (i = 0; i < count; i++)
706 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
707 break;
709 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
711 while (i > 0) release_object( objects[--i] );
712 return ret;
715 /* check if the thread waiting condition is satisfied */
716 static int check_wait( struct thread *thread )
718 int i;
719 struct thread_wait *wait = thread->wait;
720 struct wait_queue_entry *entry;
722 assert( wait );
724 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
725 return STATUS_USER_APC;
727 /* Suspended threads may not acquire locks, but they can run system APCs */
728 if (thread->process->suspend + thread->suspend > 0) return -1;
730 if (wait->select == SELECT_WAIT_ALL)
732 int not_ok = 0;
733 /* Note: we must check them all anyway, as some objects may
734 * want to do something when signaled, even if others are not */
735 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
736 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
737 if (!not_ok) return STATUS_WAIT_0;
739 else
741 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
742 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
745 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
746 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
747 return -1;
750 /* send the wakeup signal to a thread */
751 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
753 struct wake_up_reply reply;
754 int ret;
756 memset( &reply, 0, sizeof(reply) );
757 reply.cookie = cookie;
758 reply.signaled = signaled;
759 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
760 return 0;
761 if (ret >= 0)
762 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
763 else if (errno == EPIPE)
764 kill_thread( thread, 0 ); /* normal death */
765 else
766 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
767 return -1;
770 /* attempt to wake up a thread */
771 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
772 int wake_thread( struct thread *thread )
774 int signaled, count;
775 client_ptr_t cookie;
777 for (count = 0; thread->wait; count++)
779 if ((signaled = check_wait( thread )) == -1) break;
781 cookie = thread->wait->cookie;
782 signaled = end_wait( thread, signaled );
783 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
784 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
786 if (!count) count = -1;
787 break;
790 return count;
793 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
794 int wake_thread_queue_entry( struct wait_queue_entry *entry )
796 struct thread_wait *wait = entry->wait;
797 struct thread *thread = wait->thread;
798 int signaled;
799 client_ptr_t cookie;
801 if (thread->wait != wait) return 0; /* not the current wait */
802 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
804 assert( wait->select != SELECT_WAIT_ALL );
806 cookie = wait->cookie;
807 signaled = end_wait( thread, entry - wait->queues );
808 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
810 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
811 wake_thread( thread ); /* check other waits too */
813 return 1;
816 /* thread wait timeout */
817 static void thread_timeout( void *ptr )
819 struct thread_wait *wait = ptr;
820 struct thread *thread = wait->thread;
821 client_ptr_t cookie = wait->cookie;
823 wait->user = NULL;
824 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
825 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
827 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
828 end_wait( thread, STATUS_TIMEOUT );
830 assert( cookie );
831 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
832 /* check if other objects have become signaled in the meantime */
833 wake_thread( thread );
836 /* try signaling an event flag, a semaphore or a mutex */
837 static int signal_object( obj_handle_t handle )
839 struct object *obj;
840 int ret = 0;
842 obj = get_handle_obj( current->process, handle, 0, NULL );
843 if (obj)
845 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
846 release_object( obj );
848 return ret;
851 /* select on a list of handles */
852 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
853 int flags, timeout_t timeout )
855 int ret;
856 unsigned int count;
857 struct object *object;
859 if (timeout <= 0) timeout = current_time - timeout;
861 switch (select_op->op)
863 case SELECT_NONE:
864 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
865 break;
867 case SELECT_WAIT:
868 case SELECT_WAIT_ALL:
869 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
870 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
872 set_error( STATUS_INVALID_PARAMETER );
873 return 0;
875 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
876 return timeout;
877 break;
879 case SELECT_SIGNAL_AND_WAIT:
880 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
881 return timeout;
882 if (select_op->signal_and_wait.signal)
884 if (!signal_object( select_op->signal_and_wait.signal ))
886 end_wait( current, get_error() );
887 return timeout;
889 /* check if we woke ourselves up */
890 if (!current->wait) return timeout;
892 break;
894 case SELECT_KEYED_EVENT_WAIT:
895 case SELECT_KEYED_EVENT_RELEASE:
896 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
897 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
898 if (!object) return timeout;
899 ret = wait_on( select_op, 1, &object, flags, timeout );
900 release_object( object );
901 if (!ret) return timeout;
902 current->wait->key = select_op->keyed_event.key;
903 break;
905 default:
906 set_error( STATUS_INVALID_PARAMETER );
907 return 0;
910 if ((ret = check_wait( current )) != -1)
912 /* condition is already satisfied */
913 set_error( end_wait( current, ret ));
914 return timeout;
917 /* now we need to wait */
918 if (current->wait->timeout != TIMEOUT_INFINITE)
920 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
921 thread_timeout, current->wait )))
923 end_wait( current, get_error() );
924 return timeout;
927 current->wait->cookie = cookie;
928 set_error( STATUS_PENDING );
929 return timeout;
932 /* attempt to wake threads sleeping on the object wait queue */
933 void wake_up( struct object *obj, int max )
935 struct list *ptr;
936 int ret;
938 LIST_FOR_EACH( ptr, &obj->wait_queue )
940 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
941 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
942 if (ret > 0 && max && !--max) break;
943 /* restart at the head of the list since a wake up can change the object wait queue */
944 ptr = &obj->wait_queue;
948 /* return the apc queue to use for a given apc type */
949 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
951 switch(type)
953 case APC_NONE:
954 case APC_USER:
955 case APC_TIMER:
956 return &thread->user_apc;
957 default:
958 return &thread->system_apc;
962 /* check if thread is currently waiting for a (system) apc */
963 static inline int is_in_apc_wait( struct thread *thread )
965 return (thread->process->suspend || thread->suspend ||
966 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
969 /* queue an existing APC to a given thread */
970 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
972 struct list *queue;
974 if (thread && thread->state == TERMINATED && process)
975 thread = NULL;
977 if (!thread) /* find a suitable thread inside the process */
979 struct thread *candidate;
981 /* first try to find a waiting thread */
982 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
984 if (candidate->state == TERMINATED) continue;
985 if (is_in_apc_wait( candidate ))
987 thread = candidate;
988 break;
991 if (!thread)
993 /* then use the first one that accepts a signal */
994 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
996 if (send_thread_signal( candidate, SIGUSR1 ))
998 thread = candidate;
999 break;
1003 if (!thread) return 0; /* nothing found */
1004 queue = get_apc_queue( thread, apc->call.type );
1006 else
1008 if (thread->state == TERMINATED) return 0;
1009 queue = get_apc_queue( thread, apc->call.type );
1010 /* send signal for system APCs if needed */
1011 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1013 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1015 /* cancel a possible previous APC with the same owner */
1016 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1019 grab_object( apc );
1020 list_add_tail( queue, &apc->entry );
1021 if (!list_prev( queue, &apc->entry )) /* first one */
1022 wake_thread( thread );
1024 return 1;
1027 /* queue an async procedure call */
1028 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1030 struct thread_apc *apc;
1031 int ret = 0;
1033 if ((apc = create_apc( owner, call_data )))
1035 ret = queue_apc( process, thread, apc );
1036 release_object( apc );
1038 return ret;
1041 /* cancel the async procedure call owned by a specific object */
1042 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1044 struct thread_apc *apc;
1045 struct list *queue = get_apc_queue( thread, type );
1047 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1049 if (apc->owner != owner) continue;
1050 list_remove( &apc->entry );
1051 apc->executed = 1;
1052 wake_up( &apc->obj, 0 );
1053 release_object( apc );
1054 return;
1058 /* remove the head apc from the queue; the returned object must be released by the caller */
1059 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1061 struct thread_apc *apc = NULL;
1062 struct list *ptr = list_head( &thread->system_apc );
1064 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1065 if (ptr)
1067 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1068 list_remove( ptr );
1070 return apc;
1073 /* clear an APC queue, cancelling all the APCs on it */
1074 static void clear_apc_queue( struct list *queue )
1076 struct list *ptr;
1078 while ((ptr = list_head( queue )))
1080 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1081 list_remove( &apc->entry );
1082 apc->executed = 1;
1083 wake_up( &apc->obj, 0 );
1084 release_object( apc );
1088 /* add an fd to the inflight list */
1089 /* return list index, or -1 on error */
1090 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1092 int i;
1094 if (server == -1) return -1;
1095 if (client == -1)
1097 close( server );
1098 return -1;
1101 /* first check if we already have an entry for this fd */
1102 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1103 if (thread->inflight[i].client == client)
1105 close( thread->inflight[i].server );
1106 thread->inflight[i].server = server;
1107 return i;
1110 /* now find a free spot to store it */
1111 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1112 if (thread->inflight[i].client == -1)
1114 thread->inflight[i].client = client;
1115 thread->inflight[i].server = server;
1116 return i;
1119 close( server );
1120 return -1;
1123 /* get an inflight fd and purge it from the list */
1124 /* the fd must be closed when no longer used */
1125 int thread_get_inflight_fd( struct thread *thread, int client )
1127 int i, ret;
1129 if (client == -1) return -1;
1133 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1135 if (thread->inflight[i].client == client)
1137 ret = thread->inflight[i].server;
1138 thread->inflight[i].server = thread->inflight[i].client = -1;
1139 return ret;
1142 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1143 return -1;
1146 /* kill a thread on the spot */
1147 void kill_thread( struct thread *thread, int violent_death )
1149 if (thread->state == TERMINATED) return; /* already killed */
1150 thread->state = TERMINATED;
1151 thread->exit_time = current_time;
1152 if (current == thread) current = NULL;
1153 if (debug_level)
1154 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1155 thread->id, thread->exit_code );
1156 if (thread->wait)
1158 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1159 send_thread_wakeup( thread, 0, thread->exit_code );
1160 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1161 violent_death = 0;
1163 kill_console_processes( thread, 0 );
1164 debug_exit_thread( thread );
1165 abandon_mutexes( thread );
1166 wake_up( &thread->obj, 0 );
1167 if (violent_death) send_thread_signal( thread, SIGQUIT );
1168 cleanup_thread( thread );
1169 remove_process_thread( thread->process, thread );
1170 release_object( thread );
1173 /* copy parts of a context structure */
1174 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1176 assert( to->cpu == from->cpu );
1177 to->flags |= flags;
1178 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1179 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1180 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1181 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1182 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1183 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1186 /* return the context flags that correspond to system regs */
1187 /* (system regs are the ones we can't access on the client side) */
1188 static unsigned int get_context_system_regs( enum cpu_type cpu )
1190 switch (cpu)
1192 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1193 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1194 case CPU_POWERPC: return 0;
1195 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1196 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1198 return 0;
1201 /* trigger a breakpoint event in a given thread */
1202 void break_thread( struct thread *thread )
1204 debug_event_t data;
1206 assert( thread->context );
1208 memset( &data, 0, sizeof(data) );
1209 data.exception.first = 1;
1210 data.exception.exc_code = STATUS_BREAKPOINT;
1211 data.exception.flags = EXCEPTION_CONTINUABLE;
1212 switch (thread->context->cpu)
1214 case CPU_x86:
1215 data.exception.address = thread->context->ctl.i386_regs.eip;
1216 break;
1217 case CPU_x86_64:
1218 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1219 break;
1220 case CPU_POWERPC:
1221 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1222 break;
1223 case CPU_ARM:
1224 data.exception.address = thread->context->ctl.arm_regs.pc;
1225 break;
1226 case CPU_ARM64:
1227 data.exception.address = thread->context->ctl.arm64_regs.pc;
1228 break;
1230 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1231 thread->debug_break = 0;
1234 /* take a snapshot of currently running threads */
1235 struct thread_snapshot *thread_snap( int *count )
1237 struct thread_snapshot *snapshot, *ptr;
1238 struct thread *thread;
1239 int total = 0;
1241 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1242 if (thread->state != TERMINATED) total++;
1243 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1244 ptr = snapshot;
1245 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1247 if (thread->state == TERMINATED) continue;
1248 ptr->thread = thread;
1249 ptr->count = thread->obj.refcount;
1250 ptr->priority = thread->priority;
1251 grab_object( thread );
1252 ptr++;
1254 *count = total;
1255 return snapshot;
1258 /* gets the current impersonation token */
1259 struct token *thread_get_impersonation_token( struct thread *thread )
1261 if (thread->token)
1262 return thread->token;
1263 else
1264 return thread->process->token;
1267 /* check if a cpu type can be supported on this server */
1268 int is_cpu_supported( enum cpu_type cpu )
1270 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1272 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1273 if (!(supported_cpus & prefix_cpu_mask))
1274 set_error( STATUS_NOT_SUPPORTED );
1275 else if (supported_cpus & CPU_FLAG(cpu))
1276 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1277 else
1278 set_error( STATUS_INVALID_IMAGE_FORMAT );
1279 return 0;
1282 /* return the cpu mask for supported cpus */
1283 unsigned int get_supported_cpu_mask(void)
1285 return supported_cpus & get_prefix_cpu_mask();
1288 /* create a new thread */
1289 DECL_HANDLER(new_thread)
1291 struct thread *thread;
1292 struct process *process;
1293 struct unicode_str name;
1294 const struct security_descriptor *sd;
1295 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1296 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1298 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1300 if (request_fd != -1) close( request_fd );
1301 return;
1304 if (process != current->process)
1306 if (request_fd != -1) /* can't create a request fd in a different process */
1308 close( request_fd );
1309 set_error( STATUS_INVALID_PARAMETER );
1310 goto done;
1312 if (process->running_threads) /* only the initial thread can be created in another process */
1314 set_error( STATUS_ACCESS_DENIED );
1315 goto done;
1318 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1320 if (request_fd != -1) close( request_fd );
1321 set_error( STATUS_INVALID_HANDLE );
1322 goto done;
1325 if ((thread = create_thread( request_fd, process, sd )))
1327 thread->system_regs = current->system_regs;
1328 if (req->suspend) thread->suspend++;
1329 reply->tid = get_thread_id( thread );
1330 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1331 req->access, objattr->attributes )))
1333 /* thread object will be released when the thread gets killed */
1334 goto done;
1336 kill_thread( thread, 1 );
1338 done:
1339 release_object( process );
1342 /* initialize a new thread */
1343 DECL_HANDLER(init_thread)
1345 struct process *process = current->process;
1346 int wait_fd, reply_fd;
1348 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1350 set_error( STATUS_TOO_MANY_OPENED_FILES );
1351 return;
1353 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1355 set_error( STATUS_TOO_MANY_OPENED_FILES );
1356 goto error;
1359 if (current->reply_fd) /* already initialised */
1361 set_error( STATUS_INVALID_PARAMETER );
1362 goto error;
1365 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1367 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1368 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1369 if (!current->reply_fd || !current->wait_fd) return;
1371 if (!is_valid_address(req->teb))
1373 set_error( STATUS_INVALID_PARAMETER );
1374 return;
1377 current->unix_pid = req->unix_pid;
1378 current->unix_tid = req->unix_tid;
1379 current->teb = req->teb;
1380 current->entry_point = process->peb ? req->entry : 0;
1382 if (!process->peb) /* first thread, initialize the process too */
1384 if (!is_cpu_supported( req->cpu )) return;
1385 process->unix_pid = current->unix_pid;
1386 process->peb = req->entry;
1387 process->cpu = req->cpu;
1388 reply->info_size = init_process( current );
1389 if (!process->parent_id)
1390 process->affinity = current->affinity = get_thread_affinity( current );
1391 else
1392 set_thread_affinity( current, current->affinity );
1394 else
1396 if (req->cpu != process->cpu)
1398 set_error( STATUS_INVALID_PARAMETER );
1399 return;
1401 if (process->unix_pid != current->unix_pid)
1402 process->unix_pid = -1; /* can happen with linuxthreads */
1403 init_thread_context( current );
1404 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1405 set_thread_affinity( current, current->affinity );
1407 debug_level = max( debug_level, req->debug_level );
1409 reply->pid = get_process_id( process );
1410 reply->tid = get_thread_id( current );
1411 reply->version = SERVER_PROTOCOL_VERSION;
1412 reply->server_start = server_start_time;
1413 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1414 reply->suspend = (current->suspend || process->suspend);
1415 return;
1417 error:
1418 if (reply_fd != -1) close( reply_fd );
1419 if (wait_fd != -1) close( wait_fd );
1422 /* terminate a thread */
1423 DECL_HANDLER(terminate_thread)
1425 struct thread *thread;
1427 reply->self = 0;
1428 reply->last = 0;
1429 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1431 thread->exit_code = req->exit_code;
1432 if (thread != current) kill_thread( thread, 1 );
1433 else
1435 reply->self = 1;
1436 reply->last = (thread->process->running_threads == 1);
1438 release_object( thread );
1442 /* open a handle to a thread */
1443 DECL_HANDLER(open_thread)
1445 struct thread *thread = get_thread_from_id( req->tid );
1447 reply->handle = 0;
1448 if (thread)
1450 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1451 release_object( thread );
1455 /* fetch information about a thread */
1456 DECL_HANDLER(get_thread_info)
1458 struct thread *thread;
1459 obj_handle_t handle = req->handle;
1461 if (!handle) thread = get_thread_from_id( req->tid_in );
1462 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1464 if (thread)
1466 reply->pid = get_process_id( thread->process );
1467 reply->tid = get_thread_id( thread );
1468 reply->teb = thread->teb;
1469 reply->entry_point = thread->entry_point;
1470 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1471 reply->priority = thread->priority;
1472 reply->affinity = thread->affinity;
1473 reply->last = thread->process->running_threads == 1;
1475 release_object( thread );
1479 /* fetch information about thread times */
1480 DECL_HANDLER(get_thread_times)
1482 struct thread *thread;
1484 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1486 reply->creation_time = thread->creation_time;
1487 reply->exit_time = thread->exit_time;
1489 release_object( thread );
1493 /* set information about a thread */
1494 DECL_HANDLER(set_thread_info)
1496 struct thread *thread;
1498 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1500 set_thread_info( thread, req );
1501 release_object( thread );
1505 /* suspend a thread */
1506 DECL_HANDLER(suspend_thread)
1508 struct thread *thread;
1510 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1512 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1513 else reply->count = suspend_thread( thread );
1514 release_object( thread );
1518 /* resume a thread */
1519 DECL_HANDLER(resume_thread)
1521 struct thread *thread;
1523 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1525 reply->count = resume_thread( thread );
1526 release_object( thread );
1530 /* select on a handle list */
1531 DECL_HANDLER(select)
1533 select_op_t select_op;
1534 data_size_t op_size;
1535 struct thread_apc *apc;
1536 const apc_result_t *result = get_req_data();
1538 if (get_req_data_size() < sizeof(*result))
1540 set_error( STATUS_INVALID_PARAMETER );
1541 return;
1543 if (!req->cookie)
1545 set_error( STATUS_INVALID_PARAMETER );
1546 return;
1549 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1550 memset( &select_op, 0, sizeof(select_op) );
1551 memcpy( &select_op, result + 1, op_size );
1553 /* first store results of previous apc */
1554 if (req->prev_apc)
1556 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1557 0, &thread_apc_ops ))) return;
1558 apc->result = *result;
1559 apc->executed = 1;
1560 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1562 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1563 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1564 close_handle( current->process, apc->result.create_thread.handle );
1565 apc->result.create_thread.handle = handle;
1566 clear_error(); /* ignore errors from the above calls */
1568 else if (apc->result.type == APC_ASYNC_IO)
1570 if (apc->owner)
1571 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1573 wake_up( &apc->obj, 0 );
1574 close_handle( current->process, req->prev_apc );
1575 release_object( apc );
1578 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1580 if (get_error() == STATUS_USER_APC)
1582 for (;;)
1584 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1585 break;
1586 /* Optimization: ignore APC_NONE calls, they are only used to
1587 * wake up a thread, but since we got here the thread woke up already.
1589 if (apc->call.type != APC_NONE &&
1590 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1592 reply->call = apc->call;
1593 release_object( apc );
1594 break;
1596 apc->executed = 1;
1597 wake_up( &apc->obj, 0 );
1598 release_object( apc );
1603 /* queue an APC for a thread or process */
1604 DECL_HANDLER(queue_apc)
1606 struct thread *thread = NULL;
1607 struct process *process = NULL;
1608 struct thread_apc *apc;
1610 if (!(apc = create_apc( NULL, &req->call ))) return;
1612 switch (apc->call.type)
1614 case APC_NONE:
1615 case APC_USER:
1616 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1617 break;
1618 case APC_VIRTUAL_ALLOC:
1619 case APC_VIRTUAL_FREE:
1620 case APC_VIRTUAL_PROTECT:
1621 case APC_VIRTUAL_FLUSH:
1622 case APC_VIRTUAL_LOCK:
1623 case APC_VIRTUAL_UNLOCK:
1624 case APC_UNMAP_VIEW:
1625 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1626 break;
1627 case APC_VIRTUAL_QUERY:
1628 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1629 break;
1630 case APC_MAP_VIEW:
1631 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1632 if (process && process != current->process)
1634 /* duplicate the handle into the target process */
1635 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1636 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1637 if (handle) apc->call.map_view.handle = handle;
1638 else
1640 release_object( process );
1641 process = NULL;
1644 break;
1645 case APC_CREATE_THREAD:
1646 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1647 break;
1648 default:
1649 set_error( STATUS_INVALID_PARAMETER );
1650 break;
1653 if (thread)
1655 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1656 release_object( thread );
1658 else if (process)
1660 reply->self = (process == current->process);
1661 if (!reply->self)
1663 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1664 if (handle)
1666 if (queue_apc( process, NULL, apc ))
1668 apc->caller = (struct thread *)grab_object( current );
1669 reply->handle = handle;
1671 else
1673 close_handle( current->process, handle );
1674 set_error( STATUS_PROCESS_IS_TERMINATING );
1678 release_object( process );
1681 release_object( apc );
1684 /* Get the result of an APC call */
1685 DECL_HANDLER(get_apc_result)
1687 struct thread_apc *apc;
1689 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1690 0, &thread_apc_ops ))) return;
1692 if (apc->executed) reply->result = apc->result;
1693 else set_error( STATUS_PENDING );
1695 /* close the handle directly to avoid an extra round-trip */
1696 close_handle( current->process, req->handle );
1697 release_object( apc );
1700 /* retrieve the current context of a thread */
1701 DECL_HANDLER(get_thread_context)
1703 struct thread *thread;
1704 context_t *context;
1706 if (get_reply_max_size() < sizeof(context_t))
1708 set_error( STATUS_INVALID_PARAMETER );
1709 return;
1711 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1712 reply->self = (thread == current);
1714 if (thread != current && !thread->context)
1716 /* thread is not suspended, retry (if it's still running) */
1717 if (thread->state == RUNNING)
1719 set_error( STATUS_PENDING );
1720 if (req->suspend)
1722 release_object( thread );
1723 /* make sure we have suspend access */
1724 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1725 suspend_thread( thread );
1728 else set_error( STATUS_UNSUCCESSFUL );
1730 else if ((context = set_reply_data_size( sizeof(context_t) )))
1732 unsigned int flags = get_context_system_regs( thread->process->cpu );
1734 memset( context, 0, sizeof(context_t) );
1735 context->cpu = thread->process->cpu;
1736 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1737 if (req->flags & flags) get_thread_context( thread, context, req->flags & flags );
1739 release_object( thread );
1742 /* set the current context of a thread */
1743 DECL_HANDLER(set_thread_context)
1745 struct thread *thread;
1746 const context_t *context = get_req_data();
1748 if (get_req_data_size() < sizeof(context_t))
1750 set_error( STATUS_INVALID_PARAMETER );
1751 return;
1753 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1754 reply->self = (thread == current);
1756 if (thread != current && !thread->context)
1758 /* thread is not suspended, retry (if it's still running) */
1759 if (thread->state == RUNNING)
1761 set_error( STATUS_PENDING );
1762 if (req->suspend)
1764 release_object( thread );
1765 /* make sure we have suspend access */
1766 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1767 suspend_thread( thread );
1770 else set_error( STATUS_UNSUCCESSFUL );
1772 else if (context->cpu == thread->process->cpu)
1774 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1775 unsigned int client_flags = context->flags & ~system_flags;
1777 if (system_flags) set_thread_context( thread, context, system_flags );
1778 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1780 else set_error( STATUS_INVALID_PARAMETER );
1782 release_object( thread );
1785 /* retrieve the suspended context of a thread */
1786 DECL_HANDLER(get_suspend_context)
1788 if (get_reply_max_size() < sizeof(context_t))
1790 set_error( STATUS_INVALID_PARAMETER );
1791 return;
1794 if (current->suspend_context)
1796 if (current->suspend_context->flags)
1797 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1798 else
1799 free( current->suspend_context );
1800 if (current->context == current->suspend_context)
1802 current->context = NULL;
1803 stop_thread_if_suspended( current );
1805 current->suspend_context = NULL;
1807 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1810 /* store the suspended context of a thread */
1811 DECL_HANDLER(set_suspend_context)
1813 const context_t *context = get_req_data();
1815 if (get_req_data_size() < sizeof(context_t))
1817 set_error( STATUS_INVALID_PARAMETER );
1818 return;
1821 if (current->context || context->cpu != current->process->cpu)
1823 /* nested suspend or exception, shouldn't happen */
1824 set_error( STATUS_INVALID_PARAMETER );
1826 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1828 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1829 current->suspend_context->flags = 0; /* to keep track of what is modified */
1830 current->context = current->suspend_context;
1831 if (current->debug_break) break_thread( current );
1835 /* fetch a selector entry for a thread */
1836 DECL_HANDLER(get_selector_entry)
1838 struct thread *thread;
1839 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1841 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1842 release_object( thread );