shlwapi: SHMapHandle should not set error when NULL is passed as hShared.
[wine.git] / server / thread.c
blobf5f98ebef1bb063d8518689f5a069e00b8fd9f8e
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 void destroy_thread( struct object *obj );
138 static const struct object_ops thread_ops =
140 sizeof(struct thread), /* size */
141 dump_thread, /* dump */
142 thread_get_type, /* get_type */
143 add_queue, /* add_queue */
144 remove_queue, /* remove_queue */
145 thread_signaled, /* signaled */
146 no_satisfied, /* satisfied */
147 no_signal, /* signal */
148 no_get_fd, /* get_fd */
149 thread_map_access, /* map_access */
150 default_get_sd, /* get_sd */
151 default_set_sd, /* set_sd */
152 no_lookup_name, /* lookup_name */
153 no_link_name, /* link_name */
154 NULL, /* unlink_name */
155 no_open_file, /* open_file */
156 no_kernel_obj_list, /* get_kernel_obj_list */
157 no_close_handle, /* close_handle */
158 destroy_thread /* destroy */
161 static const struct fd_ops thread_fd_ops =
163 NULL, /* get_poll_events */
164 thread_poll_event, /* poll_event */
165 NULL, /* flush */
166 NULL, /* get_fd_type */
167 NULL, /* ioctl */
168 NULL, /* queue_async */
169 NULL /* reselect_async */
172 static struct list thread_list = LIST_INIT(thread_list);
174 /* initialize the structure for a newly allocated thread */
175 static inline void init_thread_structure( struct thread *thread )
177 int i;
179 thread->unix_pid = -1; /* not known yet */
180 thread->unix_tid = -1; /* not known yet */
181 thread->context = NULL;
182 thread->suspend_context = NULL;
183 thread->teb = 0;
184 thread->entry_point = 0;
185 thread->debug_ctx = NULL;
186 thread->debug_event = NULL;
187 thread->debug_break = 0;
188 thread->system_regs = 0;
189 thread->queue = NULL;
190 thread->wait = NULL;
191 thread->error = 0;
192 thread->req_data = NULL;
193 thread->req_toread = 0;
194 thread->reply_data = NULL;
195 thread->reply_towrite = 0;
196 thread->request_fd = NULL;
197 thread->reply_fd = NULL;
198 thread->wait_fd = NULL;
199 thread->state = RUNNING;
200 thread->exit_code = 0;
201 thread->priority = 0;
202 thread->suspend = 0;
203 thread->desktop_users = 0;
204 thread->token = NULL;
206 thread->creation_time = current_time;
207 thread->exit_time = 0;
209 list_init( &thread->mutex_list );
210 list_init( &thread->system_apc );
211 list_init( &thread->user_apc );
213 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
214 thread->inflight[i].server = thread->inflight[i].client = -1;
217 /* check if address looks valid for a client-side data structure (TEB etc.) */
218 static inline int is_valid_address( client_ptr_t addr )
220 return addr && !(addr % sizeof(int));
223 /* create a new thread */
224 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
226 struct thread *thread;
227 int request_pipe[2];
229 if (fd == -1)
231 if (pipe( request_pipe ) == -1)
233 file_set_error();
234 return NULL;
236 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
238 close( request_pipe[0] );
239 close( request_pipe[1] );
240 return NULL;
242 close( request_pipe[1] );
243 fd = request_pipe[0];
246 if (process->is_terminating)
248 close( fd );
249 set_error( STATUS_PROCESS_IS_TERMINATING );
250 return NULL;
253 if (!(thread = alloc_object( &thread_ops )))
255 close( fd );
256 return NULL;
259 init_thread_structure( thread );
261 thread->process = (struct process *)grab_object( process );
262 thread->desktop = process->desktop;
263 thread->affinity = process->affinity;
264 if (!current) current = thread;
266 list_add_head( &thread_list, &thread->entry );
268 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
269 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
270 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
271 process->token ))
273 close( fd );
274 release_object( thread );
275 return NULL;
277 if (!(thread->id = alloc_ptid( thread )))
279 close( fd );
280 release_object( thread );
281 return NULL;
283 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
285 release_object( thread );
286 return NULL;
289 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
290 add_process_thread( thread->process, thread );
291 return thread;
294 /* handle a client event */
295 static void thread_poll_event( struct fd *fd, int event )
297 struct thread *thread = get_fd_user( fd );
298 assert( thread->obj.ops == &thread_ops );
300 grab_object( thread );
301 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
302 else if (event & POLLIN) read_request( thread );
303 else if (event & POLLOUT) write_reply( thread );
304 release_object( thread );
307 /* cleanup everything that is no longer needed by a dead thread */
308 /* used by destroy_thread and kill_thread */
309 static void cleanup_thread( struct thread *thread )
311 int i;
313 clear_apc_queue( &thread->system_apc );
314 clear_apc_queue( &thread->user_apc );
315 free( thread->req_data );
316 free( thread->reply_data );
317 if (thread->request_fd) release_object( thread->request_fd );
318 if (thread->reply_fd) release_object( thread->reply_fd );
319 if (thread->wait_fd) release_object( thread->wait_fd );
320 free( thread->suspend_context );
321 cleanup_clipboard_thread(thread);
322 destroy_thread_windows( thread );
323 free_msg_queue( thread );
324 close_thread_desktop( thread );
325 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
327 if (thread->inflight[i].client != -1)
329 close( thread->inflight[i].server );
330 thread->inflight[i].client = thread->inflight[i].server = -1;
333 thread->req_data = NULL;
334 thread->reply_data = NULL;
335 thread->request_fd = NULL;
336 thread->reply_fd = NULL;
337 thread->wait_fd = NULL;
338 thread->context = NULL;
339 thread->suspend_context = NULL;
340 thread->desktop = 0;
343 /* destroy a thread when its refcount is 0 */
344 static void destroy_thread( struct object *obj )
346 struct thread *thread = (struct thread *)obj;
347 assert( obj->ops == &thread_ops );
349 assert( !thread->debug_ctx ); /* cannot still be debugging something */
350 list_remove( &thread->entry );
351 cleanup_thread( thread );
352 release_object( thread->process );
353 if (thread->id) free_ptid( thread->id );
354 if (thread->token) release_object( thread->token );
357 /* dump a thread on stdout for debugging purposes */
358 static void dump_thread( struct object *obj, int verbose )
360 struct thread *thread = (struct thread *)obj;
361 assert( obj->ops == &thread_ops );
363 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
364 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
367 static struct object_type *thread_get_type( struct object *obj )
369 static const WCHAR name[] = {'T','h','r','e','a','d'};
370 static const struct unicode_str str = { name, sizeof(name) };
371 return get_object_type( &str );
374 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
376 struct thread *mythread = (struct thread *)obj;
377 return (mythread->state == TERMINATED);
380 static unsigned int thread_map_access( struct object *obj, unsigned int access )
382 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
383 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
384 THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
385 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
386 if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
388 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
389 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
391 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
394 static void dump_thread_apc( struct object *obj, int verbose )
396 struct thread_apc *apc = (struct thread_apc *)obj;
397 assert( obj->ops == &thread_apc_ops );
399 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
402 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
404 struct thread_apc *apc = (struct thread_apc *)obj;
405 return apc->executed;
408 static void thread_apc_destroy( struct object *obj )
410 struct thread_apc *apc = (struct thread_apc *)obj;
411 if (apc->caller) release_object( apc->caller );
412 if (apc->owner) release_object( apc->owner );
415 /* queue an async procedure call */
416 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
418 struct thread_apc *apc;
420 if ((apc = alloc_object( &thread_apc_ops )))
422 apc->call = *call_data;
423 apc->caller = NULL;
424 apc->owner = owner;
425 apc->executed = 0;
426 apc->result.type = APC_NONE;
427 if (owner) grab_object( owner );
429 return apc;
432 /* get a thread pointer from a thread id (and increment the refcount) */
433 struct thread *get_thread_from_id( thread_id_t id )
435 struct object *obj = get_ptid_entry( id );
437 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
438 set_error( STATUS_INVALID_CID );
439 return NULL;
442 /* get a thread from a handle (and increment the refcount) */
443 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
445 return (struct thread *)get_handle_obj( current->process, handle,
446 access, &thread_ops );
449 /* find a thread from a Unix tid */
450 struct thread *get_thread_from_tid( int tid )
452 struct thread *thread;
454 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
456 if (thread->unix_tid == tid) return thread;
458 return NULL;
461 /* find a thread from a Unix pid */
462 struct thread *get_thread_from_pid( int pid )
464 struct thread *thread;
466 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
468 if (thread->unix_pid == pid) return thread;
470 return NULL;
473 int set_thread_affinity( struct thread *thread, affinity_t affinity )
475 int ret = 0;
476 #ifdef HAVE_SCHED_SETAFFINITY
477 if (thread->unix_tid != -1)
479 cpu_set_t set;
480 int i;
481 affinity_t mask;
483 CPU_ZERO( &set );
484 for (i = 0, mask = 1; mask; i++, mask <<= 1)
485 if (affinity & mask) CPU_SET( i, &set );
487 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
489 #endif
490 if (!ret) thread->affinity = affinity;
491 return ret;
494 affinity_t get_thread_affinity( struct thread *thread )
496 affinity_t mask = 0;
497 #ifdef HAVE_SCHED_SETAFFINITY
498 if (thread->unix_tid != -1)
500 cpu_set_t set;
501 unsigned int i;
503 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
504 for (i = 0; i < 8 * sizeof(mask); i++)
505 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
507 #endif
508 if (!mask) mask = ~(affinity_t)0;
509 return mask;
512 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
513 #define THREAD_PRIORITY_REALTIME_LOWEST -7
515 /* set all information about a thread */
516 static void set_thread_info( struct thread *thread,
517 const struct set_thread_info_request *req )
519 if (req->mask & SET_THREAD_INFO_PRIORITY)
521 int max = THREAD_PRIORITY_HIGHEST;
522 int min = THREAD_PRIORITY_LOWEST;
523 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
525 max = THREAD_PRIORITY_REALTIME_HIGHEST;
526 min = THREAD_PRIORITY_REALTIME_LOWEST;
528 if ((req->priority >= min && req->priority <= max) ||
529 req->priority == THREAD_PRIORITY_IDLE ||
530 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
531 thread->priority = req->priority;
532 else
533 set_error( STATUS_INVALID_PARAMETER );
535 if (req->mask & SET_THREAD_INFO_AFFINITY)
537 if ((req->affinity & thread->process->affinity) != req->affinity)
538 set_error( STATUS_INVALID_PARAMETER );
539 else if (thread->state == TERMINATED)
540 set_error( STATUS_THREAD_IS_TERMINATING );
541 else if (set_thread_affinity( thread, req->affinity ))
542 file_set_error();
544 if (req->mask & SET_THREAD_INFO_TOKEN)
545 security_set_thread_token( thread, req->token );
546 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
547 thread->entry_point = req->entry_point;
550 /* stop a thread (at the Unix level) */
551 void stop_thread( struct thread *thread )
553 if (thread->context) return; /* already inside a debug event, no need for a signal */
554 /* can't stop a thread while initialisation is in progress */
555 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
558 /* stop a thread if it's supposed to be suspended */
559 void stop_thread_if_suspended( struct thread *thread )
561 if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
564 /* suspend a thread */
565 static int suspend_thread( struct thread *thread )
567 int old_count = thread->suspend;
568 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
570 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
572 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
573 return old_count;
576 /* resume a thread */
577 static int resume_thread( struct thread *thread )
579 int old_count = thread->suspend;
580 if (thread->suspend > 0)
582 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
584 return old_count;
587 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
588 int add_queue( struct object *obj, struct wait_queue_entry *entry )
590 grab_object( obj );
591 entry->obj = obj;
592 list_add_tail( &obj->wait_queue, &entry->entry );
593 return 1;
596 /* remove a thread from an object wait queue */
597 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
599 list_remove( &entry->entry );
600 release_object( obj );
603 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
605 return entry->wait->thread;
608 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
610 return entry->wait->select;
613 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
615 return entry->wait->key;
618 void make_wait_abandoned( struct wait_queue_entry *entry )
620 entry->wait->abandoned = 1;
623 /* finish waiting */
624 static unsigned int end_wait( struct thread *thread, unsigned int status )
626 struct thread_wait *wait = thread->wait;
627 struct wait_queue_entry *entry;
628 int i;
630 assert( wait );
631 thread->wait = wait->next;
633 if (status < wait->count) /* wait satisfied, tell it to the objects */
635 if (wait->select == SELECT_WAIT_ALL)
637 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
638 entry->obj->ops->satisfied( entry->obj, entry );
640 else
642 entry = wait->queues + status;
643 entry->obj->ops->satisfied( entry->obj, entry );
645 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
647 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
648 entry->obj->ops->remove_queue( entry->obj, entry );
649 if (wait->user) remove_timeout_user( wait->user );
650 free( wait );
651 return status;
654 /* build the thread wait structure */
655 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
656 int flags, timeout_t timeout )
658 struct thread_wait *wait;
659 struct wait_queue_entry *entry;
660 unsigned int i;
662 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
663 wait->next = current->wait;
664 wait->thread = current;
665 wait->count = count;
666 wait->flags = flags;
667 wait->select = select_op->op;
668 wait->cookie = 0;
669 wait->user = NULL;
670 wait->timeout = timeout;
671 wait->abandoned = 0;
672 current->wait = wait;
674 for (i = 0, entry = wait->queues; i < count; i++, entry++)
676 struct object *obj = objects[i];
677 entry->wait = wait;
678 if (!obj->ops->add_queue( obj, entry ))
680 wait->count = i;
681 end_wait( current, get_error() );
682 return 0;
685 return 1;
688 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
689 int flags, timeout_t timeout )
691 struct object *objects[MAXIMUM_WAIT_OBJECTS];
692 unsigned int i;
693 int ret = 0;
695 assert( count <= MAXIMUM_WAIT_OBJECTS );
697 for (i = 0; i < count; i++)
698 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
699 break;
701 if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
703 while (i > 0) release_object( objects[--i] );
704 return ret;
707 /* check if the thread waiting condition is satisfied */
708 static int check_wait( struct thread *thread )
710 int i;
711 struct thread_wait *wait = thread->wait;
712 struct wait_queue_entry *entry;
714 assert( wait );
716 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
717 return STATUS_USER_APC;
719 /* Suspended threads may not acquire locks, but they can run system APCs */
720 if (thread->process->suspend + thread->suspend > 0) return -1;
722 if (wait->select == SELECT_WAIT_ALL)
724 int not_ok = 0;
725 /* Note: we must check them all anyway, as some objects may
726 * want to do something when signaled, even if others are not */
727 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
728 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
729 if (!not_ok) return STATUS_WAIT_0;
731 else
733 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
734 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
737 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
738 if (wait->timeout <= current_time) return STATUS_TIMEOUT;
739 return -1;
742 /* send the wakeup signal to a thread */
743 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
745 struct wake_up_reply reply;
746 int ret;
748 memset( &reply, 0, sizeof(reply) );
749 reply.cookie = cookie;
750 reply.signaled = signaled;
751 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
752 return 0;
753 if (ret >= 0)
754 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
755 else if (errno == EPIPE)
756 kill_thread( thread, 0 ); /* normal death */
757 else
758 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
759 return -1;
762 /* attempt to wake up a thread */
763 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
764 int wake_thread( struct thread *thread )
766 int signaled, count;
767 client_ptr_t cookie;
769 for (count = 0; thread->wait; count++)
771 if ((signaled = check_wait( thread )) == -1) break;
773 cookie = thread->wait->cookie;
774 signaled = end_wait( thread, signaled );
775 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
776 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
778 if (!count) count = -1;
779 break;
782 return count;
785 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
786 int wake_thread_queue_entry( struct wait_queue_entry *entry )
788 struct thread_wait *wait = entry->wait;
789 struct thread *thread = wait->thread;
790 int signaled;
791 client_ptr_t cookie;
793 if (thread->wait != wait) return 0; /* not the current wait */
794 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
796 assert( wait->select != SELECT_WAIT_ALL );
798 cookie = wait->cookie;
799 signaled = end_wait( thread, entry - wait->queues );
800 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
802 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
803 wake_thread( thread ); /* check other waits too */
805 return 1;
808 /* thread wait timeout */
809 static void thread_timeout( void *ptr )
811 struct thread_wait *wait = ptr;
812 struct thread *thread = wait->thread;
813 client_ptr_t cookie = wait->cookie;
815 wait->user = NULL;
816 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
817 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
819 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
820 end_wait( thread, STATUS_TIMEOUT );
822 assert( cookie );
823 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
824 /* check if other objects have become signaled in the meantime */
825 wake_thread( thread );
828 /* try signaling an event flag, a semaphore or a mutex */
829 static int signal_object( obj_handle_t handle )
831 struct object *obj;
832 int ret = 0;
834 obj = get_handle_obj( current->process, handle, 0, NULL );
835 if (obj)
837 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
838 release_object( obj );
840 return ret;
843 /* select on a list of handles */
844 static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
845 int flags, timeout_t timeout )
847 int ret;
848 unsigned int count;
849 struct object *object;
851 if (timeout <= 0) timeout = current_time - timeout;
853 switch (select_op->op)
855 case SELECT_NONE:
856 if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
857 break;
859 case SELECT_WAIT:
860 case SELECT_WAIT_ALL:
861 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
862 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
864 set_error( STATUS_INVALID_PARAMETER );
865 return 0;
867 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
868 return timeout;
869 break;
871 case SELECT_SIGNAL_AND_WAIT:
872 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
873 return timeout;
874 if (select_op->signal_and_wait.signal)
876 if (!signal_object( select_op->signal_and_wait.signal ))
878 end_wait( current, get_error() );
879 return timeout;
881 /* check if we woke ourselves up */
882 if (!current->wait) return timeout;
884 break;
886 case SELECT_KEYED_EVENT_WAIT:
887 case SELECT_KEYED_EVENT_RELEASE:
888 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
889 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
890 if (!object) return timeout;
891 ret = wait_on( select_op, 1, &object, flags, timeout );
892 release_object( object );
893 if (!ret) return timeout;
894 current->wait->key = select_op->keyed_event.key;
895 break;
897 default:
898 set_error( STATUS_INVALID_PARAMETER );
899 return 0;
902 if ((ret = check_wait( current )) != -1)
904 /* condition is already satisfied */
905 set_error( end_wait( current, ret ));
906 return timeout;
909 /* now we need to wait */
910 if (current->wait->timeout != TIMEOUT_INFINITE)
912 if (!(current->wait->user = add_timeout_user( current->wait->timeout,
913 thread_timeout, current->wait )))
915 end_wait( current, get_error() );
916 return timeout;
919 current->wait->cookie = cookie;
920 set_error( STATUS_PENDING );
921 return timeout;
924 /* attempt to wake threads sleeping on the object wait queue */
925 void wake_up( struct object *obj, int max )
927 struct list *ptr;
928 int ret;
930 LIST_FOR_EACH( ptr, &obj->wait_queue )
932 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
933 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
934 if (ret > 0 && max && !--max) break;
935 /* restart at the head of the list since a wake up can change the object wait queue */
936 ptr = &obj->wait_queue;
940 /* return the apc queue to use for a given apc type */
941 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
943 switch(type)
945 case APC_NONE:
946 case APC_USER:
947 case APC_TIMER:
948 return &thread->user_apc;
949 default:
950 return &thread->system_apc;
954 /* check if thread is currently waiting for a (system) apc */
955 static inline int is_in_apc_wait( struct thread *thread )
957 return (thread->process->suspend || thread->suspend ||
958 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
961 /* queue an existing APC to a given thread */
962 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
964 struct list *queue;
966 if (thread && thread->state == TERMINATED && process)
967 thread = NULL;
969 if (!thread) /* find a suitable thread inside the process */
971 struct thread *candidate;
973 /* first try to find a waiting thread */
974 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
976 if (candidate->state == TERMINATED) continue;
977 if (is_in_apc_wait( candidate ))
979 thread = candidate;
980 break;
983 if (!thread)
985 /* then use the first one that accepts a signal */
986 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
988 if (send_thread_signal( candidate, SIGUSR1 ))
990 thread = candidate;
991 break;
995 if (!thread) return 0; /* nothing found */
996 queue = get_apc_queue( thread, apc->call.type );
998 else
1000 if (thread->state == TERMINATED) return 0;
1001 queue = get_apc_queue( thread, apc->call.type );
1002 /* send signal for system APCs if needed */
1003 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1005 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1007 /* cancel a possible previous APC with the same owner */
1008 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1011 grab_object( apc );
1012 list_add_tail( queue, &apc->entry );
1013 if (!list_prev( queue, &apc->entry )) /* first one */
1014 wake_thread( thread );
1016 return 1;
1019 /* queue an async procedure call */
1020 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1022 struct thread_apc *apc;
1023 int ret = 0;
1025 if ((apc = create_apc( owner, call_data )))
1027 ret = queue_apc( process, thread, apc );
1028 release_object( apc );
1030 return ret;
1033 /* cancel the async procedure call owned by a specific object */
1034 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1036 struct thread_apc *apc;
1037 struct list *queue = get_apc_queue( thread, type );
1039 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1041 if (apc->owner != owner) continue;
1042 list_remove( &apc->entry );
1043 apc->executed = 1;
1044 wake_up( &apc->obj, 0 );
1045 release_object( apc );
1046 return;
1050 /* remove the head apc from the queue; the returned object must be released by the caller */
1051 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1053 struct thread_apc *apc = NULL;
1054 struct list *ptr = list_head( &thread->system_apc );
1056 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
1057 if (ptr)
1059 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1060 list_remove( ptr );
1062 return apc;
1065 /* clear an APC queue, cancelling all the APCs on it */
1066 static void clear_apc_queue( struct list *queue )
1068 struct list *ptr;
1070 while ((ptr = list_head( queue )))
1072 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1073 list_remove( &apc->entry );
1074 apc->executed = 1;
1075 wake_up( &apc->obj, 0 );
1076 release_object( apc );
1080 /* add an fd to the inflight list */
1081 /* return list index, or -1 on error */
1082 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1084 int i;
1086 if (server == -1) return -1;
1087 if (client == -1)
1089 close( server );
1090 return -1;
1093 /* first check if we already have an entry for this fd */
1094 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1095 if (thread->inflight[i].client == client)
1097 close( thread->inflight[i].server );
1098 thread->inflight[i].server = server;
1099 return i;
1102 /* now find a free spot to store it */
1103 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1104 if (thread->inflight[i].client == -1)
1106 thread->inflight[i].client = client;
1107 thread->inflight[i].server = server;
1108 return i;
1111 close( server );
1112 return -1;
1115 /* get an inflight fd and purge it from the list */
1116 /* the fd must be closed when no longer used */
1117 int thread_get_inflight_fd( struct thread *thread, int client )
1119 int i, ret;
1121 if (client == -1) return -1;
1125 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1127 if (thread->inflight[i].client == client)
1129 ret = thread->inflight[i].server;
1130 thread->inflight[i].server = thread->inflight[i].client = -1;
1131 return ret;
1134 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1135 return -1;
1138 /* kill a thread on the spot */
1139 void kill_thread( struct thread *thread, int violent_death )
1141 if (thread->state == TERMINATED) return; /* already killed */
1142 thread->state = TERMINATED;
1143 thread->exit_time = current_time;
1144 if (current == thread) current = NULL;
1145 if (debug_level)
1146 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1147 thread->id, thread->exit_code );
1148 if (thread->wait)
1150 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1151 send_thread_wakeup( thread, 0, thread->exit_code );
1152 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1153 violent_death = 0;
1155 kill_console_processes( thread, 0 );
1156 debug_exit_thread( thread );
1157 abandon_mutexes( thread );
1158 wake_up( &thread->obj, 0 );
1159 if (violent_death) send_thread_signal( thread, SIGQUIT );
1160 cleanup_thread( thread );
1161 remove_process_thread( thread->process, thread );
1162 release_object( thread );
1165 /* copy parts of a context structure */
1166 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1168 assert( to->cpu == from->cpu );
1169 to->flags |= flags;
1170 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1171 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1172 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1173 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1174 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1175 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1178 /* return the context flags that correspond to system regs */
1179 /* (system regs are the ones we can't access on the client side) */
1180 static unsigned int get_context_system_regs( enum cpu_type cpu )
1182 switch (cpu)
1184 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1185 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1186 case CPU_POWERPC: return 0;
1187 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1188 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1190 return 0;
1193 /* trigger a breakpoint event in a given thread */
1194 void break_thread( struct thread *thread )
1196 debug_event_t data;
1198 assert( thread->context );
1200 memset( &data, 0, sizeof(data) );
1201 data.exception.first = 1;
1202 data.exception.exc_code = STATUS_BREAKPOINT;
1203 data.exception.flags = EXCEPTION_CONTINUABLE;
1204 switch (thread->context->cpu)
1206 case CPU_x86:
1207 data.exception.address = thread->context->ctl.i386_regs.eip;
1208 break;
1209 case CPU_x86_64:
1210 data.exception.address = thread->context->ctl.x86_64_regs.rip;
1211 break;
1212 case CPU_POWERPC:
1213 data.exception.address = thread->context->ctl.powerpc_regs.iar;
1214 break;
1215 case CPU_ARM:
1216 data.exception.address = thread->context->ctl.arm_regs.pc;
1217 break;
1218 case CPU_ARM64:
1219 data.exception.address = thread->context->ctl.arm64_regs.pc;
1220 break;
1222 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
1223 thread->debug_break = 0;
1226 /* take a snapshot of currently running threads */
1227 struct thread_snapshot *thread_snap( int *count )
1229 struct thread_snapshot *snapshot, *ptr;
1230 struct thread *thread;
1231 int total = 0;
1233 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1234 if (thread->state != TERMINATED) total++;
1235 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
1236 ptr = snapshot;
1237 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1239 if (thread->state == TERMINATED) continue;
1240 ptr->thread = thread;
1241 ptr->count = thread->obj.refcount;
1242 ptr->priority = thread->priority;
1243 grab_object( thread );
1244 ptr++;
1246 *count = total;
1247 return snapshot;
1250 /* gets the current impersonation token */
1251 struct token *thread_get_impersonation_token( struct thread *thread )
1253 if (thread->token)
1254 return thread->token;
1255 else
1256 return thread->process->token;
1259 /* check if a cpu type can be supported on this server */
1260 int is_cpu_supported( enum cpu_type cpu )
1262 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1264 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1265 if (!(supported_cpus & prefix_cpu_mask))
1266 set_error( STATUS_NOT_SUPPORTED );
1267 else if (supported_cpus & CPU_FLAG(cpu))
1268 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1269 else
1270 set_error( STATUS_INVALID_IMAGE_FORMAT );
1271 return 0;
1274 /* return the cpu mask for supported cpus */
1275 unsigned int get_supported_cpu_mask(void)
1277 return supported_cpus & get_prefix_cpu_mask();
1280 /* create a new thread */
1281 DECL_HANDLER(new_thread)
1283 struct thread *thread;
1284 struct process *process;
1285 struct unicode_str name;
1286 const struct security_descriptor *sd;
1287 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1288 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1290 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1292 if (request_fd != -1) close( request_fd );
1293 return;
1296 if (process != current->process)
1298 if (request_fd != -1) /* can't create a request fd in a different process */
1300 close( request_fd );
1301 set_error( STATUS_INVALID_PARAMETER );
1302 goto done;
1304 if (process->running_threads) /* only the initial thread can be created in another process */
1306 set_error( STATUS_ACCESS_DENIED );
1307 goto done;
1310 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1312 if (request_fd != -1) close( request_fd );
1313 set_error( STATUS_INVALID_HANDLE );
1314 goto done;
1317 if ((thread = create_thread( request_fd, process, sd )))
1319 thread->system_regs = current->system_regs;
1320 if (req->suspend) thread->suspend++;
1321 reply->tid = get_thread_id( thread );
1322 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1323 req->access, objattr->attributes )))
1325 /* thread object will be released when the thread gets killed */
1326 goto done;
1328 kill_thread( thread, 1 );
1330 done:
1331 release_object( process );
1334 /* initialize a new thread */
1335 DECL_HANDLER(init_thread)
1337 struct process *process = current->process;
1338 int wait_fd, reply_fd;
1340 if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
1342 set_error( STATUS_TOO_MANY_OPENED_FILES );
1343 return;
1345 if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
1347 set_error( STATUS_TOO_MANY_OPENED_FILES );
1348 goto error;
1351 if (current->reply_fd) /* already initialised */
1353 set_error( STATUS_INVALID_PARAMETER );
1354 goto error;
1357 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1359 current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1360 current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
1361 if (!current->reply_fd || !current->wait_fd) return;
1363 if (!is_valid_address(req->teb))
1365 set_error( STATUS_INVALID_PARAMETER );
1366 return;
1369 current->unix_pid = req->unix_pid;
1370 current->unix_tid = req->unix_tid;
1371 current->teb = req->teb;
1372 current->entry_point = process->peb ? req->entry : 0;
1374 if (!process->peb) /* first thread, initialize the process too */
1376 if (!is_cpu_supported( req->cpu )) return;
1377 process->unix_pid = current->unix_pid;
1378 process->peb = req->entry;
1379 process->cpu = req->cpu;
1380 reply->info_size = init_process( current );
1381 if (!process->parent_id)
1382 process->affinity = current->affinity = get_thread_affinity( current );
1383 else
1384 set_thread_affinity( current, current->affinity );
1386 else
1388 if (req->cpu != process->cpu)
1390 set_error( STATUS_INVALID_PARAMETER );
1391 return;
1393 if (process->unix_pid != current->unix_pid)
1394 process->unix_pid = -1; /* can happen with linuxthreads */
1395 init_thread_context( current );
1396 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1397 set_thread_affinity( current, current->affinity );
1399 debug_level = max( debug_level, req->debug_level );
1401 reply->pid = get_process_id( process );
1402 reply->tid = get_thread_id( current );
1403 reply->version = SERVER_PROTOCOL_VERSION;
1404 reply->server_start = server_start_time;
1405 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1406 reply->suspend = (current->suspend || process->suspend);
1407 return;
1409 error:
1410 if (reply_fd != -1) close( reply_fd );
1411 if (wait_fd != -1) close( wait_fd );
1414 /* terminate a thread */
1415 DECL_HANDLER(terminate_thread)
1417 struct thread *thread;
1419 reply->self = 0;
1420 reply->last = 0;
1421 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1423 thread->exit_code = req->exit_code;
1424 if (thread != current) kill_thread( thread, 1 );
1425 else
1427 reply->self = 1;
1428 reply->last = (thread->process->running_threads == 1);
1430 release_object( thread );
1434 /* open a handle to a thread */
1435 DECL_HANDLER(open_thread)
1437 struct thread *thread = get_thread_from_id( req->tid );
1439 reply->handle = 0;
1440 if (thread)
1442 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1443 release_object( thread );
1447 /* fetch information about a thread */
1448 DECL_HANDLER(get_thread_info)
1450 struct thread *thread;
1451 obj_handle_t handle = req->handle;
1453 if (!handle) thread = get_thread_from_id( req->tid_in );
1454 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1456 if (thread)
1458 reply->pid = get_process_id( thread->process );
1459 reply->tid = get_thread_id( thread );
1460 reply->teb = thread->teb;
1461 reply->entry_point = thread->entry_point;
1462 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1463 reply->priority = thread->priority;
1464 reply->affinity = thread->affinity;
1465 reply->last = thread->process->running_threads == 1;
1467 release_object( thread );
1471 /* fetch information about thread times */
1472 DECL_HANDLER(get_thread_times)
1474 struct thread *thread;
1476 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1478 reply->creation_time = thread->creation_time;
1479 reply->exit_time = thread->exit_time;
1481 release_object( thread );
1485 /* set information about a thread */
1486 DECL_HANDLER(set_thread_info)
1488 struct thread *thread;
1490 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1492 set_thread_info( thread, req );
1493 release_object( thread );
1497 /* suspend a thread */
1498 DECL_HANDLER(suspend_thread)
1500 struct thread *thread;
1502 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1504 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1505 else reply->count = suspend_thread( thread );
1506 release_object( thread );
1510 /* resume a thread */
1511 DECL_HANDLER(resume_thread)
1513 struct thread *thread;
1515 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1517 reply->count = resume_thread( thread );
1518 release_object( thread );
1522 /* select on a handle list */
1523 DECL_HANDLER(select)
1525 select_op_t select_op;
1526 data_size_t op_size;
1527 struct thread_apc *apc;
1528 const apc_result_t *result = get_req_data();
1530 if (get_req_data_size() < sizeof(*result))
1532 set_error( STATUS_INVALID_PARAMETER );
1533 return;
1535 if (!req->cookie)
1537 set_error( STATUS_INVALID_PARAMETER );
1538 return;
1541 op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
1542 memset( &select_op, 0, sizeof(select_op) );
1543 memcpy( &select_op, result + 1, op_size );
1545 /* first store results of previous apc */
1546 if (req->prev_apc)
1548 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1549 0, &thread_apc_ops ))) return;
1550 apc->result = *result;
1551 apc->executed = 1;
1552 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1554 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1555 apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1556 close_handle( current->process, apc->result.create_thread.handle );
1557 apc->result.create_thread.handle = handle;
1558 clear_error(); /* ignore errors from the above calls */
1560 else if (apc->result.type == APC_ASYNC_IO)
1562 if (apc->owner)
1563 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1565 wake_up( &apc->obj, 0 );
1566 close_handle( current->process, req->prev_apc );
1567 release_object( apc );
1570 reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1572 if (get_error() == STATUS_USER_APC)
1574 for (;;)
1576 if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
1577 break;
1578 /* Optimization: ignore APC_NONE calls, they are only used to
1579 * wake up a thread, but since we got here the thread woke up already.
1581 if (apc->call.type != APC_NONE &&
1582 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1584 reply->call = apc->call;
1585 release_object( apc );
1586 break;
1588 apc->executed = 1;
1589 wake_up( &apc->obj, 0 );
1590 release_object( apc );
1595 /* queue an APC for a thread or process */
1596 DECL_HANDLER(queue_apc)
1598 struct thread *thread = NULL;
1599 struct process *process = NULL;
1600 struct thread_apc *apc;
1602 if (!(apc = create_apc( NULL, &req->call ))) return;
1604 switch (apc->call.type)
1606 case APC_NONE:
1607 case APC_USER:
1608 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1609 break;
1610 case APC_VIRTUAL_ALLOC:
1611 case APC_VIRTUAL_FREE:
1612 case APC_VIRTUAL_PROTECT:
1613 case APC_VIRTUAL_FLUSH:
1614 case APC_VIRTUAL_LOCK:
1615 case APC_VIRTUAL_UNLOCK:
1616 case APC_UNMAP_VIEW:
1617 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1618 break;
1619 case APC_VIRTUAL_QUERY:
1620 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1621 break;
1622 case APC_MAP_VIEW:
1623 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1624 if (process && process != current->process)
1626 /* duplicate the handle into the target process */
1627 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1628 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
1629 if (handle) apc->call.map_view.handle = handle;
1630 else
1632 release_object( process );
1633 process = NULL;
1636 break;
1637 case APC_CREATE_THREAD:
1638 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1639 break;
1640 default:
1641 set_error( STATUS_INVALID_PARAMETER );
1642 break;
1645 if (thread)
1647 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
1648 release_object( thread );
1650 else if (process)
1652 reply->self = (process == current->process);
1653 if (!reply->self)
1655 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1656 if (handle)
1658 if (queue_apc( process, NULL, apc ))
1660 apc->caller = (struct thread *)grab_object( current );
1661 reply->handle = handle;
1663 else
1665 close_handle( current->process, handle );
1666 set_error( STATUS_PROCESS_IS_TERMINATING );
1670 release_object( process );
1673 release_object( apc );
1676 /* Get the result of an APC call */
1677 DECL_HANDLER(get_apc_result)
1679 struct thread_apc *apc;
1681 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1682 0, &thread_apc_ops ))) return;
1684 if (apc->executed) reply->result = apc->result;
1685 else set_error( STATUS_PENDING );
1687 /* close the handle directly to avoid an extra round-trip */
1688 close_handle( current->process, req->handle );
1689 release_object( apc );
1692 /* retrieve the current context of a thread */
1693 DECL_HANDLER(get_thread_context)
1695 struct thread *thread;
1696 context_t *context;
1698 if (get_reply_max_size() < sizeof(context_t))
1700 set_error( STATUS_INVALID_PARAMETER );
1701 return;
1703 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1704 reply->self = (thread == current);
1706 if (thread != current && !thread->context)
1708 /* thread is not suspended, retry (if it's still running) */
1709 if (thread->state == RUNNING)
1711 set_error( STATUS_PENDING );
1712 if (req->suspend)
1714 release_object( thread );
1715 /* make sure we have suspend access */
1716 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1717 suspend_thread( thread );
1720 else set_error( STATUS_UNSUCCESSFUL );
1722 else if ((context = set_reply_data_size( sizeof(context_t) )))
1724 unsigned int flags = get_context_system_regs( thread->process->cpu );
1726 memset( context, 0, sizeof(context_t) );
1727 context->cpu = thread->process->cpu;
1728 if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1729 if (req->flags & flags) get_thread_context( thread, context, req->flags & flags );
1731 release_object( thread );
1734 /* set the current context of a thread */
1735 DECL_HANDLER(set_thread_context)
1737 struct thread *thread;
1738 const context_t *context = get_req_data();
1740 if (get_req_data_size() < sizeof(context_t))
1742 set_error( STATUS_INVALID_PARAMETER );
1743 return;
1745 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1746 reply->self = (thread == current);
1748 if (thread != current && !thread->context)
1750 /* thread is not suspended, retry (if it's still running) */
1751 if (thread->state == RUNNING)
1753 set_error( STATUS_PENDING );
1754 if (req->suspend)
1756 release_object( thread );
1757 /* make sure we have suspend access */
1758 if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
1759 suspend_thread( thread );
1762 else set_error( STATUS_UNSUCCESSFUL );
1764 else if (context->cpu == thread->process->cpu)
1766 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1767 unsigned int client_flags = context->flags & ~system_flags;
1769 if (system_flags) set_thread_context( thread, context, system_flags );
1770 if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1772 else set_error( STATUS_INVALID_PARAMETER );
1774 release_object( thread );
1777 /* retrieve the suspended context of a thread */
1778 DECL_HANDLER(get_suspend_context)
1780 if (get_reply_max_size() < sizeof(context_t))
1782 set_error( STATUS_INVALID_PARAMETER );
1783 return;
1786 if (current->suspend_context)
1788 if (current->suspend_context->flags)
1789 set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
1790 else
1791 free( current->suspend_context );
1792 if (current->context == current->suspend_context)
1794 current->context = NULL;
1795 stop_thread_if_suspended( current );
1797 current->suspend_context = NULL;
1799 else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
1802 /* store the suspended context of a thread */
1803 DECL_HANDLER(set_suspend_context)
1805 const context_t *context = get_req_data();
1807 if (get_req_data_size() < sizeof(context_t))
1809 set_error( STATUS_INVALID_PARAMETER );
1810 return;
1813 if (current->context || context->cpu != current->process->cpu)
1815 /* nested suspend or exception, shouldn't happen */
1816 set_error( STATUS_INVALID_PARAMETER );
1818 else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
1820 memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1821 current->suspend_context->flags = 0; /* to keep track of what is modified */
1822 current->context = current->suspend_context;
1823 if (current->debug_break) break_thread( current );
1827 /* fetch a selector entry for a thread */
1828 DECL_HANDLER(get_selector_entry)
1830 struct thread *thread;
1831 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1833 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1834 release_object( thread );