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
; /* context data */
126 static void dump_context( struct object
*obj
, int verbose
);
127 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
129 static const struct object_ops context_ops
=
131 sizeof(struct context
), /* size */
133 dump_context
, /* dump */
134 add_queue
, /* add_queue */
135 remove_queue
, /* remove_queue */
136 context_signaled
, /* signaled */
137 no_satisfied
, /* satisfied */
138 no_signal
, /* signal */
139 no_get_fd
, /* get_fd */
140 default_map_access
, /* map_access */
141 default_get_sd
, /* get_sd */
142 default_set_sd
, /* set_sd */
143 no_get_full_name
, /* get_full_name */
144 no_lookup_name
, /* lookup_name */
145 no_link_name
, /* link_name */
146 NULL
, /* unlink_name */
147 no_open_file
, /* open_file */
148 no_kernel_obj_list
, /* get_kernel_obj_list */
149 no_close_handle
, /* close_handle */
150 no_destroy
/* destroy */
154 /* thread operations */
156 static const WCHAR thread_name
[] = {'T','h','r','e','a','d'};
158 struct type_descr thread_type
=
160 { thread_name
, sizeof(thread_name
) }, /* name */
161 THREAD_ALL_ACCESS
, /* valid_access */
163 STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
,
164 STANDARD_RIGHTS_WRITE
| THREAD_SET_LIMITED_INFORMATION
| THREAD_SET_INFORMATION
165 | THREAD_SET_CONTEXT
| THREAD_SUSPEND_RESUME
| THREAD_TERMINATE
| 0x04,
166 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_RESUME
| THREAD_QUERY_LIMITED_INFORMATION
,
171 static void dump_thread( struct object
*obj
, int verbose
);
172 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
173 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
174 static void thread_poll_event( struct fd
*fd
, int event
);
175 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
176 static void destroy_thread( struct object
*obj
);
178 static const struct object_ops thread_ops
=
180 sizeof(struct thread
), /* size */
181 &thread_type
, /* type */
182 dump_thread
, /* dump */
183 add_queue
, /* add_queue */
184 remove_queue
, /* remove_queue */
185 thread_signaled
, /* signaled */
186 no_satisfied
, /* satisfied */
187 no_signal
, /* signal */
188 no_get_fd
, /* get_fd */
189 thread_map_access
, /* map_access */
190 default_get_sd
, /* get_sd */
191 default_set_sd
, /* set_sd */
192 no_get_full_name
, /* get_full_name */
193 no_lookup_name
, /* lookup_name */
194 no_link_name
, /* link_name */
195 NULL
, /* unlink_name */
196 no_open_file
, /* open_file */
197 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
198 no_close_handle
, /* close_handle */
199 destroy_thread
/* destroy */
202 static const struct fd_ops thread_fd_ops
=
204 NULL
, /* get_poll_events */
205 thread_poll_event
, /* poll_event */
207 NULL
, /* get_fd_type */
209 NULL
, /* queue_async */
210 NULL
/* reselect_async */
213 static struct list thread_list
= LIST_INIT(thread_list
);
215 /* initialize the structure for a newly allocated thread */
216 static inline void init_thread_structure( struct thread
*thread
)
220 thread
->unix_pid
= -1; /* not known yet */
221 thread
->unix_tid
= -1; /* not known yet */
222 thread
->context
= NULL
;
224 thread
->entry_point
= 0;
225 thread
->system_regs
= 0;
226 thread
->queue
= NULL
;
229 thread
->req_data
= NULL
;
230 thread
->req_toread
= 0;
231 thread
->reply_data
= NULL
;
232 thread
->reply_towrite
= 0;
233 thread
->request_fd
= NULL
;
234 thread
->reply_fd
= NULL
;
235 thread
->wait_fd
= NULL
;
236 thread
->state
= RUNNING
;
237 thread
->exit_code
= 0;
238 thread
->priority
= 0;
240 thread
->dbg_hidden
= 0;
241 thread
->desktop_users
= 0;
242 thread
->token
= NULL
;
244 thread
->desc_len
= 0;
246 thread
->creation_time
= current_time
;
247 thread
->exit_time
= 0;
249 list_init( &thread
->mutex_list
);
250 list_init( &thread
->system_apc
);
251 list_init( &thread
->user_apc
);
252 list_init( &thread
->kernel_object
);
254 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
255 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
258 /* check if address looks valid for a client-side data structure (TEB etc.) */
259 static inline int is_valid_address( client_ptr_t addr
)
261 return addr
&& !(addr
% sizeof(int));
265 /* dump a context on stdout for debugging purposes */
266 static void dump_context( struct object
*obj
, int verbose
)
268 struct context
*context
= (struct context
*)obj
;
269 assert( obj
->ops
== &context_ops
);
271 fprintf( stderr
, "context flags=%x\n", context
->regs
.flags
);
275 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
277 struct context
*context
= (struct context
*)obj
;
278 return context
->status
!= STATUS_PENDING
;
282 static struct context
*create_thread_context( struct thread
*thread
)
284 struct context
*context
;
285 if (!(context
= alloc_object( &context_ops
))) return NULL
;
286 context
->status
= STATUS_PENDING
;
287 memset( &context
->regs
, 0, sizeof(context
->regs
) );
288 context
->regs
.machine
= thread
->process
->machine
;
293 /* create a new thread */
294 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
296 struct desktop
*desktop
;
297 struct thread
*thread
;
302 if (pipe( request_pipe
) == -1)
307 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
309 close( request_pipe
[0] );
310 close( request_pipe
[1] );
313 close( request_pipe
[1] );
314 fd
= request_pipe
[0];
317 if (process
->is_terminating
)
320 set_error( STATUS_PROCESS_IS_TERMINATING
);
324 if (!(thread
= alloc_object( &thread_ops
)))
330 init_thread_structure( thread
);
332 thread
->process
= (struct process
*)grab_object( process
);
334 thread
->affinity
= process
->affinity
;
335 if (!current
) current
= thread
;
337 list_add_tail( &thread_list
, &thread
->entry
);
339 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
340 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
341 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
345 release_object( thread
);
348 if (!(thread
->id
= alloc_ptid( thread
)))
351 release_object( thread
);
354 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
356 release_object( thread
);
360 if (process
->desktop
)
362 if (!(desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error(); /* ignore errors */
365 set_thread_default_desktop( thread
, desktop
, process
->desktop
);
366 release_object( desktop
);
370 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
371 add_process_thread( thread
->process
, thread
);
375 /* handle a client event */
376 static void thread_poll_event( struct fd
*fd
, int event
)
378 struct thread
*thread
= get_fd_user( fd
);
379 assert( thread
->obj
.ops
== &thread_ops
);
381 grab_object( thread
);
382 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
383 else if (event
& POLLIN
) read_request( thread
);
384 else if (event
& POLLOUT
) write_reply( thread
);
385 release_object( thread
);
388 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
390 struct thread
*thread
= (struct thread
*)obj
;
391 return &thread
->kernel_object
;
394 /* cleanup everything that is no longer needed by a dead thread */
395 /* used by destroy_thread and kill_thread */
396 static void cleanup_thread( struct thread
*thread
)
402 thread
->context
->status
= STATUS_ACCESS_DENIED
;
403 wake_up( &thread
->context
->obj
, 0 );
404 release_object( thread
->context
);
405 thread
->context
= NULL
;
407 clear_apc_queue( &thread
->system_apc
);
408 clear_apc_queue( &thread
->user_apc
);
409 free( thread
->req_data
);
410 free( thread
->reply_data
);
411 if (thread
->request_fd
) release_object( thread
->request_fd
);
412 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
413 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
414 cleanup_clipboard_thread(thread
);
415 destroy_thread_windows( thread
);
416 free_msg_queue( thread
);
417 release_thread_desktop( thread
, 1 );
418 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
420 if (thread
->inflight
[i
].client
!= -1)
422 close( thread
->inflight
[i
].server
);
423 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
426 free( thread
->desc
);
427 thread
->req_data
= NULL
;
428 thread
->reply_data
= NULL
;
429 thread
->request_fd
= NULL
;
430 thread
->reply_fd
= NULL
;
431 thread
->wait_fd
= NULL
;
434 thread
->desc_len
= 0;
437 /* destroy a thread when its refcount is 0 */
438 static void destroy_thread( struct object
*obj
)
440 struct thread
*thread
= (struct thread
*)obj
;
441 assert( obj
->ops
== &thread_ops
);
443 list_remove( &thread
->entry
);
444 cleanup_thread( thread
);
445 release_object( thread
->process
);
446 if (thread
->id
) free_ptid( thread
->id
);
447 if (thread
->token
) release_object( thread
->token
);
450 /* dump a thread on stdout for debugging purposes */
451 static void dump_thread( struct object
*obj
, int verbose
)
453 struct thread
*thread
= (struct thread
*)obj
;
454 assert( obj
->ops
== &thread_ops
);
456 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
457 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
460 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
462 struct thread
*mythread
= (struct thread
*)obj
;
463 return (mythread
->state
== TERMINATED
);
466 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
468 access
= default_map_access( obj
, access
);
469 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
470 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
474 static void dump_thread_apc( struct object
*obj
, int verbose
)
476 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
477 assert( obj
->ops
== &thread_apc_ops
);
479 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
482 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
484 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
485 return apc
->executed
;
488 static void thread_apc_destroy( struct object
*obj
)
490 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
492 if (apc
->caller
) release_object( apc
->caller
);
495 if (apc
->result
.type
== APC_ASYNC_IO
)
496 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
497 else if (apc
->call
.type
== APC_ASYNC_IO
)
498 async_set_result( apc
->owner
, apc
->call
.async_io
.status
, 0 );
499 release_object( apc
->owner
);
503 /* queue an async procedure call */
504 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
506 struct thread_apc
*apc
;
508 if ((apc
= alloc_object( &thread_apc_ops
)))
510 apc
->call
= *call_data
;
514 apc
->result
.type
= APC_NONE
;
515 if (owner
) grab_object( owner
);
520 /* get a thread pointer from a thread id (and increment the refcount) */
521 struct thread
*get_thread_from_id( thread_id_t id
)
523 struct object
*obj
= get_ptid_entry( id
);
525 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
526 set_error( STATUS_INVALID_CID
);
530 /* get a thread from a handle (and increment the refcount) */
531 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
533 return (struct thread
*)get_handle_obj( current
->process
, handle
,
534 access
, &thread_ops
);
537 /* find a thread from a Unix tid */
538 struct thread
*get_thread_from_tid( int tid
)
540 struct thread
*thread
;
542 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
544 if (thread
->unix_tid
== tid
) return thread
;
549 /* find a thread from a Unix pid */
550 struct thread
*get_thread_from_pid( int pid
)
552 struct thread
*thread
;
554 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
556 if (thread
->unix_pid
== pid
) return thread
;
561 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
564 #ifdef HAVE_SCHED_SETAFFINITY
565 if (thread
->unix_tid
!= -1)
572 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
573 if (affinity
& mask
) CPU_SET( i
, &set
);
575 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
578 if (!ret
) thread
->affinity
= affinity
;
582 affinity_t
get_thread_affinity( struct thread
*thread
)
585 #ifdef HAVE_SCHED_SETAFFINITY
586 if (thread
->unix_tid
!= -1)
591 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
592 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
593 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
596 if (!mask
) mask
= ~(affinity_t
)0;
600 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
601 #define THREAD_PRIORITY_REALTIME_LOWEST -7
603 /* set all information about a thread */
604 static void set_thread_info( struct thread
*thread
,
605 const struct set_thread_info_request
*req
)
607 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
609 int max
= THREAD_PRIORITY_HIGHEST
;
610 int min
= THREAD_PRIORITY_LOWEST
;
611 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
613 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
614 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
616 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
617 req
->priority
== THREAD_PRIORITY_IDLE
||
618 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
619 thread
->priority
= req
->priority
;
621 set_error( STATUS_INVALID_PARAMETER
);
623 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
625 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
626 set_error( STATUS_INVALID_PARAMETER
);
627 else if (thread
->state
== TERMINATED
)
628 set_error( STATUS_THREAD_IS_TERMINATING
);
629 else if (set_thread_affinity( thread
, req
->affinity
))
632 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
633 security_set_thread_token( thread
, req
->token
);
634 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
635 thread
->entry_point
= req
->entry_point
;
636 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
637 thread
->dbg_hidden
= 1;
638 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
641 data_size_t desc_len
= get_req_data_size();
645 if ((desc
= mem_alloc( desc_len
)))
647 memcpy( desc
, get_req_data(), desc_len
);
648 free( thread
->desc
);
650 thread
->desc_len
= desc_len
;
655 free( thread
->desc
);
657 thread
->desc_len
= 0;
662 /* stop a thread (at the Unix level) */
663 void stop_thread( struct thread
*thread
)
665 if (thread
->context
) return; /* already suspended, no need for a signal */
666 if (!(thread
->context
= create_thread_context( thread
))) return;
667 /* can't stop a thread while initialisation is in progress */
668 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
671 /* suspend a thread */
672 int suspend_thread( struct thread
*thread
)
674 int old_count
= thread
->suspend
;
675 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
677 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
679 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
683 /* resume a thread */
684 int resume_thread( struct thread
*thread
)
686 int old_count
= thread
->suspend
;
687 if (thread
->suspend
> 0)
689 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
690 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
695 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
696 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
700 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
704 /* remove a thread from an object wait queue */
705 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
707 list_remove( &entry
->entry
);
708 release_object( obj
);
711 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
713 return entry
->wait
->thread
;
716 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
718 return entry
->wait
->select
;
721 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
723 return entry
->wait
->key
;
726 void make_wait_abandoned( struct wait_queue_entry
*entry
)
728 entry
->wait
->abandoned
= 1;
731 void set_wait_status( struct wait_queue_entry
*entry
, int status
)
733 entry
->wait
->status
= status
;
737 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
739 struct thread_wait
*wait
= thread
->wait
;
740 struct wait_queue_entry
*entry
;
744 thread
->wait
= wait
->next
;
746 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
748 wait
->status
= status
;
749 if (wait
->select
== SELECT_WAIT_ALL
)
751 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
752 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
756 entry
= wait
->queues
+ status
;
757 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
759 status
= wait
->status
;
760 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
762 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
763 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
764 if (wait
->user
) remove_timeout_user( wait
->user
);
769 /* build the thread wait structure */
770 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
771 int flags
, abstime_t when
)
773 struct thread_wait
*wait
;
774 struct wait_queue_entry
*entry
;
777 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
778 wait
->next
= current
->wait
;
779 wait
->thread
= current
;
782 wait
->select
= select_op
->op
;
787 current
->wait
= wait
;
789 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
791 struct object
*obj
= objects
[i
];
793 if (!obj
->ops
->add_queue( obj
, entry
))
796 end_wait( current
, get_error() );
803 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
804 int flags
, abstime_t when
)
806 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
810 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
812 for (i
= 0; i
< count
; i
++)
813 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
816 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
818 while (i
> 0) release_object( objects
[--i
] );
822 /* check if the thread waiting condition is satisfied */
823 static int check_wait( struct thread
*thread
)
826 struct thread_wait
*wait
= thread
->wait
;
827 struct wait_queue_entry
*entry
;
831 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
832 return STATUS_KERNEL_APC
;
834 /* Suspended threads may not acquire locks, but they can run system APCs */
835 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
837 if (wait
->select
== SELECT_WAIT_ALL
)
840 /* Note: we must check them all anyway, as some objects may
841 * want to do something when signaled, even if others are not */
842 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
843 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
844 if (!not_ok
) return STATUS_WAIT_0
;
848 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
849 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
852 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
853 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
854 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
858 /* send the wakeup signal to a thread */
859 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
861 struct wake_up_reply reply
;
864 /* check if we're waking current suspend wait */
865 if (thread
->context
&& thread
->suspend_cookie
== cookie
866 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
868 if (!thread
->context
->regs
.flags
)
870 release_object( thread
->context
);
871 thread
->context
= NULL
;
873 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
876 memset( &reply
, 0, sizeof(reply
) );
877 reply
.cookie
= cookie
;
878 reply
.signaled
= signaled
;
879 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
882 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
883 else if (errno
== EPIPE
)
884 kill_thread( thread
, 0 ); /* normal death */
886 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
890 /* attempt to wake up a thread */
891 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
892 int wake_thread( struct thread
*thread
)
897 for (count
= 0; thread
->wait
; count
++)
899 if ((signaled
= check_wait( thread
)) == -1) break;
901 cookie
= thread
->wait
->cookie
;
902 signaled
= end_wait( thread
, signaled
);
903 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
904 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
906 if (!count
) count
= -1;
913 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
914 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
916 struct thread_wait
*wait
= entry
->wait
;
917 struct thread
*thread
= wait
->thread
;
921 if (thread
->wait
!= wait
) return 0; /* not the current wait */
922 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
924 assert( wait
->select
!= SELECT_WAIT_ALL
);
926 cookie
= wait
->cookie
;
927 signaled
= end_wait( thread
, entry
- wait
->queues
);
928 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
930 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
931 wake_thread( thread
); /* check other waits too */
936 /* thread wait timeout */
937 static void thread_timeout( void *ptr
)
939 struct thread_wait
*wait
= ptr
;
940 struct thread
*thread
= wait
->thread
;
941 client_ptr_t cookie
= wait
->cookie
;
944 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
945 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
947 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
948 end_wait( thread
, STATUS_TIMEOUT
);
951 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
952 /* check if other objects have become signaled in the meantime */
953 wake_thread( thread
);
956 /* try signaling an event flag, a semaphore or a mutex */
957 static int signal_object( obj_handle_t handle
)
962 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
965 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
966 release_object( obj
);
971 /* select on a list of handles */
972 static void select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
973 int flags
, abstime_t when
)
977 struct object
*object
;
979 switch (select_op
->op
)
982 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return;
986 case SELECT_WAIT_ALL
:
987 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
988 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
990 set_error( STATUS_INVALID_PARAMETER
);
993 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
997 case SELECT_SIGNAL_AND_WAIT
:
998 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
1000 if (select_op
->signal_and_wait
.signal
)
1002 if (!signal_object( select_op
->signal_and_wait
.signal
))
1004 end_wait( current
, get_error() );
1007 /* check if we woke ourselves up */
1008 if (!current
->wait
) return;
1012 case SELECT_KEYED_EVENT_WAIT
:
1013 case SELECT_KEYED_EVENT_RELEASE
:
1014 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1015 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1016 if (!object
) return;
1017 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1018 release_object( object
);
1020 current
->wait
->key
= select_op
->keyed_event
.key
;
1024 set_error( STATUS_INVALID_PARAMETER
);
1028 if ((ret
= check_wait( current
)) != -1)
1030 /* condition is already satisfied */
1031 set_error( end_wait( current
, ret
));
1035 /* now we need to wait */
1036 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1038 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1039 thread_timeout
, current
->wait
)))
1041 end_wait( current
, get_error() );
1045 current
->wait
->cookie
= cookie
;
1046 set_error( STATUS_PENDING
);
1050 /* attempt to wake threads sleeping on the object wait queue */
1051 void wake_up( struct object
*obj
, int max
)
1056 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1058 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1059 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1060 if (ret
> 0 && max
&& !--max
) break;
1061 /* restart at the head of the list since a wake up can change the object wait queue */
1062 ptr
= &obj
->wait_queue
;
1066 /* return the apc queue to use for a given apc type */
1067 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1074 return &thread
->user_apc
;
1076 return &thread
->system_apc
;
1080 /* check if thread is currently waiting for a (system) apc */
1081 static inline int is_in_apc_wait( struct thread
*thread
)
1083 return (thread
->process
->suspend
|| thread
->suspend
||
1084 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1087 /* queue an existing APC to a given thread */
1088 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1092 if (thread
&& thread
->state
== TERMINATED
&& process
)
1095 if (!thread
) /* find a suitable thread inside the process */
1097 struct thread
*candidate
;
1099 /* first try to find a waiting thread */
1100 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1102 if (candidate
->state
== TERMINATED
) continue;
1103 if (is_in_apc_wait( candidate
))
1111 /* then use the first one that accepts a signal */
1112 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1114 if (send_thread_signal( candidate
, SIGUSR1
))
1121 if (!thread
) return 0; /* nothing found */
1122 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1126 if (thread
->state
== TERMINATED
) return 0;
1127 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1128 /* send signal for system APCs if needed */
1129 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1131 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1133 /* cancel a possible previous APC with the same owner */
1134 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1138 list_add_tail( queue
, &apc
->entry
);
1139 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1140 wake_thread( thread
);
1145 /* queue an async procedure call */
1146 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1148 struct thread_apc
*apc
;
1151 if ((apc
= create_apc( owner
, call_data
)))
1153 ret
= queue_apc( process
, thread
, apc
);
1154 release_object( apc
);
1159 /* cancel the async procedure call owned by a specific object */
1160 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1162 struct thread_apc
*apc
;
1163 struct list
*queue
= get_apc_queue( thread
, type
);
1165 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1167 if (apc
->owner
!= owner
) continue;
1168 list_remove( &apc
->entry
);
1170 wake_up( &apc
->obj
, 0 );
1171 release_object( apc
);
1176 /* remove the head apc from the queue; the returned object must be released by the caller */
1177 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1179 struct thread_apc
*apc
= NULL
;
1180 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1184 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1190 /* clear an APC queue, cancelling all the APCs on it */
1191 static void clear_apc_queue( struct list
*queue
)
1195 while ((ptr
= list_head( queue
)))
1197 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1198 list_remove( &apc
->entry
);
1200 wake_up( &apc
->obj
, 0 );
1201 release_object( apc
);
1205 /* add an fd to the inflight list */
1206 /* return list index, or -1 on error */
1207 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1211 if (server
== -1) return -1;
1218 /* first check if we already have an entry for this fd */
1219 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1220 if (thread
->inflight
[i
].client
== client
)
1222 close( thread
->inflight
[i
].server
);
1223 thread
->inflight
[i
].server
= server
;
1227 /* now find a free spot to store it */
1228 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1229 if (thread
->inflight
[i
].client
== -1)
1231 thread
->inflight
[i
].client
= client
;
1232 thread
->inflight
[i
].server
= server
;
1240 /* get an inflight fd and purge it from the list */
1241 /* the fd must be closed when no longer used */
1242 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1246 if (client
== -1) return -1;
1250 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1252 if (thread
->inflight
[i
].client
== client
)
1254 ret
= thread
->inflight
[i
].server
;
1255 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1259 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1263 /* kill a thread on the spot */
1264 void kill_thread( struct thread
*thread
, int violent_death
)
1266 if (thread
->state
== TERMINATED
) return; /* already killed */
1267 thread
->state
= TERMINATED
;
1268 thread
->exit_time
= current_time
;
1269 if (current
== thread
) current
= NULL
;
1271 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1272 thread
->id
, thread
->exit_code
);
1275 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1276 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1277 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1280 kill_console_processes( thread
, 0 );
1281 abandon_mutexes( thread
);
1282 wake_up( &thread
->obj
, 0 );
1283 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1284 cleanup_thread( thread
);
1285 remove_process_thread( thread
->process
, thread
);
1286 release_object( thread
);
1289 /* copy parts of a context structure */
1290 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1292 assert( to
->machine
== from
->machine
);
1293 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1294 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1295 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1296 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1297 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1298 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1299 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1302 /* return the context flags that correspond to system regs */
1303 /* (system regs are the ones we can't access on the client side) */
1304 static unsigned int get_context_system_regs( unsigned short machine
)
1308 case IMAGE_FILE_MACHINE_I386
: return SERVER_CTX_DEBUG_REGISTERS
;
1309 case IMAGE_FILE_MACHINE_AMD64
: return SERVER_CTX_DEBUG_REGISTERS
;
1310 case IMAGE_FILE_MACHINE_ARMNT
: return SERVER_CTX_DEBUG_REGISTERS
;
1311 case IMAGE_FILE_MACHINE_ARM64
: return SERVER_CTX_DEBUG_REGISTERS
;
1316 /* gets the current impersonation token */
1317 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1320 return thread
->token
;
1322 return thread
->process
->token
;
1325 /* create a new thread */
1326 DECL_HANDLER(new_thread
)
1328 struct thread
*thread
;
1329 struct process
*process
;
1330 struct unicode_str name
;
1331 const struct security_descriptor
*sd
;
1332 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1333 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1335 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1337 if (request_fd
!= -1) close( request_fd
);
1341 if (process
!= current
->process
)
1343 if (request_fd
!= -1) /* can't create a request fd in a different process */
1345 close( request_fd
);
1346 set_error( STATUS_INVALID_PARAMETER
);
1349 if (process
->running_threads
) /* only the initial thread can be created in another process */
1351 set_error( STATUS_ACCESS_DENIED
);
1355 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1357 if (request_fd
!= -1) close( request_fd
);
1358 set_error( STATUS_INVALID_HANDLE
);
1362 if ((thread
= create_thread( request_fd
, process
, sd
)))
1364 thread
->system_regs
= current
->system_regs
;
1365 if (req
->suspend
) thread
->suspend
++;
1366 reply
->tid
= get_thread_id( thread
);
1367 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1368 req
->access
, objattr
->attributes
)))
1370 /* thread object will be released when the thread gets killed */
1373 kill_thread( thread
, 1 );
1376 release_object( process
);
1379 static int init_thread( struct thread
*thread
, int reply_fd
, int wait_fd
)
1381 if ((reply_fd
= thread_get_inflight_fd( thread
, reply_fd
)) == -1)
1383 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1386 if ((wait_fd
= thread_get_inflight_fd( thread
, wait_fd
)) == -1)
1388 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1392 if (thread
->reply_fd
) /* already initialised */
1394 set_error( STATUS_INVALID_PARAMETER
);
1398 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1400 thread
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, &thread
->obj
, 0 );
1401 thread
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, &thread
->obj
, 0 );
1402 return thread
->reply_fd
&& thread
->wait_fd
;
1405 if (reply_fd
!= -1) close( reply_fd
);
1406 if (wait_fd
!= -1) close( wait_fd
);
1410 /* initialize the first thread of a new process */
1411 DECL_HANDLER(init_first_thread
)
1413 struct process
*process
= current
->process
;
1415 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1417 current
->unix_pid
= process
->unix_pid
= req
->unix_pid
;
1418 current
->unix_tid
= req
->unix_tid
;
1420 if (!process
->parent_id
)
1421 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1423 set_thread_affinity( current
, current
->affinity
);
1425 debug_level
= max( debug_level
, req
->debug_level
);
1427 reply
->pid
= get_process_id( process
);
1428 reply
->tid
= get_thread_id( current
);
1429 reply
->info_size
= get_process_startup_info_size( process
);
1430 reply
->server_start
= server_start_time
;
1431 set_reply_data( supported_machines
,
1432 min( supported_machines_count
* sizeof(unsigned short), get_reply_max_size() ));
1435 /* initialize a new thread */
1436 DECL_HANDLER(init_thread
)
1438 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1440 if (!is_valid_address(req
->teb
))
1442 set_error( STATUS_INVALID_PARAMETER
);
1446 current
->unix_pid
= current
->process
->unix_pid
;
1447 current
->unix_tid
= req
->unix_tid
;
1448 current
->teb
= req
->teb
;
1449 current
->entry_point
= req
->entry
;
1451 init_thread_context( current
);
1452 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1453 set_thread_affinity( current
, current
->affinity
);
1455 reply
->pid
= get_process_id( current
->process
);
1456 reply
->tid
= get_thread_id( current
);
1457 reply
->suspend
= (current
->suspend
|| current
->process
->suspend
|| current
->context
!= NULL
);
1460 /* terminate a thread */
1461 DECL_HANDLER(terminate_thread
)
1463 struct thread
*thread
;
1465 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1467 thread
->exit_code
= req
->exit_code
;
1468 if (thread
!= current
) kill_thread( thread
, 1 );
1469 else reply
->self
= 1;
1470 release_object( thread
);
1474 /* open a handle to a thread */
1475 DECL_HANDLER(open_thread
)
1477 struct thread
*thread
= get_thread_from_id( req
->tid
);
1482 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1483 release_object( thread
);
1487 /* fetch information about a thread */
1488 DECL_HANDLER(get_thread_info
)
1490 struct thread
*thread
;
1491 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1493 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1494 thread
= get_thread_from_handle( req
->handle
, access
);
1497 reply
->pid
= get_process_id( thread
->process
);
1498 reply
->tid
= get_thread_id( thread
);
1499 reply
->teb
= thread
->teb
;
1500 reply
->entry_point
= thread
->entry_point
;
1501 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1502 reply
->priority
= thread
->priority
;
1503 reply
->affinity
= thread
->affinity
;
1504 reply
->last
= thread
->process
->running_threads
== 1;
1505 reply
->suspend_count
= thread
->suspend
;
1506 reply
->dbg_hidden
= thread
->dbg_hidden
;
1507 reply
->desc_len
= thread
->desc_len
;
1509 if (thread
->desc
&& get_reply_max_size())
1511 if (thread
->desc_len
<= get_reply_max_size())
1512 set_reply_data( thread
->desc
, thread
->desc_len
);
1514 set_error( STATUS_BUFFER_TOO_SMALL
);
1517 release_object( thread
);
1521 /* fetch information about thread times */
1522 DECL_HANDLER(get_thread_times
)
1524 struct thread
*thread
;
1526 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1528 reply
->creation_time
= thread
->creation_time
;
1529 reply
->exit_time
= thread
->exit_time
;
1530 reply
->unix_pid
= thread
->unix_pid
;
1531 reply
->unix_tid
= thread
->unix_tid
;
1533 release_object( thread
);
1537 /* set information about a thread */
1538 DECL_HANDLER(set_thread_info
)
1540 struct thread
*thread
;
1542 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1544 set_thread_info( thread
, req
);
1545 release_object( thread
);
1549 /* suspend a thread */
1550 DECL_HANDLER(suspend_thread
)
1552 struct thread
*thread
;
1554 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1556 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1557 else reply
->count
= suspend_thread( thread
);
1558 release_object( thread
);
1562 /* resume a thread */
1563 DECL_HANDLER(resume_thread
)
1565 struct thread
*thread
;
1567 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1569 reply
->count
= resume_thread( thread
);
1570 release_object( thread
);
1574 /* select on a handle list */
1575 DECL_HANDLER(select
)
1577 select_op_t select_op
;
1578 data_size_t op_size
;
1579 struct thread_apc
*apc
;
1580 const apc_result_t
*result
= get_req_data();
1582 if (get_req_data_size() < sizeof(*result
) ||
1583 get_req_data_size() - sizeof(*result
) < req
->size
||
1586 set_error( STATUS_INVALID_PARAMETER
);
1590 if (get_req_data_size() - sizeof(*result
) - req
->size
== sizeof(context_t
))
1592 const context_t
*context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1593 if ((current
->context
&& current
->context
->status
!= STATUS_PENDING
) ||
1594 context
->machine
!= current
->process
->machine
)
1596 set_error( STATUS_INVALID_PARAMETER
);
1600 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1601 copy_context( ¤t
->context
->regs
, context
,
1602 context
->flags
& ~(current
->context
->regs
.flags
| get_context_system_regs(current
->process
->machine
)) );
1603 current
->context
->status
= STATUS_SUCCESS
;
1604 current
->suspend_cookie
= req
->cookie
;
1605 wake_up( ¤t
->context
->obj
, 0 );
1610 set_error( STATUS_INVALID_PARAMETER
);
1614 op_size
= min( req
->size
, sizeof(select_op
) );
1615 memset( &select_op
, 0, sizeof(select_op
) );
1616 memcpy( &select_op
, result
+ 1, op_size
);
1618 /* first store results of previous apc */
1621 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1622 0, &thread_apc_ops
))) return;
1623 apc
->result
= *result
;
1625 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1627 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1628 apc
->caller
->process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1629 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1630 apc
->result
.create_thread
.handle
= handle
;
1631 clear_error(); /* ignore errors from the above calls */
1633 wake_up( &apc
->obj
, 0 );
1634 close_handle( current
->process
, req
->prev_apc
);
1635 release_object( apc
);
1638 select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1640 if (get_error() == STATUS_USER_APC
)
1642 apc
= thread_dequeue_apc( current
, 0 );
1643 reply
->call
= apc
->call
;
1644 release_object( apc
);
1646 else if (get_error() == STATUS_KERNEL_APC
)
1648 apc
= thread_dequeue_apc( current
, 1 );
1649 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1650 reply
->call
= apc
->call
;
1654 wake_up( &apc
->obj
, 0 );
1656 release_object( apc
);
1658 else if (get_error() != STATUS_PENDING
&& get_reply_max_size() == sizeof(context_t
) &&
1659 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1661 if (current
->context
->regs
.flags
)
1663 unsigned int system_flags
= get_context_system_regs(current
->process
->machine
) &
1664 current
->context
->regs
.flags
;
1665 if (system_flags
) set_thread_context( current
, ¤t
->context
->regs
, system_flags
);
1666 set_reply_data( ¤t
->context
->regs
, sizeof(context_t
) );
1668 release_object( current
->context
);
1669 current
->context
= NULL
;
1673 /* queue an APC for a thread or process */
1674 DECL_HANDLER(queue_apc
)
1676 struct thread
*thread
= NULL
;
1677 struct process
*process
= NULL
;
1678 struct thread_apc
*apc
;
1680 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1682 switch (apc
->call
.type
)
1686 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1688 case APC_VIRTUAL_ALLOC
:
1689 case APC_VIRTUAL_FREE
:
1690 case APC_VIRTUAL_PROTECT
:
1691 case APC_VIRTUAL_FLUSH
:
1692 case APC_VIRTUAL_LOCK
:
1693 case APC_VIRTUAL_UNLOCK
:
1694 case APC_UNMAP_VIEW
:
1695 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1697 case APC_VIRTUAL_QUERY
:
1698 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1701 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1702 if (process
&& process
!= current
->process
)
1704 /* duplicate the handle into the target process */
1705 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1706 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1707 if (handle
) apc
->call
.map_view
.handle
= handle
;
1710 release_object( process
);
1715 case APC_CREATE_THREAD
:
1716 case APC_BREAK_PROCESS
:
1717 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1719 case APC_DUP_HANDLE
:
1720 process
= get_process_from_handle( req
->handle
, PROCESS_DUP_HANDLE
);
1721 if (process
&& process
!= current
->process
)
1723 /* duplicate the destination process handle into the target process */
1724 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.dup_handle
.dst_process
,
1725 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1726 if (handle
) apc
->call
.dup_handle
.dst_process
= handle
;
1729 release_object( process
);
1735 set_error( STATUS_INVALID_PARAMETER
);
1741 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_UNSUCCESSFUL
);
1742 release_object( thread
);
1746 reply
->self
= (process
== current
->process
);
1749 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1752 if (queue_apc( process
, NULL
, apc
))
1754 apc
->caller
= (struct thread
*)grab_object( current
);
1755 reply
->handle
= handle
;
1759 close_handle( current
->process
, handle
);
1760 set_error( STATUS_PROCESS_IS_TERMINATING
);
1764 release_object( process
);
1767 release_object( apc
);
1770 /* Get the result of an APC call */
1771 DECL_HANDLER(get_apc_result
)
1773 struct thread_apc
*apc
;
1775 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1776 0, &thread_apc_ops
))) return;
1778 if (apc
->executed
) reply
->result
= apc
->result
;
1779 else set_error( STATUS_PENDING
);
1781 /* close the handle directly to avoid an extra round-trip */
1782 close_handle( current
->process
, req
->handle
);
1783 release_object( apc
);
1786 /* retrieve the current context of a thread */
1787 DECL_HANDLER(get_thread_context
)
1789 struct context
*thread_context
= NULL
;
1790 unsigned int system_flags
;
1791 struct thread
*thread
;
1794 if (get_reply_max_size() < sizeof(context_t
))
1796 set_error( STATUS_INVALID_PARAMETER
);
1800 if ((thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->handle
, 0, &context_ops
)))
1802 close_handle( current
->process
, req
->handle
); /* avoid extra server call */
1803 system_flags
= get_context_system_regs( thread_context
->regs
.machine
);
1805 else if ((thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
)))
1808 system_flags
= get_context_system_regs( thread
->process
->machine
);
1809 if (thread
->state
== RUNNING
)
1811 reply
->self
= (thread
== current
);
1812 if (thread
!= current
) stop_thread( thread
);
1813 if (thread
->context
)
1815 /* make sure that system regs are valid in thread context */
1816 if (thread
->unix_tid
!= -1 && (req
->flags
& system_flags
& ~thread
->context
->regs
.flags
))
1817 get_thread_context( thread
, &thread
->context
->regs
, req
->flags
& system_flags
);
1818 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1820 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1822 assert( reply
->self
);
1823 memset( context
, 0, sizeof(context_t
) );
1824 context
->machine
= thread
->process
->machine
;
1825 if (req
->flags
& system_flags
)
1827 get_thread_context( thread
, context
, req
->flags
& system_flags
);
1828 context
->flags
|= req
->flags
& system_flags
;
1832 else set_error( STATUS_UNSUCCESSFUL
);
1833 release_object( thread
);
1835 if (get_error() || !thread_context
) return;
1837 set_error( thread_context
->status
);
1838 if (!thread_context
->status
&& (context
= set_reply_data_size( sizeof(context_t
) )))
1840 memset( context
, 0, sizeof(context_t
) );
1841 context
->machine
= thread_context
->regs
.machine
;
1842 copy_context( context
, &thread_context
->regs
, req
->flags
);
1843 context
->flags
= req
->flags
;
1845 else if (thread_context
->status
== STATUS_PENDING
)
1847 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1850 release_object( thread_context
);
1853 /* set the current context of a thread */
1854 DECL_HANDLER(set_thread_context
)
1856 struct thread
*thread
;
1857 const context_t
*context
= get_req_data();
1859 if (get_req_data_size() < sizeof(context_t
))
1861 set_error( STATUS_INVALID_PARAMETER
);
1864 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1865 reply
->self
= (thread
== current
);
1867 if (thread
->state
== TERMINATED
) set_error( STATUS_UNSUCCESSFUL
);
1868 else if (context
->machine
== thread
->process
->machine
)
1870 unsigned int system_flags
= get_context_system_regs( context
->machine
) & context
->flags
;
1872 if (thread
!= current
) stop_thread( thread
);
1873 else if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1874 if (thread
->context
&& !get_error())
1876 copy_context( &thread
->context
->regs
, context
, context
->flags
);
1877 thread
->context
->regs
.flags
|= context
->flags
;
1880 else if (context
->machine
== IMAGE_FILE_MACHINE_AMD64
&&
1881 thread
->process
->machine
== IMAGE_FILE_MACHINE_I386
)
1883 /* convert the WoW64 context */
1884 unsigned int system_flags
= get_context_system_regs( context
->machine
) & context
->flags
;
1887 set_thread_context( thread
, context
, system_flags
);
1888 if (thread
->context
&& !get_error())
1890 thread
->context
->regs
.debug
.i386_regs
.dr0
= context
->debug
.x86_64_regs
.dr0
;
1891 thread
->context
->regs
.debug
.i386_regs
.dr1
= context
->debug
.x86_64_regs
.dr1
;
1892 thread
->context
->regs
.debug
.i386_regs
.dr2
= context
->debug
.x86_64_regs
.dr2
;
1893 thread
->context
->regs
.debug
.i386_regs
.dr3
= context
->debug
.x86_64_regs
.dr3
;
1894 thread
->context
->regs
.debug
.i386_regs
.dr6
= context
->debug
.x86_64_regs
.dr6
;
1895 thread
->context
->regs
.debug
.i386_regs
.dr7
= context
->debug
.x86_64_regs
.dr7
;
1899 else set_error( STATUS_INVALID_PARAMETER
);
1901 release_object( thread
);
1904 /* fetch a selector entry for a thread */
1905 DECL_HANDLER(get_selector_entry
)
1907 struct thread
*thread
;
1908 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1910 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1911 release_object( thread
);
1915 /* Iterate thread list for process. Use global thread list to also
1916 * return terminated but not yet destroyed threads. */
1917 DECL_HANDLER(get_next_thread
)
1919 struct thread
*thread
;
1920 struct process
*process
;
1925 set_error( STATUS_INVALID_PARAMETER
);
1929 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1934 ptr
= req
->flags
? list_tail( &thread_list
) : list_head( &thread_list
);
1936 else if ((thread
= get_thread_from_handle( req
->last
, 0 )))
1938 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
1939 : list_next( &thread_list
, &thread
->entry
);
1940 release_object( thread
);
1944 release_object( process
);
1950 thread
= LIST_ENTRY( ptr
, struct thread
, entry
);
1951 if (thread
->process
== process
)
1953 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1954 release_object( process
);
1957 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
1958 : list_next( &thread_list
, &thread
->entry
);
1960 set_error( STATUS_NO_MORE_ENTRIES
);
1961 release_object( process
);