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
22 #include "wine/port.h"
32 #include <sys/types.h>
43 #define WIN32_NO_STATUS
60 struct thread_wait
*next
; /* next wait structure for this thread */
61 struct thread
*thread
; /* owner thread */
62 int count
; /* count of objects */
65 enum select_op select
;
66 client_ptr_t key
; /* wait key for keyed events */
67 client_ptr_t cookie
; /* magic cookie to return to client */
69 struct timeout_user
*user
;
70 int status
; /* status to return (unless STATUS_PENDING) */
71 struct wait_queue_entry queues
[1];
74 /* asynchronous procedure calls */
78 struct object obj
; /* object header */
79 struct list entry
; /* queue linked list */
80 struct thread
*caller
; /* thread that queued this apc */
81 struct object
*owner
; /* object that queued this apc */
82 int executed
; /* has it been executed by the client? */
83 apc_call_t call
; /* call arguments */
84 apc_result_t result
; /* call results once executed */
87 static void dump_thread_apc( struct object
*obj
, int verbose
);
88 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
89 static void thread_apc_destroy( struct object
*obj
);
90 static void clear_apc_queue( struct list
*queue
);
92 static const struct object_ops thread_apc_ops
=
94 sizeof(struct thread_apc
), /* size */
96 dump_thread_apc
, /* dump */
97 add_queue
, /* add_queue */
98 remove_queue
, /* remove_queue */
99 thread_apc_signaled
, /* signaled */
100 no_satisfied
, /* satisfied */
101 no_signal
, /* signal */
102 no_get_fd
, /* get_fd */
103 default_map_access
, /* map_access */
104 default_get_sd
, /* get_sd */
105 default_set_sd
, /* set_sd */
106 no_get_full_name
, /* get_full_name */
107 no_lookup_name
, /* lookup_name */
108 no_link_name
, /* link_name */
109 NULL
, /* unlink_name */
110 no_open_file
, /* open_file */
111 no_kernel_obj_list
, /* get_kernel_obj_list */
112 no_close_handle
, /* close_handle */
113 thread_apc_destroy
/* destroy */
117 /* thread CPU context */
121 struct object obj
; /* object header */
122 unsigned int status
; /* status of the context */
123 context_t regs
[3]; /* context data */
125 #define CTX_NATIVE 0 /* context for native machine */
126 #define CTX_WOW 1 /* context if thread is inside WoW */
127 #define CTX_PENDING 2 /* pending native context when we don't know whether thread is inside WoW */
129 static void dump_context( struct object
*obj
, int verbose
);
130 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
132 static const struct object_ops context_ops
=
134 sizeof(struct context
), /* size */
136 dump_context
, /* dump */
137 add_queue
, /* add_queue */
138 remove_queue
, /* remove_queue */
139 context_signaled
, /* signaled */
140 no_satisfied
, /* satisfied */
141 no_signal
, /* signal */
142 no_get_fd
, /* get_fd */
143 default_map_access
, /* map_access */
144 default_get_sd
, /* get_sd */
145 default_set_sd
, /* set_sd */
146 no_get_full_name
, /* get_full_name */
147 no_lookup_name
, /* lookup_name */
148 no_link_name
, /* link_name */
149 NULL
, /* unlink_name */
150 no_open_file
, /* open_file */
151 no_kernel_obj_list
, /* get_kernel_obj_list */
152 no_close_handle
, /* close_handle */
153 no_destroy
/* destroy */
157 /* thread operations */
159 static const WCHAR thread_name
[] = {'T','h','r','e','a','d'};
161 struct type_descr thread_type
=
163 { thread_name
, sizeof(thread_name
) }, /* name */
164 THREAD_ALL_ACCESS
, /* valid_access */
166 STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
,
167 STANDARD_RIGHTS_WRITE
| THREAD_SET_LIMITED_INFORMATION
| THREAD_SET_INFORMATION
168 | THREAD_SET_CONTEXT
| THREAD_SUSPEND_RESUME
| THREAD_TERMINATE
| 0x04,
169 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_RESUME
| THREAD_QUERY_LIMITED_INFORMATION
,
174 static void dump_thread( struct object
*obj
, int verbose
);
175 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
176 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
177 static void thread_poll_event( struct fd
*fd
, int event
);
178 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
179 static void destroy_thread( struct object
*obj
);
181 static const struct object_ops thread_ops
=
183 sizeof(struct thread
), /* size */
184 &thread_type
, /* type */
185 dump_thread
, /* dump */
186 add_queue
, /* add_queue */
187 remove_queue
, /* remove_queue */
188 thread_signaled
, /* signaled */
189 no_satisfied
, /* satisfied */
190 no_signal
, /* signal */
191 no_get_fd
, /* get_fd */
192 thread_map_access
, /* map_access */
193 default_get_sd
, /* get_sd */
194 default_set_sd
, /* set_sd */
195 no_get_full_name
, /* get_full_name */
196 no_lookup_name
, /* lookup_name */
197 no_link_name
, /* link_name */
198 NULL
, /* unlink_name */
199 no_open_file
, /* open_file */
200 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
201 no_close_handle
, /* close_handle */
202 destroy_thread
/* destroy */
205 static const struct fd_ops thread_fd_ops
=
207 NULL
, /* get_poll_events */
208 thread_poll_event
, /* poll_event */
210 NULL
, /* get_fd_type */
212 NULL
, /* queue_async */
213 NULL
/* reselect_async */
216 static struct list thread_list
= LIST_INIT(thread_list
);
218 /* initialize the structure for a newly allocated thread */
219 static inline void init_thread_structure( struct thread
*thread
)
223 thread
->unix_pid
= -1; /* not known yet */
224 thread
->unix_tid
= -1; /* not known yet */
225 thread
->context
= NULL
;
227 thread
->entry_point
= 0;
228 thread
->system_regs
= 0;
229 thread
->queue
= NULL
;
232 thread
->req_data
= NULL
;
233 thread
->req_toread
= 0;
234 thread
->reply_data
= NULL
;
235 thread
->reply_towrite
= 0;
236 thread
->request_fd
= NULL
;
237 thread
->reply_fd
= NULL
;
238 thread
->wait_fd
= NULL
;
239 thread
->state
= RUNNING
;
240 thread
->exit_code
= 0;
241 thread
->priority
= 0;
243 thread
->dbg_hidden
= 0;
244 thread
->desktop_users
= 0;
245 thread
->token
= NULL
;
247 thread
->desc_len
= 0;
249 thread
->creation_time
= current_time
;
250 thread
->exit_time
= 0;
252 list_init( &thread
->mutex_list
);
253 list_init( &thread
->system_apc
);
254 list_init( &thread
->user_apc
);
255 list_init( &thread
->kernel_object
);
257 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
258 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
261 /* check if address looks valid for a client-side data structure (TEB etc.) */
262 static inline int is_valid_address( client_ptr_t addr
)
264 return addr
&& !(addr
% sizeof(int));
268 /* dump a context on stdout for debugging purposes */
269 static void dump_context( struct object
*obj
, int verbose
)
271 struct context
*context
= (struct context
*)obj
;
272 assert( obj
->ops
== &context_ops
);
274 fprintf( stderr
, "context flags=%x/%x\n",
275 context
->regs
[CTX_NATIVE
].flags
, context
->regs
[CTX_WOW
].flags
);
279 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
281 struct context
*context
= (struct context
*)obj
;
282 return context
->status
!= STATUS_PENDING
;
286 static struct context
*create_thread_context( struct thread
*thread
)
288 struct context
*context
;
289 if (!(context
= alloc_object( &context_ops
))) return NULL
;
290 context
->status
= STATUS_PENDING
;
291 memset( &context
->regs
, 0, sizeof(context
->regs
) );
292 context
->regs
[CTX_NATIVE
].machine
= native_machine
;
293 context
->regs
[CTX_PENDING
].machine
= native_machine
;
298 /* create a new thread */
299 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
301 struct desktop
*desktop
;
302 struct thread
*thread
;
307 if (pipe( request_pipe
) == -1)
312 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
314 close( request_pipe
[0] );
315 close( request_pipe
[1] );
318 close( request_pipe
[1] );
319 fd
= request_pipe
[0];
322 if (process
->is_terminating
)
325 set_error( STATUS_PROCESS_IS_TERMINATING
);
329 if (!(thread
= alloc_object( &thread_ops
)))
335 init_thread_structure( thread
);
337 thread
->process
= (struct process
*)grab_object( process
);
339 thread
->affinity
= process
->affinity
;
340 if (!current
) current
= thread
;
342 list_add_tail( &thread_list
, &thread
->entry
);
344 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
345 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
346 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
350 release_object( thread
);
353 if (!(thread
->id
= alloc_ptid( thread
)))
356 release_object( thread
);
359 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
361 release_object( thread
);
365 if (process
->desktop
)
367 if (!(desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error(); /* ignore errors */
370 set_thread_default_desktop( thread
, desktop
, process
->desktop
);
371 release_object( desktop
);
375 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
376 add_process_thread( thread
->process
, thread
);
380 /* handle a client event */
381 static void thread_poll_event( struct fd
*fd
, int event
)
383 struct thread
*thread
= get_fd_user( fd
);
384 assert( thread
->obj
.ops
== &thread_ops
);
386 grab_object( thread
);
387 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
388 else if (event
& POLLIN
) read_request( thread
);
389 else if (event
& POLLOUT
) write_reply( thread
);
390 release_object( thread
);
393 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
395 struct thread
*thread
= (struct thread
*)obj
;
396 return &thread
->kernel_object
;
399 /* cleanup everything that is no longer needed by a dead thread */
400 /* used by destroy_thread and kill_thread */
401 static void cleanup_thread( struct thread
*thread
)
407 thread
->context
->status
= STATUS_ACCESS_DENIED
;
408 wake_up( &thread
->context
->obj
, 0 );
409 release_object( thread
->context
);
410 thread
->context
= NULL
;
412 clear_apc_queue( &thread
->system_apc
);
413 clear_apc_queue( &thread
->user_apc
);
414 free( thread
->req_data
);
415 free( thread
->reply_data
);
416 if (thread
->request_fd
) release_object( thread
->request_fd
);
417 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
418 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
419 cleanup_clipboard_thread(thread
);
420 destroy_thread_windows( thread
);
421 free_msg_queue( thread
);
422 release_thread_desktop( thread
, 1 );
423 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
425 if (thread
->inflight
[i
].client
!= -1)
427 close( thread
->inflight
[i
].server
);
428 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
431 free( thread
->desc
);
432 thread
->req_data
= NULL
;
433 thread
->reply_data
= NULL
;
434 thread
->request_fd
= NULL
;
435 thread
->reply_fd
= NULL
;
436 thread
->wait_fd
= NULL
;
439 thread
->desc_len
= 0;
442 /* destroy a thread when its refcount is 0 */
443 static void destroy_thread( struct object
*obj
)
445 struct thread
*thread
= (struct thread
*)obj
;
446 assert( obj
->ops
== &thread_ops
);
448 list_remove( &thread
->entry
);
449 cleanup_thread( thread
);
450 release_object( thread
->process
);
451 if (thread
->id
) free_ptid( thread
->id
);
452 if (thread
->token
) release_object( thread
->token
);
455 /* dump a thread on stdout for debugging purposes */
456 static void dump_thread( struct object
*obj
, int verbose
)
458 struct thread
*thread
= (struct thread
*)obj
;
459 assert( obj
->ops
== &thread_ops
);
461 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
462 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
465 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
467 struct thread
*mythread
= (struct thread
*)obj
;
468 return (mythread
->state
== TERMINATED
);
471 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
473 access
= default_map_access( obj
, access
);
474 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
475 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
479 static void dump_thread_apc( struct object
*obj
, int verbose
)
481 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
482 assert( obj
->ops
== &thread_apc_ops
);
484 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
487 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
489 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
490 return apc
->executed
;
493 static void thread_apc_destroy( struct object
*obj
)
495 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
497 if (apc
->caller
) release_object( apc
->caller
);
500 if (apc
->result
.type
== APC_ASYNC_IO
)
501 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
502 else if (apc
->call
.type
== APC_ASYNC_IO
)
503 async_set_result( apc
->owner
, apc
->call
.async_io
.status
, 0 );
504 release_object( apc
->owner
);
508 /* queue an async procedure call */
509 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
511 struct thread_apc
*apc
;
513 if ((apc
= alloc_object( &thread_apc_ops
)))
515 apc
->call
= *call_data
;
519 apc
->result
.type
= APC_NONE
;
520 if (owner
) grab_object( owner
);
525 /* get a thread pointer from a thread id (and increment the refcount) */
526 struct thread
*get_thread_from_id( thread_id_t id
)
528 struct object
*obj
= get_ptid_entry( id
);
530 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
531 set_error( STATUS_INVALID_CID
);
535 /* get a thread from a handle (and increment the refcount) */
536 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
538 return (struct thread
*)get_handle_obj( current
->process
, handle
,
539 access
, &thread_ops
);
542 /* find a thread from a Unix tid */
543 struct thread
*get_thread_from_tid( int tid
)
545 struct thread
*thread
;
547 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
549 if (thread
->unix_tid
== tid
) return thread
;
554 /* find a thread from a Unix pid */
555 struct thread
*get_thread_from_pid( int pid
)
557 struct thread
*thread
;
559 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
561 if (thread
->unix_pid
== pid
) return thread
;
566 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
569 #ifdef HAVE_SCHED_SETAFFINITY
570 if (thread
->unix_tid
!= -1)
577 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
578 if (affinity
& mask
) CPU_SET( i
, &set
);
580 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
583 if (!ret
) thread
->affinity
= affinity
;
587 affinity_t
get_thread_affinity( struct thread
*thread
)
590 #ifdef HAVE_SCHED_SETAFFINITY
591 if (thread
->unix_tid
!= -1)
596 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
597 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
598 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
601 if (!mask
) mask
= ~(affinity_t
)0;
605 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
606 #define THREAD_PRIORITY_REALTIME_LOWEST -7
608 /* set all information about a thread */
609 static void set_thread_info( struct thread
*thread
,
610 const struct set_thread_info_request
*req
)
612 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
614 int max
= THREAD_PRIORITY_HIGHEST
;
615 int min
= THREAD_PRIORITY_LOWEST
;
616 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
618 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
619 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
621 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
622 req
->priority
== THREAD_PRIORITY_IDLE
||
623 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
624 thread
->priority
= req
->priority
;
626 set_error( STATUS_INVALID_PARAMETER
);
628 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
630 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
631 set_error( STATUS_INVALID_PARAMETER
);
632 else if (thread
->state
== TERMINATED
)
633 set_error( STATUS_THREAD_IS_TERMINATING
);
634 else if (set_thread_affinity( thread
, req
->affinity
))
637 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
638 security_set_thread_token( thread
, req
->token
);
639 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
640 thread
->entry_point
= req
->entry_point
;
641 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
642 thread
->dbg_hidden
= 1;
643 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
646 data_size_t desc_len
= get_req_data_size();
650 if ((desc
= mem_alloc( desc_len
)))
652 memcpy( desc
, get_req_data(), desc_len
);
653 free( thread
->desc
);
655 thread
->desc_len
= desc_len
;
660 free( thread
->desc
);
662 thread
->desc_len
= 0;
667 /* stop a thread (at the Unix level) */
668 void stop_thread( struct thread
*thread
)
670 if (thread
->context
) return; /* already suspended, no need for a signal */
671 if (!(thread
->context
= create_thread_context( thread
))) return;
672 /* can't stop a thread while initialisation is in progress */
673 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
676 /* suspend a thread */
677 int suspend_thread( struct thread
*thread
)
679 int old_count
= thread
->suspend
;
680 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
682 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
684 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
688 /* resume a thread */
689 int resume_thread( struct thread
*thread
)
691 int old_count
= thread
->suspend
;
692 if (thread
->suspend
> 0)
694 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
695 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
700 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
701 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
705 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
709 /* remove a thread from an object wait queue */
710 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
712 list_remove( &entry
->entry
);
713 release_object( obj
);
716 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
718 return entry
->wait
->thread
;
721 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
723 return entry
->wait
->select
;
726 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
728 return entry
->wait
->key
;
731 void make_wait_abandoned( struct wait_queue_entry
*entry
)
733 entry
->wait
->abandoned
= 1;
736 void set_wait_status( struct wait_queue_entry
*entry
, int status
)
738 entry
->wait
->status
= status
;
742 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
744 struct thread_wait
*wait
= thread
->wait
;
745 struct wait_queue_entry
*entry
;
749 thread
->wait
= wait
->next
;
751 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
753 wait
->status
= status
;
754 if (wait
->select
== SELECT_WAIT_ALL
)
756 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
757 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
761 entry
= wait
->queues
+ status
;
762 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
764 status
= wait
->status
;
765 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
767 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
768 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
769 if (wait
->user
) remove_timeout_user( wait
->user
);
774 /* build the thread wait structure */
775 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
776 int flags
, abstime_t when
)
778 struct thread_wait
*wait
;
779 struct wait_queue_entry
*entry
;
782 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
783 wait
->next
= current
->wait
;
784 wait
->thread
= current
;
787 wait
->select
= select_op
->op
;
792 current
->wait
= wait
;
794 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
796 struct object
*obj
= objects
[i
];
798 if (!obj
->ops
->add_queue( obj
, entry
))
801 end_wait( current
, get_error() );
808 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
809 int flags
, abstime_t when
)
811 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
815 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
817 for (i
= 0; i
< count
; i
++)
818 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
821 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
823 while (i
> 0) release_object( objects
[--i
] );
827 /* check if the thread waiting condition is satisfied */
828 static int check_wait( struct thread
*thread
)
831 struct thread_wait
*wait
= thread
->wait
;
832 struct wait_queue_entry
*entry
;
836 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
837 return STATUS_KERNEL_APC
;
839 /* Suspended threads may not acquire locks, but they can run system APCs */
840 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
842 if (wait
->select
== SELECT_WAIT_ALL
)
845 /* Note: we must check them all anyway, as some objects may
846 * want to do something when signaled, even if others are not */
847 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
848 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
849 if (!not_ok
) return STATUS_WAIT_0
;
853 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
854 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
857 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
858 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
859 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
863 /* send the wakeup signal to a thread */
864 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
866 struct wake_up_reply reply
;
869 /* check if we're waking current suspend wait */
870 if (thread
->context
&& thread
->suspend_cookie
== cookie
871 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
873 if (!thread
->context
->regs
[CTX_NATIVE
].flags
&& !thread
->context
->regs
[CTX_WOW
].flags
)
875 release_object( thread
->context
);
876 thread
->context
= NULL
;
878 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
881 memset( &reply
, 0, sizeof(reply
) );
882 reply
.cookie
= cookie
;
883 reply
.signaled
= signaled
;
884 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
887 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
888 else if (errno
== EPIPE
)
889 kill_thread( thread
, 0 ); /* normal death */
891 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
895 /* attempt to wake up a thread */
896 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
897 int wake_thread( struct thread
*thread
)
902 for (count
= 0; thread
->wait
; count
++)
904 if ((signaled
= check_wait( thread
)) == -1) break;
906 cookie
= thread
->wait
->cookie
;
907 signaled
= end_wait( thread
, signaled
);
908 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
909 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
911 if (!count
) count
= -1;
918 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
919 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
921 struct thread_wait
*wait
= entry
->wait
;
922 struct thread
*thread
= wait
->thread
;
926 if (thread
->wait
!= wait
) return 0; /* not the current wait */
927 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
929 assert( wait
->select
!= SELECT_WAIT_ALL
);
931 cookie
= wait
->cookie
;
932 signaled
= end_wait( thread
, entry
- wait
->queues
);
933 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
935 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
936 wake_thread( thread
); /* check other waits too */
941 /* thread wait timeout */
942 static void thread_timeout( void *ptr
)
944 struct thread_wait
*wait
= ptr
;
945 struct thread
*thread
= wait
->thread
;
946 client_ptr_t cookie
= wait
->cookie
;
949 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
950 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
952 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
953 end_wait( thread
, STATUS_TIMEOUT
);
956 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
957 /* check if other objects have become signaled in the meantime */
958 wake_thread( thread
);
961 /* try signaling an event flag, a semaphore or a mutex */
962 static int signal_object( obj_handle_t handle
)
967 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
970 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
971 release_object( obj
);
976 /* select on a list of handles */
977 static int select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
978 int flags
, abstime_t when
)
982 struct object
*object
;
984 switch (select_op
->op
)
987 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return 1;
991 case SELECT_WAIT_ALL
:
992 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
993 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
995 set_error( STATUS_INVALID_PARAMETER
);
998 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
1002 case SELECT_SIGNAL_AND_WAIT
:
1003 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
1005 if (select_op
->signal_and_wait
.signal
)
1007 if (!signal_object( select_op
->signal_and_wait
.signal
))
1009 end_wait( current
, get_error() );
1012 /* check if we woke ourselves up */
1013 if (!current
->wait
) return 1;
1017 case SELECT_KEYED_EVENT_WAIT
:
1018 case SELECT_KEYED_EVENT_RELEASE
:
1019 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1020 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1021 if (!object
) return 1;
1022 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1023 release_object( object
);
1025 current
->wait
->key
= select_op
->keyed_event
.key
;
1029 set_error( STATUS_INVALID_PARAMETER
);
1033 if ((ret
= check_wait( current
)) != -1)
1035 /* condition is already satisfied */
1036 set_error( end_wait( current
, ret
));
1040 /* now we need to wait */
1041 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1043 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1044 thread_timeout
, current
->wait
)))
1046 end_wait( current
, get_error() );
1050 current
->wait
->cookie
= cookie
;
1051 set_error( STATUS_PENDING
);
1055 /* attempt to wake threads sleeping on the object wait queue */
1056 void wake_up( struct object
*obj
, int max
)
1061 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1063 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1064 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1065 if (ret
> 0 && max
&& !--max
) break;
1066 /* restart at the head of the list since a wake up can change the object wait queue */
1067 ptr
= &obj
->wait_queue
;
1071 /* return the apc queue to use for a given apc type */
1072 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1079 return &thread
->user_apc
;
1081 return &thread
->system_apc
;
1085 /* check if thread is currently waiting for a (system) apc */
1086 static inline int is_in_apc_wait( struct thread
*thread
)
1088 return (thread
->process
->suspend
|| thread
->suspend
||
1089 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1092 /* queue an existing APC to a given thread */
1093 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1097 if (thread
&& thread
->state
== TERMINATED
&& process
)
1100 if (!thread
) /* find a suitable thread inside the process */
1102 struct thread
*candidate
;
1104 /* first try to find a waiting thread */
1105 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1107 if (candidate
->state
== TERMINATED
) continue;
1108 if (is_in_apc_wait( candidate
))
1116 /* then use the first one that accepts a signal */
1117 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1119 if (send_thread_signal( candidate
, SIGUSR1
))
1126 if (!thread
) return 0; /* nothing found */
1127 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1131 if (thread
->state
== TERMINATED
) return 0;
1132 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1133 /* send signal for system APCs if needed */
1134 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1136 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1138 /* cancel a possible previous APC with the same owner */
1139 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1143 list_add_tail( queue
, &apc
->entry
);
1144 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1145 wake_thread( thread
);
1150 /* queue an async procedure call */
1151 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1153 struct thread_apc
*apc
;
1156 if ((apc
= create_apc( owner
, call_data
)))
1158 ret
= queue_apc( process
, thread
, apc
);
1159 release_object( apc
);
1164 /* cancel the async procedure call owned by a specific object */
1165 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1167 struct thread_apc
*apc
;
1168 struct list
*queue
= get_apc_queue( thread
, type
);
1170 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1172 if (apc
->owner
!= owner
) continue;
1173 list_remove( &apc
->entry
);
1175 wake_up( &apc
->obj
, 0 );
1176 release_object( apc
);
1181 /* remove the head apc from the queue; the returned object must be released by the caller */
1182 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1184 struct thread_apc
*apc
= NULL
;
1185 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1189 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1195 /* clear an APC queue, cancelling all the APCs on it */
1196 static void clear_apc_queue( struct list
*queue
)
1200 while ((ptr
= list_head( queue
)))
1202 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1203 list_remove( &apc
->entry
);
1205 wake_up( &apc
->obj
, 0 );
1206 release_object( apc
);
1210 /* add an fd to the inflight list */
1211 /* return list index, or -1 on error */
1212 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1216 if (server
== -1) return -1;
1223 /* first check if we already have an entry for this fd */
1224 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1225 if (thread
->inflight
[i
].client
== client
)
1227 close( thread
->inflight
[i
].server
);
1228 thread
->inflight
[i
].server
= server
;
1232 /* now find a free spot to store it */
1233 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1234 if (thread
->inflight
[i
].client
== -1)
1236 thread
->inflight
[i
].client
= client
;
1237 thread
->inflight
[i
].server
= server
;
1245 /* get an inflight fd and purge it from the list */
1246 /* the fd must be closed when no longer used */
1247 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1251 if (client
== -1) return -1;
1255 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1257 if (thread
->inflight
[i
].client
== client
)
1259 ret
= thread
->inflight
[i
].server
;
1260 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1264 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1268 /* kill a thread on the spot */
1269 void kill_thread( struct thread
*thread
, int violent_death
)
1271 if (thread
->state
== TERMINATED
) return; /* already killed */
1272 thread
->state
= TERMINATED
;
1273 thread
->exit_time
= current_time
;
1274 if (current
== thread
) current
= NULL
;
1276 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1277 thread
->id
, thread
->exit_code
);
1280 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1281 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1282 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1285 kill_console_processes( thread
, 0 );
1286 abandon_mutexes( thread
);
1287 wake_up( &thread
->obj
, 0 );
1288 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1289 cleanup_thread( thread
);
1290 remove_process_thread( thread
->process
, thread
);
1291 release_object( thread
);
1294 /* copy parts of a context structure */
1295 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1297 assert( to
->machine
== from
->machine
);
1298 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1299 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1300 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1301 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1302 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1303 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1304 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1307 /* return the context flags that correspond to system regs */
1308 /* (system regs are the ones we can't access on the client side) */
1309 static unsigned int get_context_system_regs( unsigned short machine
)
1313 case IMAGE_FILE_MACHINE_I386
: return SERVER_CTX_DEBUG_REGISTERS
;
1314 case IMAGE_FILE_MACHINE_AMD64
: return SERVER_CTX_DEBUG_REGISTERS
;
1315 case IMAGE_FILE_MACHINE_ARMNT
: return SERVER_CTX_DEBUG_REGISTERS
;
1316 case IMAGE_FILE_MACHINE_ARM64
: return SERVER_CTX_DEBUG_REGISTERS
;
1321 /* gets the current impersonation token */
1322 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1325 return thread
->token
;
1327 return thread
->process
->token
;
1330 /* create a new thread */
1331 DECL_HANDLER(new_thread
)
1333 struct thread
*thread
;
1334 struct process
*process
;
1335 struct unicode_str name
;
1336 const struct security_descriptor
*sd
;
1337 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1338 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1340 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1342 if (request_fd
!= -1) close( request_fd
);
1346 if (process
!= current
->process
)
1348 if (request_fd
!= -1) /* can't create a request fd in a different process */
1350 close( request_fd
);
1351 set_error( STATUS_INVALID_PARAMETER
);
1354 if (process
->running_threads
) /* only the initial thread can be created in another process */
1356 set_error( STATUS_ACCESS_DENIED
);
1360 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1362 if (request_fd
!= -1) close( request_fd
);
1363 set_error( STATUS_INVALID_HANDLE
);
1367 if ((thread
= create_thread( request_fd
, process
, sd
)))
1369 thread
->system_regs
= current
->system_regs
;
1370 if (req
->suspend
) thread
->suspend
++;
1371 reply
->tid
= get_thread_id( thread
);
1372 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1373 req
->access
, objattr
->attributes
)))
1375 /* thread object will be released when the thread gets killed */
1378 kill_thread( thread
, 1 );
1381 release_object( process
);
1384 static int init_thread( struct thread
*thread
, int reply_fd
, int wait_fd
)
1386 if ((reply_fd
= thread_get_inflight_fd( thread
, reply_fd
)) == -1)
1388 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1391 if ((wait_fd
= thread_get_inflight_fd( thread
, wait_fd
)) == -1)
1393 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1397 if (thread
->reply_fd
) /* already initialised */
1399 set_error( STATUS_INVALID_PARAMETER
);
1403 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1405 thread
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, &thread
->obj
, 0 );
1406 thread
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, &thread
->obj
, 0 );
1407 return thread
->reply_fd
&& thread
->wait_fd
;
1410 if (reply_fd
!= -1) close( reply_fd
);
1411 if (wait_fd
!= -1) close( wait_fd
);
1415 /* initialize the first thread of a new process */
1416 DECL_HANDLER(init_first_thread
)
1418 struct process
*process
= current
->process
;
1420 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1422 current
->unix_pid
= process
->unix_pid
= req
->unix_pid
;
1423 current
->unix_tid
= req
->unix_tid
;
1425 if (!process
->parent_id
)
1426 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1428 set_thread_affinity( current
, current
->affinity
);
1430 debug_level
= max( debug_level
, req
->debug_level
);
1432 reply
->pid
= get_process_id( process
);
1433 reply
->tid
= get_thread_id( current
);
1434 reply
->session_id
= process
->session_id
;
1435 reply
->info_size
= get_process_startup_info_size( process
);
1436 reply
->server_start
= server_start_time
;
1437 set_reply_data( supported_machines
,
1438 min( supported_machines_count
* sizeof(unsigned short), get_reply_max_size() ));
1441 /* initialize a new thread */
1442 DECL_HANDLER(init_thread
)
1444 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1446 if (!is_valid_address(req
->teb
))
1448 set_error( STATUS_INVALID_PARAMETER
);
1452 current
->unix_pid
= current
->process
->unix_pid
;
1453 current
->unix_tid
= req
->unix_tid
;
1454 current
->teb
= req
->teb
;
1455 current
->entry_point
= req
->entry
;
1457 init_thread_context( current
);
1458 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1459 set_thread_affinity( current
, current
->affinity
);
1461 reply
->suspend
= (current
->suspend
|| current
->process
->suspend
|| current
->context
!= NULL
);
1464 /* terminate a thread */
1465 DECL_HANDLER(terminate_thread
)
1467 struct thread
*thread
;
1469 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1471 thread
->exit_code
= req
->exit_code
;
1472 if (thread
!= current
) kill_thread( thread
, 1 );
1473 else reply
->self
= 1;
1474 release_object( thread
);
1478 /* open a handle to a thread */
1479 DECL_HANDLER(open_thread
)
1481 struct thread
*thread
= get_thread_from_id( req
->tid
);
1486 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1487 release_object( thread
);
1491 /* fetch information about a thread */
1492 DECL_HANDLER(get_thread_info
)
1494 struct thread
*thread
;
1495 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1497 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1498 thread
= get_thread_from_handle( req
->handle
, access
);
1501 reply
->pid
= get_process_id( thread
->process
);
1502 reply
->tid
= get_thread_id( thread
);
1503 reply
->teb
= thread
->teb
;
1504 reply
->entry_point
= thread
->entry_point
;
1505 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1506 reply
->priority
= thread
->priority
;
1507 reply
->affinity
= thread
->affinity
;
1508 reply
->last
= thread
->process
->running_threads
== 1;
1509 reply
->suspend_count
= thread
->suspend
;
1510 reply
->dbg_hidden
= thread
->dbg_hidden
;
1511 reply
->desc_len
= thread
->desc_len
;
1513 if (thread
->desc
&& get_reply_max_size())
1515 if (thread
->desc_len
<= get_reply_max_size())
1516 set_reply_data( thread
->desc
, thread
->desc_len
);
1518 set_error( STATUS_BUFFER_TOO_SMALL
);
1521 release_object( thread
);
1525 /* fetch information about thread times */
1526 DECL_HANDLER(get_thread_times
)
1528 struct thread
*thread
;
1530 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1532 reply
->creation_time
= thread
->creation_time
;
1533 reply
->exit_time
= thread
->exit_time
;
1534 reply
->unix_pid
= thread
->unix_pid
;
1535 reply
->unix_tid
= thread
->unix_tid
;
1537 release_object( thread
);
1541 /* set information about a thread */
1542 DECL_HANDLER(set_thread_info
)
1544 struct thread
*thread
;
1546 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1548 set_thread_info( thread
, req
);
1549 release_object( thread
);
1553 /* suspend a thread */
1554 DECL_HANDLER(suspend_thread
)
1556 struct thread
*thread
;
1558 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1560 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1561 else reply
->count
= suspend_thread( thread
);
1562 release_object( thread
);
1566 /* resume a thread */
1567 DECL_HANDLER(resume_thread
)
1569 struct thread
*thread
;
1571 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1573 reply
->count
= resume_thread( thread
);
1574 release_object( thread
);
1578 /* select on a handle list */
1579 DECL_HANDLER(select
)
1581 select_op_t select_op
;
1582 data_size_t op_size
, ctx_size
;
1583 struct context
*ctx
;
1584 struct thread_apc
*apc
;
1585 const apc_result_t
*result
= get_req_data();
1586 unsigned int ctx_count
;
1588 if (get_req_data_size() < sizeof(*result
)) goto invalid_param
;
1589 if (get_req_data_size() - sizeof(*result
) < req
->size
) goto invalid_param
;
1590 if (req
->size
& 3) goto invalid_param
;
1591 ctx_size
= get_req_data_size() - sizeof(*result
) - req
->size
;
1592 ctx_count
= ctx_size
/ sizeof(context_t
);
1593 if (ctx_count
* sizeof(context_t
) != ctx_size
) goto invalid_param
;
1594 if (ctx_count
> 1 + (current
->process
->machine
!= native_machine
)) goto invalid_param
;
1598 const context_t
*native_context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1599 const context_t
*wow_context
= (ctx_count
> 1) ? native_context
+ 1 : NULL
;
1600 unsigned int system_flags
= get_context_system_regs( native_machine
);
1602 if (current
->context
&& current
->context
->status
!= STATUS_PENDING
) goto invalid_param
;
1604 if (native_context
->machine
== native_machine
)
1606 if (wow_context
&& wow_context
->machine
!= current
->process
->machine
) goto invalid_param
;
1608 else if (native_context
->machine
== current
->process
->machine
)
1610 if (wow_context
) goto invalid_param
;
1611 wow_context
= native_context
;
1612 native_context
= NULL
;
1614 else goto invalid_param
;
1616 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1618 ctx
= current
->context
;
1621 copy_context( &ctx
->regs
[CTX_NATIVE
], native_context
,
1622 native_context
->flags
& ~(ctx
->regs
[CTX_NATIVE
].flags
| system_flags
) );
1626 ctx
->regs
[CTX_WOW
].machine
= current
->process
->machine
;
1627 copy_context( &ctx
->regs
[CTX_WOW
], wow_context
, wow_context
->flags
& ~ctx
->regs
[CTX_WOW
].flags
);
1629 else if (ctx
->regs
[CTX_PENDING
].flags
)
1631 unsigned int flags
= ctx
->regs
[CTX_PENDING
].flags
& ~ctx
->regs
[CTX_NATIVE
].flags
;
1632 copy_context( &ctx
->regs
[CTX_NATIVE
], &ctx
->regs
[CTX_PENDING
], flags
);
1633 ctx
->regs
[CTX_NATIVE
].flags
|= flags
;
1635 ctx
->regs
[CTX_PENDING
].flags
= 0;
1636 ctx
->status
= STATUS_SUCCESS
;
1637 current
->suspend_cookie
= req
->cookie
;
1638 wake_up( &ctx
->obj
, 0 );
1641 if (!req
->cookie
) goto invalid_param
;
1643 op_size
= min( req
->size
, sizeof(select_op
) );
1644 memset( &select_op
, 0, sizeof(select_op
) );
1645 memcpy( &select_op
, result
+ 1, op_size
);
1647 /* first store results of previous apc */
1650 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1651 0, &thread_apc_ops
))) return;
1652 apc
->result
= *result
;
1654 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1656 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1657 apc
->caller
->process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1658 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1659 apc
->result
.create_thread
.handle
= handle
;
1660 clear_error(); /* ignore errors from the above calls */
1662 wake_up( &apc
->obj
, 0 );
1663 close_handle( current
->process
, req
->prev_apc
);
1664 release_object( apc
);
1667 reply
->signaled
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1669 if (get_error() == STATUS_USER_APC
)
1671 apc
= thread_dequeue_apc( current
, 0 );
1672 reply
->call
= apc
->call
;
1673 release_object( apc
);
1675 else if (get_error() == STATUS_KERNEL_APC
)
1677 apc
= thread_dequeue_apc( current
, 1 );
1678 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1679 reply
->call
= apc
->call
;
1683 wake_up( &apc
->obj
, 0 );
1685 release_object( apc
);
1687 else if (reply
->signaled
&& get_reply_max_size() >= sizeof(context_t
) &&
1688 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1690 ctx
= current
->context
;
1691 if (ctx
->regs
[CTX_NATIVE
].flags
|| ctx
->regs
[CTX_WOW
].flags
)
1693 data_size_t size
= (ctx
->regs
[CTX_WOW
].flags
? 2 : 1) * sizeof(context_t
);
1694 unsigned int system_flags
= get_context_system_regs( native_machine
) & ctx
->regs
[CTX_NATIVE
].flags
;
1695 if (system_flags
) set_thread_context( current
, &ctx
->regs
[CTX_NATIVE
], system_flags
);
1696 set_reply_data( ctx
->regs
, min( size
, get_reply_max_size() ));
1698 release_object( ctx
);
1699 current
->context
= NULL
;
1704 set_error( STATUS_INVALID_PARAMETER
);
1707 /* queue an APC for a thread or process */
1708 DECL_HANDLER(queue_apc
)
1710 struct thread
*thread
= NULL
;
1711 struct process
*process
= NULL
;
1712 struct thread_apc
*apc
;
1714 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1716 switch (apc
->call
.type
)
1720 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1722 case APC_VIRTUAL_ALLOC
:
1723 case APC_VIRTUAL_FREE
:
1724 case APC_VIRTUAL_PROTECT
:
1725 case APC_VIRTUAL_FLUSH
:
1726 case APC_VIRTUAL_LOCK
:
1727 case APC_VIRTUAL_UNLOCK
:
1728 case APC_UNMAP_VIEW
:
1729 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1731 case APC_VIRTUAL_QUERY
:
1732 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1735 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1736 if (process
&& process
!= current
->process
)
1738 /* duplicate the handle into the target process */
1739 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1740 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1741 if (handle
) apc
->call
.map_view
.handle
= handle
;
1744 release_object( process
);
1749 case APC_CREATE_THREAD
:
1750 case APC_BREAK_PROCESS
:
1751 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1753 case APC_DUP_HANDLE
:
1754 process
= get_process_from_handle( req
->handle
, PROCESS_DUP_HANDLE
);
1755 if (process
&& process
!= current
->process
)
1757 /* duplicate the destination process handle into the target process */
1758 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.dup_handle
.dst_process
,
1759 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1760 if (handle
) apc
->call
.dup_handle
.dst_process
= handle
;
1763 release_object( process
);
1769 set_error( STATUS_INVALID_PARAMETER
);
1775 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_UNSUCCESSFUL
);
1776 release_object( thread
);
1780 reply
->self
= (process
== current
->process
);
1783 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1786 if (queue_apc( process
, NULL
, apc
))
1788 apc
->caller
= (struct thread
*)grab_object( current
);
1789 reply
->handle
= handle
;
1793 close_handle( current
->process
, handle
);
1794 set_error( STATUS_PROCESS_IS_TERMINATING
);
1798 release_object( process
);
1801 release_object( apc
);
1804 /* Get the result of an APC call */
1805 DECL_HANDLER(get_apc_result
)
1807 struct thread_apc
*apc
;
1809 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1810 0, &thread_apc_ops
))) return;
1812 if (apc
->executed
) reply
->result
= apc
->result
;
1813 else set_error( STATUS_PENDING
);
1815 /* close the handle directly to avoid an extra round-trip */
1816 close_handle( current
->process
, req
->handle
);
1817 release_object( apc
);
1820 /* retrieve the current context of a thread */
1821 DECL_HANDLER(get_thread_context
)
1823 struct context
*thread_context
= NULL
;
1824 struct thread
*thread
;
1827 if (get_reply_max_size() < 2 * sizeof(context_t
))
1829 set_error( STATUS_INVALID_PARAMETER
);
1835 if (!(thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->context
,
1838 close_handle( current
->process
, req
->context
); /* avoid extra server call */
1842 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1843 if (req
->machine
!= native_machine
&& req
->machine
!= thread
->process
->machine
)
1844 set_error( STATUS_INVALID_PARAMETER
);
1845 else if (thread
->state
!= RUNNING
)
1846 set_error( STATUS_UNSUCCESSFUL
);
1849 unsigned int system_flags
= get_context_system_regs( native_machine
);
1850 reply
->self
= (thread
== current
);
1851 if (thread
!= current
) stop_thread( thread
);
1852 if (thread
->context
)
1854 /* make sure that system regs are valid in thread context */
1855 if (thread
->unix_tid
!= -1 && (system_flags
& ~thread
->context
->regs
[CTX_NATIVE
].flags
))
1856 get_thread_context( thread
, &thread
->context
->regs
[CTX_NATIVE
], system_flags
);
1857 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1859 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1861 assert( reply
->self
);
1862 memset( context
, 0, sizeof(context_t
) );
1863 context
->machine
= native_machine
;
1864 if (system_flags
) get_thread_context( thread
, context
, system_flags
);
1867 release_object( thread
);
1868 if (!thread_context
) return;
1871 if (!thread_context
->status
)
1873 unsigned int system_flags
= get_context_system_regs( native_machine
);
1874 unsigned int native_flags
= req
->flags
, wow_flags
= 0;
1876 if (req
->machine
== thread_context
->regs
[CTX_WOW
].machine
)
1878 native_flags
= req
->flags
& system_flags
;
1879 wow_flags
= req
->flags
& ~system_flags
;
1881 if ((context
= set_reply_data_size( (!!native_flags
+ !!wow_flags
) * sizeof(context_t
) )))
1885 memset( context
, 0, sizeof(*context
) );
1886 context
->machine
= thread_context
->regs
[CTX_NATIVE
].machine
;
1887 copy_context( context
, &thread_context
->regs
[CTX_NATIVE
], native_flags
);
1888 context
->flags
= native_flags
;
1893 memset( context
, 0, sizeof(*context
) );
1894 context
->machine
= thread_context
->regs
[CTX_WOW
].machine
;
1895 copy_context( context
, &thread_context
->regs
[CTX_WOW
], wow_flags
);
1896 context
->flags
= wow_flags
;
1902 set_error( thread_context
->status
);
1903 if (thread_context
->status
== STATUS_PENDING
)
1904 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1907 release_object( thread_context
);
1910 /* set the current context of a thread */
1911 DECL_HANDLER(set_thread_context
)
1913 struct thread
*thread
;
1914 const context_t
*contexts
= get_req_data();
1915 unsigned int ctx_count
= get_req_data_size() / sizeof(context_t
);
1917 if (!ctx_count
|| ctx_count
> 2 || ctx_count
* sizeof(context_t
) != get_req_data_size())
1919 set_error( STATUS_INVALID_PARAMETER
);
1923 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1924 reply
->self
= (thread
== current
);
1926 if (contexts
[CTX_NATIVE
].machine
!= native_machine
||
1927 (ctx_count
== 2 && contexts
[CTX_WOW
].machine
!= thread
->process
->machine
))
1928 set_error( STATUS_INVALID_PARAMETER
);
1929 else if (thread
->state
!= TERMINATED
)
1931 unsigned int flags
, ctx
= CTX_NATIVE
;
1932 const context_t
*context
= &contexts
[CTX_NATIVE
];
1933 unsigned int system_flags
= get_context_system_regs( native_machine
) & context
->flags
;
1935 if (thread
!= current
) stop_thread( thread
);
1936 else if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1937 if (thread
->context
&& !get_error())
1941 /* If the target thread doesn't have a WoW context, set native instead.
1942 * If we don't know yet whether we have a WoW context, store native context
1943 * in CTX_PENDING and update when the target thread sends its context(s). */
1944 if (thread
->context
->status
!= STATUS_PENDING
)
1946 ctx
= thread
->context
->regs
[CTX_WOW
].machine
? CTX_WOW
: CTX_NATIVE
;
1947 context
= &contexts
[ctx
];
1949 else ctx
= CTX_PENDING
;
1951 flags
= context
->flags
;
1952 if (system_flags
&& ctx
!= CTX_NATIVE
) /* system regs are always set from the native context */
1954 copy_context( &thread
->context
->regs
[CTX_NATIVE
], &contexts
[CTX_NATIVE
], system_flags
);
1955 thread
->context
->regs
[CTX_NATIVE
].flags
|= system_flags
;
1956 flags
&= ~system_flags
;
1958 copy_context( &thread
->context
->regs
[ctx
], context
, flags
);
1959 thread
->context
->regs
[ctx
].flags
|= flags
;
1962 else set_error( STATUS_UNSUCCESSFUL
);
1964 release_object( thread
);
1967 /* fetch a selector entry for a thread */
1968 DECL_HANDLER(get_selector_entry
)
1970 struct thread
*thread
;
1971 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1973 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1974 release_object( thread
);
1978 /* Iterate thread list for process. Use global thread list to also
1979 * return terminated but not yet destroyed threads. */
1980 DECL_HANDLER(get_next_thread
)
1982 struct thread
*thread
;
1983 struct process
*process
;
1988 set_error( STATUS_INVALID_PARAMETER
);
1992 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1997 ptr
= req
->flags
? list_tail( &thread_list
) : list_head( &thread_list
);
1999 else if ((thread
= get_thread_from_handle( req
->last
, 0 )))
2001 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
2002 : list_next( &thread_list
, &thread
->entry
);
2003 release_object( thread
);
2007 release_object( process
);
2013 thread
= LIST_ENTRY( ptr
, struct thread
, entry
);
2014 if (thread
->process
== process
)
2016 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
2017 release_object( process
);
2020 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
2021 : list_next( &thread_list
, &thread
->entry
);
2023 set_error( STATUS_NO_MORE_ENTRIES
);
2024 release_object( process
);