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
31 #include <sys/types.h>
36 /* FreeBSD needs this for cpu_set_t instead of its cpuset_t */
37 #define _WITH_CPU_SET_T
42 #define WIN32_NO_STATUS
59 struct thread_wait
*next
; /* next wait structure for this thread */
60 struct thread
*thread
; /* owner thread */
61 int count
; /* count of objects */
64 enum select_op select
;
65 client_ptr_t key
; /* wait key for keyed events */
66 client_ptr_t cookie
; /* magic cookie to return to client */
68 struct timeout_user
*user
;
69 int status
; /* status to return (unless STATUS_PENDING) */
70 struct wait_queue_entry queues
[1];
73 /* asynchronous procedure calls */
77 struct object obj
; /* object header */
78 struct list entry
; /* queue linked list */
79 struct thread
*caller
; /* thread that queued this apc */
80 struct object
*owner
; /* object that queued this apc */
81 int executed
; /* has it been executed by the client? */
82 apc_call_t call
; /* call arguments */
83 apc_result_t result
; /* call results once executed */
86 static void dump_thread_apc( struct object
*obj
, int verbose
);
87 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
88 static void thread_apc_destroy( struct object
*obj
);
89 static void clear_apc_queue( struct list
*queue
);
91 static const struct object_ops thread_apc_ops
=
93 sizeof(struct thread_apc
), /* size */
95 dump_thread_apc
, /* dump */
96 add_queue
, /* add_queue */
97 remove_queue
, /* remove_queue */
98 thread_apc_signaled
, /* signaled */
99 no_satisfied
, /* satisfied */
100 no_signal
, /* signal */
101 no_get_fd
, /* get_fd */
102 default_map_access
, /* map_access */
103 default_get_sd
, /* get_sd */
104 default_set_sd
, /* set_sd */
105 no_get_full_name
, /* get_full_name */
106 no_lookup_name
, /* lookup_name */
107 no_link_name
, /* link_name */
108 NULL
, /* unlink_name */
109 no_open_file
, /* open_file */
110 no_kernel_obj_list
, /* get_kernel_obj_list */
111 no_close_handle
, /* close_handle */
112 thread_apc_destroy
/* destroy */
116 /* thread CPU context */
120 struct object obj
; /* object header */
121 unsigned int status
; /* status of the context */
122 context_t regs
[3]; /* context data */
124 #define CTX_NATIVE 0 /* context for native machine */
125 #define CTX_WOW 1 /* context if thread is inside WoW */
126 #define CTX_PENDING 2 /* pending native context when we don't know whether thread is inside WoW */
128 /* flags for registers that always need to be set from the server side */
129 static const unsigned int system_flags
= SERVER_CTX_DEBUG_REGISTERS
;
130 /* flags for registers that are set from the native context even in WoW mode */
131 static const unsigned int always_native_flags
= SERVER_CTX_DEBUG_REGISTERS
| SERVER_CTX_FLOATING_POINT
| SERVER_CTX_YMM_REGISTERS
;
133 static void dump_context( struct object
*obj
, int verbose
);
134 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
136 static const struct object_ops context_ops
=
138 sizeof(struct context
), /* size */
140 dump_context
, /* dump */
141 add_queue
, /* add_queue */
142 remove_queue
, /* remove_queue */
143 context_signaled
, /* signaled */
144 no_satisfied
, /* satisfied */
145 no_signal
, /* signal */
146 no_get_fd
, /* get_fd */
147 default_map_access
, /* map_access */
148 default_get_sd
, /* get_sd */
149 default_set_sd
, /* set_sd */
150 no_get_full_name
, /* get_full_name */
151 no_lookup_name
, /* lookup_name */
152 no_link_name
, /* link_name */
153 NULL
, /* unlink_name */
154 no_open_file
, /* open_file */
155 no_kernel_obj_list
, /* get_kernel_obj_list */
156 no_close_handle
, /* close_handle */
157 no_destroy
/* destroy */
161 /* thread operations */
163 static const WCHAR thread_name
[] = {'T','h','r','e','a','d'};
165 struct type_descr thread_type
=
167 { thread_name
, sizeof(thread_name
) }, /* name */
168 THREAD_ALL_ACCESS
, /* valid_access */
170 STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
,
171 STANDARD_RIGHTS_WRITE
| THREAD_SET_LIMITED_INFORMATION
| THREAD_SET_INFORMATION
172 | THREAD_SET_CONTEXT
| THREAD_SUSPEND_RESUME
| THREAD_TERMINATE
| 0x04,
173 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_RESUME
| THREAD_QUERY_LIMITED_INFORMATION
,
178 static void dump_thread( struct object
*obj
, int verbose
);
179 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
180 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
181 static void thread_poll_event( struct fd
*fd
, int event
);
182 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
183 static void destroy_thread( struct object
*obj
);
185 static const struct object_ops thread_ops
=
187 sizeof(struct thread
), /* size */
188 &thread_type
, /* type */
189 dump_thread
, /* dump */
190 add_queue
, /* add_queue */
191 remove_queue
, /* remove_queue */
192 thread_signaled
, /* signaled */
193 no_satisfied
, /* satisfied */
194 no_signal
, /* signal */
195 no_get_fd
, /* get_fd */
196 thread_map_access
, /* map_access */
197 default_get_sd
, /* get_sd */
198 default_set_sd
, /* set_sd */
199 no_get_full_name
, /* get_full_name */
200 no_lookup_name
, /* lookup_name */
201 no_link_name
, /* link_name */
202 NULL
, /* unlink_name */
203 no_open_file
, /* open_file */
204 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
205 no_close_handle
, /* close_handle */
206 destroy_thread
/* destroy */
209 static const struct fd_ops thread_fd_ops
=
211 NULL
, /* get_poll_events */
212 thread_poll_event
, /* poll_event */
214 NULL
, /* get_fd_type */
216 NULL
, /* queue_async */
217 NULL
/* reselect_async */
220 static struct list thread_list
= LIST_INIT(thread_list
);
222 /* initialize the structure for a newly allocated thread */
223 static inline void init_thread_structure( struct thread
*thread
)
227 thread
->unix_pid
= -1; /* not known yet */
228 thread
->unix_tid
= -1; /* not known yet */
229 thread
->context
= NULL
;
231 thread
->entry_point
= 0;
232 thread
->system_regs
= 0;
233 thread
->queue
= NULL
;
236 thread
->req_data
= NULL
;
237 thread
->req_toread
= 0;
238 thread
->reply_data
= NULL
;
239 thread
->reply_towrite
= 0;
240 thread
->request_fd
= NULL
;
241 thread
->reply_fd
= NULL
;
242 thread
->wait_fd
= NULL
;
243 thread
->state
= RUNNING
;
244 thread
->exit_code
= 0;
245 thread
->priority
= 0;
247 thread
->dbg_hidden
= 0;
248 thread
->desktop_users
= 0;
249 thread
->token
= NULL
;
251 thread
->desc_len
= 0;
253 thread
->creation_time
= current_time
;
254 thread
->exit_time
= 0;
256 list_init( &thread
->mutex_list
);
257 list_init( &thread
->system_apc
);
258 list_init( &thread
->user_apc
);
259 list_init( &thread
->kernel_object
);
261 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
262 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
265 /* check if address looks valid for a client-side data structure (TEB etc.) */
266 static inline int is_valid_address( client_ptr_t addr
)
268 return addr
&& !(addr
% sizeof(int));
272 /* dump a context on stdout for debugging purposes */
273 static void dump_context( struct object
*obj
, int verbose
)
275 struct context
*context
= (struct context
*)obj
;
276 assert( obj
->ops
== &context_ops
);
278 fprintf( stderr
, "context flags=%x/%x\n",
279 context
->regs
[CTX_NATIVE
].flags
, context
->regs
[CTX_WOW
].flags
);
283 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
285 struct context
*context
= (struct context
*)obj
;
286 return context
->status
!= STATUS_PENDING
;
290 static struct context
*create_thread_context( struct thread
*thread
)
292 struct context
*context
;
293 if (!(context
= alloc_object( &context_ops
))) return NULL
;
294 context
->status
= STATUS_PENDING
;
295 memset( &context
->regs
, 0, sizeof(context
->regs
) );
296 context
->regs
[CTX_NATIVE
].machine
= native_machine
;
297 context
->regs
[CTX_PENDING
].machine
= native_machine
;
302 /* create a new thread */
303 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
305 struct desktop
*desktop
;
306 struct thread
*thread
;
311 if (pipe( request_pipe
) == -1)
316 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
318 close( request_pipe
[0] );
319 close( request_pipe
[1] );
322 close( request_pipe
[1] );
323 fd
= request_pipe
[0];
326 if (process
->is_terminating
)
329 set_error( STATUS_PROCESS_IS_TERMINATING
);
333 if (!(thread
= alloc_object( &thread_ops
)))
339 init_thread_structure( thread
);
341 thread
->process
= (struct process
*)grab_object( process
);
343 thread
->affinity
= process
->affinity
;
344 if (!current
) current
= thread
;
346 list_add_tail( &thread_list
, &thread
->entry
);
348 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
349 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
350 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
354 release_object( thread
);
357 if (!(thread
->id
= alloc_ptid( thread
)))
360 release_object( thread
);
363 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
365 release_object( thread
);
369 if (process
->desktop
)
371 if (!(desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error(); /* ignore errors */
374 set_thread_default_desktop( thread
, desktop
, process
->desktop
);
375 release_object( desktop
);
379 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
380 add_process_thread( thread
->process
, thread
);
384 /* handle a client event */
385 static void thread_poll_event( struct fd
*fd
, int event
)
387 struct thread
*thread
= get_fd_user( fd
);
388 assert( thread
->obj
.ops
== &thread_ops
);
390 grab_object( thread
);
391 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
392 else if (event
& POLLIN
) read_request( thread
);
393 else if (event
& POLLOUT
) write_reply( thread
);
394 release_object( thread
);
397 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
399 struct thread
*thread
= (struct thread
*)obj
;
400 return &thread
->kernel_object
;
403 /* cleanup everything that is no longer needed by a dead thread */
404 /* used by destroy_thread and kill_thread */
405 static void cleanup_thread( struct thread
*thread
)
411 thread
->context
->status
= STATUS_ACCESS_DENIED
;
412 wake_up( &thread
->context
->obj
, 0 );
413 release_object( thread
->context
);
414 thread
->context
= NULL
;
416 clear_apc_queue( &thread
->system_apc
);
417 clear_apc_queue( &thread
->user_apc
);
418 free( thread
->req_data
);
419 free( thread
->reply_data
);
420 if (thread
->request_fd
) release_object( thread
->request_fd
);
421 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
422 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
423 cleanup_clipboard_thread(thread
);
424 destroy_thread_windows( thread
);
425 free_msg_queue( thread
);
426 release_thread_desktop( thread
, 1 );
427 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
429 if (thread
->inflight
[i
].client
!= -1)
431 close( thread
->inflight
[i
].server
);
432 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
435 free( thread
->desc
);
436 thread
->req_data
= NULL
;
437 thread
->reply_data
= NULL
;
438 thread
->request_fd
= NULL
;
439 thread
->reply_fd
= NULL
;
440 thread
->wait_fd
= NULL
;
443 thread
->desc_len
= 0;
446 /* destroy a thread when its refcount is 0 */
447 static void destroy_thread( struct object
*obj
)
449 struct thread
*thread
= (struct thread
*)obj
;
450 assert( obj
->ops
== &thread_ops
);
452 list_remove( &thread
->entry
);
453 cleanup_thread( thread
);
454 release_object( thread
->process
);
455 if (thread
->id
) free_ptid( thread
->id
);
456 if (thread
->token
) release_object( thread
->token
);
459 /* dump a thread on stdout for debugging purposes */
460 static void dump_thread( struct object
*obj
, int verbose
)
462 struct thread
*thread
= (struct thread
*)obj
;
463 assert( obj
->ops
== &thread_ops
);
465 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
466 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
469 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
471 struct thread
*mythread
= (struct thread
*)obj
;
472 return (mythread
->state
== TERMINATED
);
475 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
477 access
= default_map_access( obj
, access
);
478 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
479 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
483 static void dump_thread_apc( struct object
*obj
, int verbose
)
485 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
486 assert( obj
->ops
== &thread_apc_ops
);
488 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
491 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
493 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
494 return apc
->executed
;
497 static void thread_apc_destroy( struct object
*obj
)
499 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
501 if (apc
->caller
) release_object( apc
->caller
);
504 if (apc
->result
.type
== APC_ASYNC_IO
)
505 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
506 else if (apc
->call
.type
== APC_ASYNC_IO
)
507 async_set_result( apc
->owner
, apc
->call
.async_io
.status
, 0 );
508 release_object( apc
->owner
);
512 /* queue an async procedure call */
513 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
515 struct thread_apc
*apc
;
517 if ((apc
= alloc_object( &thread_apc_ops
)))
519 apc
->call
= *call_data
;
523 apc
->result
.type
= APC_NONE
;
524 if (owner
) grab_object( owner
);
529 /* get a thread pointer from a thread id (and increment the refcount) */
530 struct thread
*get_thread_from_id( thread_id_t id
)
532 struct object
*obj
= get_ptid_entry( id
);
534 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
535 set_error( STATUS_INVALID_CID
);
539 /* get a thread from a handle (and increment the refcount) */
540 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
542 return (struct thread
*)get_handle_obj( current
->process
, handle
,
543 access
, &thread_ops
);
546 /* find a thread from a Unix tid */
547 struct thread
*get_thread_from_tid( int tid
)
549 struct thread
*thread
;
551 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
553 if (thread
->unix_tid
== tid
) return thread
;
558 /* find a thread from a Unix pid */
559 struct thread
*get_thread_from_pid( int pid
)
561 struct thread
*thread
;
563 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
565 if (thread
->unix_pid
== pid
) return thread
;
570 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
573 #ifdef HAVE_SCHED_SETAFFINITY
574 if (thread
->unix_tid
!= -1)
581 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
582 if (affinity
& mask
) CPU_SET( i
, &set
);
584 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
587 if (!ret
) thread
->affinity
= affinity
;
591 affinity_t
get_thread_affinity( struct thread
*thread
)
594 #ifdef HAVE_SCHED_SETAFFINITY
595 if (thread
->unix_tid
!= -1)
600 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
601 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
602 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
605 if (!mask
) mask
= ~(affinity_t
)0;
609 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
610 #define THREAD_PRIORITY_REALTIME_LOWEST -7
612 /* set all information about a thread */
613 static void set_thread_info( struct thread
*thread
,
614 const struct set_thread_info_request
*req
)
616 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
618 int max
= THREAD_PRIORITY_HIGHEST
;
619 int min
= THREAD_PRIORITY_LOWEST
;
620 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
622 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
623 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
625 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
626 req
->priority
== THREAD_PRIORITY_IDLE
||
627 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
628 thread
->priority
= req
->priority
;
630 set_error( STATUS_INVALID_PARAMETER
);
632 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
634 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
635 set_error( STATUS_INVALID_PARAMETER
);
636 else if (thread
->state
== TERMINATED
)
637 set_error( STATUS_THREAD_IS_TERMINATING
);
638 else if (set_thread_affinity( thread
, req
->affinity
))
641 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
642 security_set_thread_token( thread
, req
->token
);
643 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
644 thread
->entry_point
= req
->entry_point
;
645 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
646 thread
->dbg_hidden
= 1;
647 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
650 data_size_t desc_len
= get_req_data_size();
654 if ((desc
= mem_alloc( desc_len
)))
656 memcpy( desc
, get_req_data(), desc_len
);
657 free( thread
->desc
);
659 thread
->desc_len
= desc_len
;
664 free( thread
->desc
);
666 thread
->desc_len
= 0;
671 /* stop a thread (at the Unix level) */
672 void stop_thread( struct thread
*thread
)
674 if (thread
->context
) return; /* already suspended, no need for a signal */
675 if (!(thread
->context
= create_thread_context( thread
))) return;
676 /* can't stop a thread while initialisation is in progress */
677 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
680 /* suspend a thread */
681 int suspend_thread( struct thread
*thread
)
683 int old_count
= thread
->suspend
;
684 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
686 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
688 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
692 /* resume a thread */
693 int resume_thread( struct thread
*thread
)
695 int old_count
= thread
->suspend
;
696 if (thread
->suspend
> 0)
698 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
699 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
704 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
705 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
709 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
713 /* remove a thread from an object wait queue */
714 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
716 list_remove( &entry
->entry
);
717 release_object( obj
);
720 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
722 return entry
->wait
->thread
;
725 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
727 return entry
->wait
->select
;
730 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
732 return entry
->wait
->key
;
735 void make_wait_abandoned( struct wait_queue_entry
*entry
)
737 entry
->wait
->abandoned
= 1;
740 void set_wait_status( struct wait_queue_entry
*entry
, int status
)
742 entry
->wait
->status
= status
;
746 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
748 struct thread_wait
*wait
= thread
->wait
;
749 struct wait_queue_entry
*entry
;
753 thread
->wait
= wait
->next
;
755 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
757 wait
->status
= status
;
758 if (wait
->select
== SELECT_WAIT_ALL
)
760 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
761 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
765 entry
= wait
->queues
+ status
;
766 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
768 status
= wait
->status
;
769 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
771 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
772 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
773 if (wait
->user
) remove_timeout_user( wait
->user
);
778 /* build the thread wait structure */
779 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
780 int flags
, abstime_t when
)
782 struct thread_wait
*wait
;
783 struct wait_queue_entry
*entry
;
786 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
787 wait
->next
= current
->wait
;
788 wait
->thread
= current
;
791 wait
->select
= select_op
->op
;
796 current
->wait
= wait
;
798 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
800 struct object
*obj
= objects
[i
];
802 if (!obj
->ops
->add_queue( obj
, entry
))
805 end_wait( current
, get_error() );
812 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
813 int flags
, abstime_t when
)
815 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
819 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
821 for (i
= 0; i
< count
; i
++)
822 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
825 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
827 while (i
> 0) release_object( objects
[--i
] );
831 /* check if the thread waiting condition is satisfied */
832 static int check_wait( struct thread
*thread
)
835 struct thread_wait
*wait
= thread
->wait
;
836 struct wait_queue_entry
*entry
;
840 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
841 return STATUS_KERNEL_APC
;
843 /* Suspended threads may not acquire locks, but they can run system APCs */
844 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
846 if (wait
->select
== SELECT_WAIT_ALL
)
849 /* Note: we must check them all anyway, as some objects may
850 * want to do something when signaled, even if others are not */
851 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
852 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
853 if (!not_ok
) return STATUS_WAIT_0
;
857 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
858 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
861 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
862 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
863 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
867 /* send the wakeup signal to a thread */
868 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
870 struct wake_up_reply reply
;
873 /* check if we're waking current suspend wait */
874 if (thread
->context
&& thread
->suspend_cookie
== cookie
875 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
877 if (!thread
->context
->regs
[CTX_NATIVE
].flags
&& !thread
->context
->regs
[CTX_WOW
].flags
)
879 release_object( thread
->context
);
880 thread
->context
= NULL
;
882 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
885 memset( &reply
, 0, sizeof(reply
) );
886 reply
.cookie
= cookie
;
887 reply
.signaled
= signaled
;
888 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
891 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
892 else if (errno
== EPIPE
)
893 kill_thread( thread
, 0 ); /* normal death */
895 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
899 /* attempt to wake up a thread */
900 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
901 int wake_thread( struct thread
*thread
)
906 for (count
= 0; thread
->wait
; count
++)
908 if ((signaled
= check_wait( thread
)) == -1) break;
910 cookie
= thread
->wait
->cookie
;
911 signaled
= end_wait( thread
, signaled
);
912 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
913 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
915 if (!count
) count
= -1;
922 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
923 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
925 struct thread_wait
*wait
= entry
->wait
;
926 struct thread
*thread
= wait
->thread
;
930 if (thread
->wait
!= wait
) return 0; /* not the current wait */
931 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
933 assert( wait
->select
!= SELECT_WAIT_ALL
);
935 cookie
= wait
->cookie
;
936 signaled
= end_wait( thread
, entry
- wait
->queues
);
937 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
939 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
940 wake_thread( thread
); /* check other waits too */
945 /* thread wait timeout */
946 static void thread_timeout( void *ptr
)
948 struct thread_wait
*wait
= ptr
;
949 struct thread
*thread
= wait
->thread
;
950 client_ptr_t cookie
= wait
->cookie
;
953 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
954 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
956 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
957 end_wait( thread
, STATUS_TIMEOUT
);
960 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
961 /* check if other objects have become signaled in the meantime */
962 wake_thread( thread
);
965 /* try signaling an event flag, a semaphore or a mutex */
966 static int signal_object( obj_handle_t handle
)
971 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
974 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
975 release_object( obj
);
980 /* select on a list of handles */
981 static int select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
982 int flags
, abstime_t when
)
986 struct object
*object
;
988 switch (select_op
->op
)
991 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return 1;
995 case SELECT_WAIT_ALL
:
996 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
997 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
999 set_error( STATUS_INVALID_PARAMETER
);
1002 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
1006 case SELECT_SIGNAL_AND_WAIT
:
1007 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
1009 if (select_op
->signal_and_wait
.signal
)
1011 if (!signal_object( select_op
->signal_and_wait
.signal
))
1013 end_wait( current
, get_error() );
1016 /* check if we woke ourselves up */
1017 if (!current
->wait
) return 1;
1021 case SELECT_KEYED_EVENT_WAIT
:
1022 case SELECT_KEYED_EVENT_RELEASE
:
1023 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1024 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1025 if (!object
) return 1;
1026 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1027 release_object( object
);
1029 current
->wait
->key
= select_op
->keyed_event
.key
;
1033 set_error( STATUS_INVALID_PARAMETER
);
1037 if ((ret
= check_wait( current
)) != -1)
1039 /* condition is already satisfied */
1040 set_error( end_wait( current
, ret
));
1044 /* now we need to wait */
1045 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1047 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1048 thread_timeout
, current
->wait
)))
1050 end_wait( current
, get_error() );
1054 current
->wait
->cookie
= cookie
;
1055 set_error( STATUS_PENDING
);
1059 /* attempt to wake threads sleeping on the object wait queue */
1060 void wake_up( struct object
*obj
, int max
)
1065 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1067 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1068 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1069 if (ret
> 0 && max
&& !--max
) break;
1070 /* restart at the head of the list since a wake up can change the object wait queue */
1071 ptr
= &obj
->wait_queue
;
1075 /* return the apc queue to use for a given apc type */
1076 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1083 return &thread
->user_apc
;
1085 return &thread
->system_apc
;
1089 /* check if thread is currently waiting for a (system) apc */
1090 static inline int is_in_apc_wait( struct thread
*thread
)
1092 return (thread
->process
->suspend
|| thread
->suspend
||
1093 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1096 /* queue an existing APC to a given thread */
1097 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1101 if (thread
&& thread
->state
== TERMINATED
&& process
)
1104 if (!thread
) /* find a suitable thread inside the process */
1106 struct thread
*candidate
;
1108 /* first try to find a waiting thread */
1109 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1111 if (candidate
->state
== TERMINATED
) continue;
1112 if (is_in_apc_wait( candidate
))
1120 /* then use the first one that accepts a signal */
1121 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1123 if (send_thread_signal( candidate
, SIGUSR1
))
1130 if (!thread
) return 0; /* nothing found */
1131 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1135 if (thread
->state
== TERMINATED
) return 0;
1136 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1137 /* send signal for system APCs if needed */
1138 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1140 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1142 /* cancel a possible previous APC with the same owner */
1143 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1147 list_add_tail( queue
, &apc
->entry
);
1148 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1149 wake_thread( thread
);
1154 /* queue an async procedure call */
1155 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1157 struct thread_apc
*apc
;
1160 if ((apc
= create_apc( owner
, call_data
)))
1162 ret
= queue_apc( process
, thread
, apc
);
1163 release_object( apc
);
1168 /* cancel the async procedure call owned by a specific object */
1169 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1171 struct thread_apc
*apc
;
1172 struct list
*queue
= get_apc_queue( thread
, type
);
1174 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1176 if (apc
->owner
!= owner
) continue;
1177 list_remove( &apc
->entry
);
1179 wake_up( &apc
->obj
, 0 );
1180 release_object( apc
);
1185 /* remove the head apc from the queue; the returned object must be released by the caller */
1186 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1188 struct thread_apc
*apc
= NULL
;
1189 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1193 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1199 /* clear an APC queue, cancelling all the APCs on it */
1200 static void clear_apc_queue( struct list
*queue
)
1204 while ((ptr
= list_head( queue
)))
1206 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1207 list_remove( &apc
->entry
);
1209 wake_up( &apc
->obj
, 0 );
1210 release_object( apc
);
1214 /* add an fd to the inflight list */
1215 /* return list index, or -1 on error */
1216 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1220 if (server
== -1) return -1;
1227 /* first check if we already have an entry for this fd */
1228 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1229 if (thread
->inflight
[i
].client
== client
)
1231 close( thread
->inflight
[i
].server
);
1232 thread
->inflight
[i
].server
= server
;
1236 /* now find a free spot to store it */
1237 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1238 if (thread
->inflight
[i
].client
== -1)
1240 thread
->inflight
[i
].client
= client
;
1241 thread
->inflight
[i
].server
= server
;
1249 /* get an inflight fd and purge it from the list */
1250 /* the fd must be closed when no longer used */
1251 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1255 if (client
== -1) return -1;
1259 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1261 if (thread
->inflight
[i
].client
== client
)
1263 ret
= thread
->inflight
[i
].server
;
1264 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1268 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1272 /* kill a thread on the spot */
1273 void kill_thread( struct thread
*thread
, int violent_death
)
1275 if (thread
->state
== TERMINATED
) return; /* already killed */
1276 thread
->state
= TERMINATED
;
1277 thread
->exit_time
= current_time
;
1278 if (current
== thread
) current
= NULL
;
1280 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1281 thread
->id
, thread
->exit_code
);
1284 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1285 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1286 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1289 kill_console_processes( thread
, 0 );
1290 abandon_mutexes( thread
);
1291 wake_up( &thread
->obj
, 0 );
1292 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1293 cleanup_thread( thread
);
1294 remove_process_thread( thread
->process
, thread
);
1295 release_object( thread
);
1298 /* copy parts of a context structure */
1299 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1301 assert( to
->machine
== from
->machine
);
1302 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1303 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1304 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1305 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1306 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1307 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1308 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1311 /* gets the current impersonation token */
1312 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1315 return thread
->token
;
1317 return thread
->process
->token
;
1320 /* create a new thread */
1321 DECL_HANDLER(new_thread
)
1323 struct thread
*thread
;
1324 struct process
*process
;
1325 struct unicode_str name
;
1326 const struct security_descriptor
*sd
;
1327 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1328 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1330 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1332 if (request_fd
!= -1) close( request_fd
);
1336 if (process
!= current
->process
)
1338 if (request_fd
!= -1) /* can't create a request fd in a different process */
1340 close( request_fd
);
1341 set_error( STATUS_INVALID_PARAMETER
);
1344 if (process
->running_threads
) /* only the initial thread can be created in another process */
1346 set_error( STATUS_ACCESS_DENIED
);
1350 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1352 if (request_fd
!= -1) close( request_fd
);
1353 set_error( STATUS_INVALID_HANDLE
);
1357 if ((thread
= create_thread( request_fd
, process
, sd
)))
1359 thread
->system_regs
= current
->system_regs
;
1360 if (req
->flags
& THREAD_CREATE_FLAGS_CREATE_SUSPENDED
) thread
->suspend
++;
1361 thread
->dbg_hidden
= !!(req
->flags
& THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER
);
1362 reply
->tid
= get_thread_id( thread
);
1363 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1364 req
->access
, objattr
->attributes
)))
1366 /* thread object will be released when the thread gets killed */
1369 kill_thread( thread
, 1 );
1372 release_object( process
);
1375 static int init_thread( struct thread
*thread
, int reply_fd
, int wait_fd
)
1377 if ((reply_fd
= thread_get_inflight_fd( thread
, reply_fd
)) == -1)
1379 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1382 if ((wait_fd
= thread_get_inflight_fd( thread
, wait_fd
)) == -1)
1384 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1388 if (thread
->reply_fd
) /* already initialised */
1390 set_error( STATUS_INVALID_PARAMETER
);
1394 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1396 thread
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, &thread
->obj
, 0 );
1397 thread
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, &thread
->obj
, 0 );
1398 return thread
->reply_fd
&& thread
->wait_fd
;
1401 if (reply_fd
!= -1) close( reply_fd
);
1402 if (wait_fd
!= -1) close( wait_fd
);
1406 /* initialize the first thread of a new process */
1407 DECL_HANDLER(init_first_thread
)
1409 struct process
*process
= current
->process
;
1411 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1413 current
->unix_pid
= process
->unix_pid
= req
->unix_pid
;
1414 current
->unix_tid
= req
->unix_tid
;
1416 if (!process
->parent_id
)
1417 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1419 set_thread_affinity( current
, current
->affinity
);
1421 debug_level
= max( debug_level
, req
->debug_level
);
1423 reply
->pid
= get_process_id( process
);
1424 reply
->tid
= get_thread_id( current
);
1425 reply
->session_id
= process
->session_id
;
1426 reply
->info_size
= get_process_startup_info_size( process
);
1427 reply
->server_start
= server_start_time
;
1428 set_reply_data( supported_machines
,
1429 min( supported_machines_count
* sizeof(unsigned short), get_reply_max_size() ));
1432 /* initialize a new thread */
1433 DECL_HANDLER(init_thread
)
1435 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1437 if (!is_valid_address(req
->teb
))
1439 set_error( STATUS_INVALID_PARAMETER
);
1443 current
->unix_pid
= current
->process
->unix_pid
;
1444 current
->unix_tid
= req
->unix_tid
;
1445 current
->teb
= req
->teb
;
1446 current
->entry_point
= req
->entry
;
1448 init_thread_context( current
);
1449 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1450 set_thread_affinity( current
, current
->affinity
);
1452 reply
->suspend
= (current
->suspend
|| current
->process
->suspend
|| current
->context
!= NULL
);
1455 /* terminate a thread */
1456 DECL_HANDLER(terminate_thread
)
1458 struct thread
*thread
;
1460 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1462 thread
->exit_code
= req
->exit_code
;
1463 if (thread
!= current
) kill_thread( thread
, 1 );
1464 else reply
->self
= 1;
1465 cancel_terminating_thread_asyncs( thread
);
1466 release_object( thread
);
1470 /* open a handle to a thread */
1471 DECL_HANDLER(open_thread
)
1473 struct thread
*thread
= get_thread_from_id( req
->tid
);
1478 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1479 release_object( thread
);
1483 /* fetch information about a thread */
1484 DECL_HANDLER(get_thread_info
)
1486 struct thread
*thread
;
1487 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1489 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1490 thread
= get_thread_from_handle( req
->handle
, access
);
1493 reply
->pid
= get_process_id( thread
->process
);
1494 reply
->tid
= get_thread_id( thread
);
1495 reply
->teb
= thread
->teb
;
1496 reply
->entry_point
= thread
->entry_point
;
1497 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1498 reply
->priority
= thread
->priority
;
1499 reply
->affinity
= thread
->affinity
;
1500 reply
->last
= thread
->process
->running_threads
== 1;
1501 reply
->suspend_count
= thread
->suspend
;
1502 reply
->dbg_hidden
= thread
->dbg_hidden
;
1503 reply
->desc_len
= thread
->desc_len
;
1505 if (thread
->desc
&& get_reply_max_size())
1507 if (thread
->desc_len
<= get_reply_max_size())
1508 set_reply_data( thread
->desc
, thread
->desc_len
);
1510 set_error( STATUS_BUFFER_TOO_SMALL
);
1513 release_object( thread
);
1517 /* fetch information about thread times */
1518 DECL_HANDLER(get_thread_times
)
1520 struct thread
*thread
;
1522 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1524 reply
->creation_time
= thread
->creation_time
;
1525 reply
->exit_time
= thread
->exit_time
;
1526 reply
->unix_pid
= thread
->unix_pid
;
1527 reply
->unix_tid
= thread
->unix_tid
;
1529 release_object( thread
);
1533 /* set information about a thread */
1534 DECL_HANDLER(set_thread_info
)
1536 struct thread
*thread
;
1537 unsigned int access
= (req
->mask
== SET_THREAD_INFO_DESCRIPTION
) ? THREAD_SET_LIMITED_INFORMATION
1538 : THREAD_SET_INFORMATION
;
1540 if ((thread
= get_thread_from_handle( req
->handle
, access
)))
1542 set_thread_info( thread
, req
);
1543 release_object( thread
);
1547 /* suspend a thread */
1548 DECL_HANDLER(suspend_thread
)
1550 struct thread
*thread
;
1552 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1554 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1555 else reply
->count
= suspend_thread( thread
);
1556 release_object( thread
);
1560 /* resume a thread */
1561 DECL_HANDLER(resume_thread
)
1563 struct thread
*thread
;
1565 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1567 reply
->count
= resume_thread( thread
);
1568 release_object( thread
);
1572 /* select on a handle list */
1573 DECL_HANDLER(select
)
1575 select_op_t select_op
;
1576 data_size_t op_size
, ctx_size
;
1577 struct context
*ctx
;
1578 struct thread_apc
*apc
;
1579 const apc_result_t
*result
= get_req_data();
1580 unsigned int ctx_count
;
1582 if (get_req_data_size() < sizeof(*result
)) goto invalid_param
;
1583 if (get_req_data_size() - sizeof(*result
) < req
->size
) goto invalid_param
;
1584 if (req
->size
& 3) goto invalid_param
;
1585 ctx_size
= get_req_data_size() - sizeof(*result
) - req
->size
;
1586 ctx_count
= ctx_size
/ sizeof(context_t
);
1587 if (ctx_count
* sizeof(context_t
) != ctx_size
) goto invalid_param
;
1588 if (ctx_count
> 1 + (current
->process
->machine
!= native_machine
)) goto invalid_param
;
1592 const context_t
*native_context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1593 const context_t
*wow_context
= (ctx_count
> 1) ? native_context
+ 1 : NULL
;
1595 if (current
->context
&& current
->context
->status
!= STATUS_PENDING
) goto invalid_param
;
1597 if (native_context
->machine
== native_machine
)
1599 if (wow_context
&& wow_context
->machine
!= current
->process
->machine
) goto invalid_param
;
1601 else if (native_context
->machine
== current
->process
->machine
)
1603 if (wow_context
) goto invalid_param
;
1604 wow_context
= native_context
;
1605 native_context
= NULL
;
1607 else goto invalid_param
;
1609 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1611 ctx
= current
->context
;
1614 copy_context( &ctx
->regs
[CTX_NATIVE
], native_context
,
1615 native_context
->flags
& ~(ctx
->regs
[CTX_NATIVE
].flags
| system_flags
) );
1619 ctx
->regs
[CTX_WOW
].machine
= current
->process
->machine
;
1620 copy_context( &ctx
->regs
[CTX_WOW
], wow_context
, wow_context
->flags
& ~ctx
->regs
[CTX_WOW
].flags
);
1622 else if (ctx
->regs
[CTX_PENDING
].flags
)
1624 unsigned int flags
= ctx
->regs
[CTX_PENDING
].flags
& ~ctx
->regs
[CTX_NATIVE
].flags
;
1625 copy_context( &ctx
->regs
[CTX_NATIVE
], &ctx
->regs
[CTX_PENDING
], flags
);
1626 ctx
->regs
[CTX_NATIVE
].flags
|= flags
;
1628 ctx
->regs
[CTX_PENDING
].flags
= 0;
1629 ctx
->status
= STATUS_SUCCESS
;
1630 current
->suspend_cookie
= req
->cookie
;
1631 wake_up( &ctx
->obj
, 0 );
1634 if (!req
->cookie
) goto invalid_param
;
1636 op_size
= min( req
->size
, sizeof(select_op
) );
1637 memset( &select_op
, 0, sizeof(select_op
) );
1638 memcpy( &select_op
, result
+ 1, op_size
);
1640 /* first store results of previous apc */
1643 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1644 0, &thread_apc_ops
))) return;
1645 apc
->result
= *result
;
1647 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1649 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1650 apc
->caller
->process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1651 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1652 apc
->result
.create_thread
.handle
= handle
;
1653 clear_error(); /* ignore errors from the above calls */
1655 wake_up( &apc
->obj
, 0 );
1656 close_handle( current
->process
, req
->prev_apc
);
1657 release_object( apc
);
1660 reply
->signaled
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1662 if (get_error() == STATUS_USER_APC
)
1664 apc
= thread_dequeue_apc( current
, 0 );
1665 reply
->call
= apc
->call
;
1666 release_object( apc
);
1668 else if (get_error() == STATUS_KERNEL_APC
)
1670 apc
= thread_dequeue_apc( current
, 1 );
1671 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1672 reply
->call
= apc
->call
;
1676 wake_up( &apc
->obj
, 0 );
1678 release_object( apc
);
1680 else if (reply
->signaled
&& get_reply_max_size() >= sizeof(context_t
) &&
1681 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1683 ctx
= current
->context
;
1684 if (ctx
->regs
[CTX_NATIVE
].flags
|| ctx
->regs
[CTX_WOW
].flags
)
1686 data_size_t size
= (ctx
->regs
[CTX_WOW
].flags
? 2 : 1) * sizeof(context_t
);
1687 unsigned int flags
= system_flags
& ctx
->regs
[CTX_NATIVE
].flags
;
1688 if (flags
) set_thread_context( current
, &ctx
->regs
[CTX_NATIVE
], flags
);
1689 set_reply_data( ctx
->regs
, min( size
, get_reply_max_size() ));
1691 release_object( ctx
);
1692 current
->context
= NULL
;
1697 set_error( STATUS_INVALID_PARAMETER
);
1700 /* queue an APC for a thread or process */
1701 DECL_HANDLER(queue_apc
)
1703 struct thread
*thread
= NULL
;
1704 struct process
*process
= NULL
;
1705 struct thread_apc
*apc
;
1707 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1709 switch (apc
->call
.type
)
1713 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1715 case APC_VIRTUAL_ALLOC
:
1716 case APC_VIRTUAL_ALLOC_EX
:
1717 case APC_VIRTUAL_FREE
:
1718 case APC_VIRTUAL_PROTECT
:
1719 case APC_VIRTUAL_FLUSH
:
1720 case APC_VIRTUAL_LOCK
:
1721 case APC_VIRTUAL_UNLOCK
:
1722 case APC_UNMAP_VIEW
:
1723 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1725 case APC_VIRTUAL_QUERY
:
1726 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1729 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1730 if (process
&& process
!= current
->process
)
1732 /* duplicate the handle into the target process */
1733 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1734 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1735 if (handle
) apc
->call
.map_view
.handle
= handle
;
1738 release_object( process
);
1743 case APC_CREATE_THREAD
:
1744 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1746 case APC_DUP_HANDLE
:
1747 process
= get_process_from_handle( req
->handle
, PROCESS_DUP_HANDLE
);
1748 if (process
&& process
!= current
->process
)
1750 /* duplicate the destination process handle into the target process */
1751 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.dup_handle
.dst_process
,
1752 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1753 if (handle
) apc
->call
.dup_handle
.dst_process
= handle
;
1756 release_object( process
);
1762 set_error( STATUS_INVALID_PARAMETER
);
1768 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_UNSUCCESSFUL
);
1769 release_object( thread
);
1773 reply
->self
= (process
== current
->process
);
1776 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1779 if (queue_apc( process
, NULL
, apc
))
1781 apc
->caller
= (struct thread
*)grab_object( current
);
1782 reply
->handle
= handle
;
1786 close_handle( current
->process
, handle
);
1787 set_error( STATUS_PROCESS_IS_TERMINATING
);
1791 release_object( process
);
1794 release_object( apc
);
1797 /* Get the result of an APC call */
1798 DECL_HANDLER(get_apc_result
)
1800 struct thread_apc
*apc
;
1802 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1803 0, &thread_apc_ops
))) return;
1805 if (apc
->executed
) reply
->result
= apc
->result
;
1806 else set_error( STATUS_PENDING
);
1808 /* close the handle directly to avoid an extra round-trip */
1809 close_handle( current
->process
, req
->handle
);
1810 release_object( apc
);
1813 /* retrieve the current context of a thread */
1814 DECL_HANDLER(get_thread_context
)
1816 struct context
*thread_context
= NULL
;
1817 struct thread
*thread
;
1820 if (get_reply_max_size() < 2 * sizeof(context_t
))
1822 set_error( STATUS_INVALID_PARAMETER
);
1828 if (!(thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->context
,
1831 close_handle( current
->process
, req
->context
); /* avoid extra server call */
1835 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1836 if (req
->machine
!= native_machine
&& req
->machine
!= thread
->process
->machine
)
1837 set_error( STATUS_INVALID_PARAMETER
);
1838 else if (thread
->state
!= RUNNING
)
1839 set_error( STATUS_UNSUCCESSFUL
);
1842 reply
->self
= (thread
== current
);
1843 if (thread
!= current
) stop_thread( thread
);
1844 if (thread
->context
)
1846 /* make sure that system regs are valid in thread context */
1847 if (thread
->unix_tid
!= -1 && (system_flags
& ~thread
->context
->regs
[CTX_NATIVE
].flags
))
1848 get_thread_context( thread
, &thread
->context
->regs
[CTX_NATIVE
], system_flags
);
1849 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1851 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1853 assert( reply
->self
);
1854 memset( context
, 0, sizeof(context_t
) );
1855 context
->machine
= native_machine
;
1856 if (system_flags
) get_thread_context( thread
, context
, system_flags
);
1859 release_object( thread
);
1860 if (!thread_context
) return;
1863 if (!thread_context
->status
)
1865 unsigned int native_flags
= req
->flags
, wow_flags
= 0;
1867 if (req
->machine
== thread_context
->regs
[CTX_WOW
].machine
)
1869 native_flags
= req
->flags
& always_native_flags
;
1870 wow_flags
= req
->flags
& ~always_native_flags
;
1872 if ((context
= set_reply_data_size( (!!native_flags
+ !!wow_flags
) * sizeof(context_t
) )))
1876 memset( context
, 0, sizeof(*context
) );
1877 context
->machine
= thread_context
->regs
[CTX_NATIVE
].machine
;
1878 copy_context( context
, &thread_context
->regs
[CTX_NATIVE
], native_flags
);
1879 context
->flags
= native_flags
;
1884 memset( context
, 0, sizeof(*context
) );
1885 context
->machine
= thread_context
->regs
[CTX_WOW
].machine
;
1886 copy_context( context
, &thread_context
->regs
[CTX_WOW
], wow_flags
);
1887 context
->flags
= wow_flags
;
1893 set_error( thread_context
->status
);
1894 if (thread_context
->status
== STATUS_PENDING
)
1895 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1898 release_object( thread_context
);
1901 /* set the current context of a thread */
1902 DECL_HANDLER(set_thread_context
)
1904 struct thread
*thread
;
1905 const context_t
*contexts
= get_req_data();
1906 unsigned int ctx_count
= get_req_data_size() / sizeof(context_t
);
1908 if (!ctx_count
|| ctx_count
> 2 || ctx_count
* sizeof(context_t
) != get_req_data_size())
1910 set_error( STATUS_INVALID_PARAMETER
);
1914 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1915 reply
->self
= (thread
== current
);
1917 if (contexts
[CTX_NATIVE
].machine
!= native_machine
||
1918 (ctx_count
== 2 && contexts
[CTX_WOW
].machine
!= thread
->process
->machine
))
1919 set_error( STATUS_INVALID_PARAMETER
);
1920 else if (thread
->state
!= TERMINATED
)
1922 unsigned int ctx
= CTX_NATIVE
;
1923 const context_t
*context
= &contexts
[CTX_NATIVE
];
1924 unsigned int flags
= system_flags
& context
->flags
;
1925 unsigned int native_flags
= always_native_flags
& context
->flags
;
1927 if (thread
!= current
) stop_thread( thread
);
1928 else if (flags
) set_thread_context( thread
, context
, flags
);
1929 if (thread
->context
&& !get_error())
1933 /* If the target thread doesn't have a WoW context, set native instead.
1934 * If we don't know yet whether we have a WoW context, store native context
1935 * in CTX_PENDING and update when the target thread sends its context(s). */
1936 if (thread
->context
->status
!= STATUS_PENDING
)
1938 ctx
= thread
->context
->regs
[CTX_WOW
].machine
? CTX_WOW
: CTX_NATIVE
;
1939 context
= &contexts
[ctx
];
1941 else ctx
= CTX_PENDING
;
1943 flags
= context
->flags
;
1944 if (native_flags
&& ctx
!= CTX_NATIVE
) /* some regs are always set from the native context */
1946 copy_context( &thread
->context
->regs
[CTX_NATIVE
], &contexts
[CTX_NATIVE
], native_flags
);
1947 thread
->context
->regs
[CTX_NATIVE
].flags
|= native_flags
;
1948 flags
&= ~native_flags
;
1950 copy_context( &thread
->context
->regs
[ctx
], context
, flags
);
1951 thread
->context
->regs
[ctx
].flags
|= flags
;
1954 else set_error( STATUS_UNSUCCESSFUL
);
1956 release_object( thread
);
1959 /* fetch a selector entry for a thread */
1960 DECL_HANDLER(get_selector_entry
)
1962 struct thread
*thread
;
1963 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1965 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1966 release_object( thread
);
1970 /* Iterate thread list for process. Use global thread list to also
1971 * return terminated but not yet destroyed threads. */
1972 DECL_HANDLER(get_next_thread
)
1974 struct thread
*thread
;
1975 struct process
*process
;
1980 set_error( STATUS_INVALID_PARAMETER
);
1984 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1989 ptr
= req
->flags
? list_tail( &thread_list
) : list_head( &thread_list
);
1991 else if ((thread
= get_thread_from_handle( req
->last
, 0 )))
1993 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
1994 : list_next( &thread_list
, &thread
->entry
);
1995 release_object( thread
);
1999 release_object( process
);
2005 thread
= LIST_ENTRY( ptr
, struct thread
, entry
);
2006 if (thread
->process
== process
)
2008 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
2009 release_object( process
);
2012 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
2013 : list_next( &thread_list
, &thread
->entry
);
2015 set_error( STATUS_NO_MORE_ENTRIES
);
2016 release_object( process
);