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 /* flags for registers that always need to be set from the server side */
130 static const unsigned int system_flags
= SERVER_CTX_DEBUG_REGISTERS
;
131 /* flags for registers that are set from the native context even in WoW mode */
132 static const unsigned int always_native_flags
= SERVER_CTX_DEBUG_REGISTERS
| SERVER_CTX_FLOATING_POINT
| SERVER_CTX_YMM_REGISTERS
;
134 static void dump_context( struct object
*obj
, int verbose
);
135 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
137 static const struct object_ops context_ops
=
139 sizeof(struct context
), /* size */
141 dump_context
, /* dump */
142 add_queue
, /* add_queue */
143 remove_queue
, /* remove_queue */
144 context_signaled
, /* signaled */
145 no_satisfied
, /* satisfied */
146 no_signal
, /* signal */
147 no_get_fd
, /* get_fd */
148 default_map_access
, /* map_access */
149 default_get_sd
, /* get_sd */
150 default_set_sd
, /* set_sd */
151 no_get_full_name
, /* get_full_name */
152 no_lookup_name
, /* lookup_name */
153 no_link_name
, /* link_name */
154 NULL
, /* unlink_name */
155 no_open_file
, /* open_file */
156 no_kernel_obj_list
, /* get_kernel_obj_list */
157 no_close_handle
, /* close_handle */
158 no_destroy
/* destroy */
162 /* thread operations */
164 static const WCHAR thread_name
[] = {'T','h','r','e','a','d'};
166 struct type_descr thread_type
=
168 { thread_name
, sizeof(thread_name
) }, /* name */
169 THREAD_ALL_ACCESS
, /* valid_access */
171 STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
,
172 STANDARD_RIGHTS_WRITE
| THREAD_SET_LIMITED_INFORMATION
| THREAD_SET_INFORMATION
173 | THREAD_SET_CONTEXT
| THREAD_SUSPEND_RESUME
| THREAD_TERMINATE
| 0x04,
174 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_RESUME
| THREAD_QUERY_LIMITED_INFORMATION
,
179 static void dump_thread( struct object
*obj
, int verbose
);
180 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
181 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
182 static void thread_poll_event( struct fd
*fd
, int event
);
183 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
184 static void destroy_thread( struct object
*obj
);
186 static const struct object_ops thread_ops
=
188 sizeof(struct thread
), /* size */
189 &thread_type
, /* type */
190 dump_thread
, /* dump */
191 add_queue
, /* add_queue */
192 remove_queue
, /* remove_queue */
193 thread_signaled
, /* signaled */
194 no_satisfied
, /* satisfied */
195 no_signal
, /* signal */
196 no_get_fd
, /* get_fd */
197 thread_map_access
, /* map_access */
198 default_get_sd
, /* get_sd */
199 default_set_sd
, /* set_sd */
200 no_get_full_name
, /* get_full_name */
201 no_lookup_name
, /* lookup_name */
202 no_link_name
, /* link_name */
203 NULL
, /* unlink_name */
204 no_open_file
, /* open_file */
205 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
206 no_close_handle
, /* close_handle */
207 destroy_thread
/* destroy */
210 static const struct fd_ops thread_fd_ops
=
212 NULL
, /* get_poll_events */
213 thread_poll_event
, /* poll_event */
215 NULL
, /* get_fd_type */
217 NULL
, /* queue_async */
218 NULL
/* reselect_async */
221 static struct list thread_list
= LIST_INIT(thread_list
);
223 /* initialize the structure for a newly allocated thread */
224 static inline void init_thread_structure( struct thread
*thread
)
228 thread
->unix_pid
= -1; /* not known yet */
229 thread
->unix_tid
= -1; /* not known yet */
230 thread
->context
= NULL
;
232 thread
->entry_point
= 0;
233 thread
->system_regs
= 0;
234 thread
->queue
= NULL
;
237 thread
->req_data
= NULL
;
238 thread
->req_toread
= 0;
239 thread
->reply_data
= NULL
;
240 thread
->reply_towrite
= 0;
241 thread
->request_fd
= NULL
;
242 thread
->reply_fd
= NULL
;
243 thread
->wait_fd
= NULL
;
244 thread
->state
= RUNNING
;
245 thread
->exit_code
= 0;
246 thread
->priority
= 0;
248 thread
->dbg_hidden
= 0;
249 thread
->desktop_users
= 0;
250 thread
->token
= NULL
;
252 thread
->desc_len
= 0;
254 thread
->creation_time
= current_time
;
255 thread
->exit_time
= 0;
257 list_init( &thread
->mutex_list
);
258 list_init( &thread
->system_apc
);
259 list_init( &thread
->user_apc
);
260 list_init( &thread
->kernel_object
);
262 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
263 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
266 /* check if address looks valid for a client-side data structure (TEB etc.) */
267 static inline int is_valid_address( client_ptr_t addr
)
269 return addr
&& !(addr
% sizeof(int));
273 /* dump a context on stdout for debugging purposes */
274 static void dump_context( struct object
*obj
, int verbose
)
276 struct context
*context
= (struct context
*)obj
;
277 assert( obj
->ops
== &context_ops
);
279 fprintf( stderr
, "context flags=%x/%x\n",
280 context
->regs
[CTX_NATIVE
].flags
, context
->regs
[CTX_WOW
].flags
);
284 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
286 struct context
*context
= (struct context
*)obj
;
287 return context
->status
!= STATUS_PENDING
;
291 static struct context
*create_thread_context( struct thread
*thread
)
293 struct context
*context
;
294 if (!(context
= alloc_object( &context_ops
))) return NULL
;
295 context
->status
= STATUS_PENDING
;
296 memset( &context
->regs
, 0, sizeof(context
->regs
) );
297 context
->regs
[CTX_NATIVE
].machine
= native_machine
;
298 context
->regs
[CTX_PENDING
].machine
= native_machine
;
303 /* create a new thread */
304 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
306 struct desktop
*desktop
;
307 struct thread
*thread
;
312 if (pipe( request_pipe
) == -1)
317 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
319 close( request_pipe
[0] );
320 close( request_pipe
[1] );
323 close( request_pipe
[1] );
324 fd
= request_pipe
[0];
327 if (process
->is_terminating
)
330 set_error( STATUS_PROCESS_IS_TERMINATING
);
334 if (!(thread
= alloc_object( &thread_ops
)))
340 init_thread_structure( thread
);
342 thread
->process
= (struct process
*)grab_object( process
);
344 thread
->affinity
= process
->affinity
;
345 if (!current
) current
= thread
;
347 list_add_tail( &thread_list
, &thread
->entry
);
349 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
350 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
351 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
355 release_object( thread
);
358 if (!(thread
->id
= alloc_ptid( thread
)))
361 release_object( thread
);
364 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
366 release_object( thread
);
370 if (process
->desktop
)
372 if (!(desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error(); /* ignore errors */
375 set_thread_default_desktop( thread
, desktop
, process
->desktop
);
376 release_object( desktop
);
380 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
381 add_process_thread( thread
->process
, thread
);
385 /* handle a client event */
386 static void thread_poll_event( struct fd
*fd
, int event
)
388 struct thread
*thread
= get_fd_user( fd
);
389 assert( thread
->obj
.ops
== &thread_ops
);
391 grab_object( thread
);
392 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
393 else if (event
& POLLIN
) read_request( thread
);
394 else if (event
& POLLOUT
) write_reply( thread
);
395 release_object( thread
);
398 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
400 struct thread
*thread
= (struct thread
*)obj
;
401 return &thread
->kernel_object
;
404 /* cleanup everything that is no longer needed by a dead thread */
405 /* used by destroy_thread and kill_thread */
406 static void cleanup_thread( struct thread
*thread
)
412 thread
->context
->status
= STATUS_ACCESS_DENIED
;
413 wake_up( &thread
->context
->obj
, 0 );
414 release_object( thread
->context
);
415 thread
->context
= NULL
;
417 clear_apc_queue( &thread
->system_apc
);
418 clear_apc_queue( &thread
->user_apc
);
419 free( thread
->req_data
);
420 free( thread
->reply_data
);
421 if (thread
->request_fd
) release_object( thread
->request_fd
);
422 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
423 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
424 cleanup_clipboard_thread(thread
);
425 destroy_thread_windows( thread
);
426 free_msg_queue( thread
);
427 release_thread_desktop( thread
, 1 );
428 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
430 if (thread
->inflight
[i
].client
!= -1)
432 close( thread
->inflight
[i
].server
);
433 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
436 free( thread
->desc
);
437 thread
->req_data
= NULL
;
438 thread
->reply_data
= NULL
;
439 thread
->request_fd
= NULL
;
440 thread
->reply_fd
= NULL
;
441 thread
->wait_fd
= NULL
;
444 thread
->desc_len
= 0;
447 /* destroy a thread when its refcount is 0 */
448 static void destroy_thread( struct object
*obj
)
450 struct thread
*thread
= (struct thread
*)obj
;
451 assert( obj
->ops
== &thread_ops
);
453 list_remove( &thread
->entry
);
454 cleanup_thread( thread
);
455 release_object( thread
->process
);
456 if (thread
->id
) free_ptid( thread
->id
);
457 if (thread
->token
) release_object( thread
->token
);
460 /* dump a thread on stdout for debugging purposes */
461 static void dump_thread( struct object
*obj
, int verbose
)
463 struct thread
*thread
= (struct thread
*)obj
;
464 assert( obj
->ops
== &thread_ops
);
466 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
467 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
470 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
472 struct thread
*mythread
= (struct thread
*)obj
;
473 return (mythread
->state
== TERMINATED
);
476 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
478 access
= default_map_access( obj
, access
);
479 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
480 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
484 static void dump_thread_apc( struct object
*obj
, int verbose
)
486 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
487 assert( obj
->ops
== &thread_apc_ops
);
489 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
492 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
494 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
495 return apc
->executed
;
498 static void thread_apc_destroy( struct object
*obj
)
500 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
502 if (apc
->caller
) release_object( apc
->caller
);
505 if (apc
->result
.type
== APC_ASYNC_IO
)
506 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
507 else if (apc
->call
.type
== APC_ASYNC_IO
)
508 async_set_result( apc
->owner
, apc
->call
.async_io
.status
, 0 );
509 release_object( apc
->owner
);
513 /* queue an async procedure call */
514 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
516 struct thread_apc
*apc
;
518 if ((apc
= alloc_object( &thread_apc_ops
)))
520 apc
->call
= *call_data
;
524 apc
->result
.type
= APC_NONE
;
525 if (owner
) grab_object( owner
);
530 /* get a thread pointer from a thread id (and increment the refcount) */
531 struct thread
*get_thread_from_id( thread_id_t id
)
533 struct object
*obj
= get_ptid_entry( id
);
535 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
536 set_error( STATUS_INVALID_CID
);
540 /* get a thread from a handle (and increment the refcount) */
541 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
543 return (struct thread
*)get_handle_obj( current
->process
, handle
,
544 access
, &thread_ops
);
547 /* find a thread from a Unix tid */
548 struct thread
*get_thread_from_tid( int tid
)
550 struct thread
*thread
;
552 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
554 if (thread
->unix_tid
== tid
) return thread
;
559 /* find a thread from a Unix pid */
560 struct thread
*get_thread_from_pid( int pid
)
562 struct thread
*thread
;
564 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
566 if (thread
->unix_pid
== pid
) return thread
;
571 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
574 #ifdef HAVE_SCHED_SETAFFINITY
575 if (thread
->unix_tid
!= -1)
582 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
583 if (affinity
& mask
) CPU_SET( i
, &set
);
585 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
588 if (!ret
) thread
->affinity
= affinity
;
592 affinity_t
get_thread_affinity( struct thread
*thread
)
595 #ifdef HAVE_SCHED_SETAFFINITY
596 if (thread
->unix_tid
!= -1)
601 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
602 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
603 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
606 if (!mask
) mask
= ~(affinity_t
)0;
610 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
611 #define THREAD_PRIORITY_REALTIME_LOWEST -7
613 /* set all information about a thread */
614 static void set_thread_info( struct thread
*thread
,
615 const struct set_thread_info_request
*req
)
617 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
619 int max
= THREAD_PRIORITY_HIGHEST
;
620 int min
= THREAD_PRIORITY_LOWEST
;
621 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
623 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
624 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
626 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
627 req
->priority
== THREAD_PRIORITY_IDLE
||
628 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
629 thread
->priority
= req
->priority
;
631 set_error( STATUS_INVALID_PARAMETER
);
633 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
635 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
636 set_error( STATUS_INVALID_PARAMETER
);
637 else if (thread
->state
== TERMINATED
)
638 set_error( STATUS_THREAD_IS_TERMINATING
);
639 else if (set_thread_affinity( thread
, req
->affinity
))
642 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
643 security_set_thread_token( thread
, req
->token
);
644 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
645 thread
->entry_point
= req
->entry_point
;
646 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
647 thread
->dbg_hidden
= 1;
648 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
651 data_size_t desc_len
= get_req_data_size();
655 if ((desc
= mem_alloc( desc_len
)))
657 memcpy( desc
, get_req_data(), desc_len
);
658 free( thread
->desc
);
660 thread
->desc_len
= desc_len
;
665 free( thread
->desc
);
667 thread
->desc_len
= 0;
672 /* stop a thread (at the Unix level) */
673 void stop_thread( struct thread
*thread
)
675 if (thread
->context
) return; /* already suspended, no need for a signal */
676 if (!(thread
->context
= create_thread_context( thread
))) return;
677 /* can't stop a thread while initialisation is in progress */
678 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
681 /* suspend a thread */
682 int suspend_thread( struct thread
*thread
)
684 int old_count
= thread
->suspend
;
685 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
687 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
689 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
693 /* resume a thread */
694 int resume_thread( struct thread
*thread
)
696 int old_count
= thread
->suspend
;
697 if (thread
->suspend
> 0)
699 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
700 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
705 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
706 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
710 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
714 /* remove a thread from an object wait queue */
715 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
717 list_remove( &entry
->entry
);
718 release_object( obj
);
721 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
723 return entry
->wait
->thread
;
726 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
728 return entry
->wait
->select
;
731 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
733 return entry
->wait
->key
;
736 void make_wait_abandoned( struct wait_queue_entry
*entry
)
738 entry
->wait
->abandoned
= 1;
741 void set_wait_status( struct wait_queue_entry
*entry
, int status
)
743 entry
->wait
->status
= status
;
747 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
749 struct thread_wait
*wait
= thread
->wait
;
750 struct wait_queue_entry
*entry
;
754 thread
->wait
= wait
->next
;
756 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
758 wait
->status
= status
;
759 if (wait
->select
== SELECT_WAIT_ALL
)
761 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
762 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
766 entry
= wait
->queues
+ status
;
767 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
769 status
= wait
->status
;
770 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
772 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
773 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
774 if (wait
->user
) remove_timeout_user( wait
->user
);
779 /* build the thread wait structure */
780 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
781 int flags
, abstime_t when
)
783 struct thread_wait
*wait
;
784 struct wait_queue_entry
*entry
;
787 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
788 wait
->next
= current
->wait
;
789 wait
->thread
= current
;
792 wait
->select
= select_op
->op
;
797 current
->wait
= wait
;
799 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
801 struct object
*obj
= objects
[i
];
803 if (!obj
->ops
->add_queue( obj
, entry
))
806 end_wait( current
, get_error() );
813 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
814 int flags
, abstime_t when
)
816 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
820 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
822 for (i
= 0; i
< count
; i
++)
823 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
826 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
828 while (i
> 0) release_object( objects
[--i
] );
832 /* check if the thread waiting condition is satisfied */
833 static int check_wait( struct thread
*thread
)
836 struct thread_wait
*wait
= thread
->wait
;
837 struct wait_queue_entry
*entry
;
841 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
842 return STATUS_KERNEL_APC
;
844 /* Suspended threads may not acquire locks, but they can run system APCs */
845 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
847 if (wait
->select
== SELECT_WAIT_ALL
)
850 /* Note: we must check them all anyway, as some objects may
851 * want to do something when signaled, even if others are not */
852 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
853 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
854 if (!not_ok
) return STATUS_WAIT_0
;
858 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
859 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
862 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
863 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
864 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
868 /* send the wakeup signal to a thread */
869 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
871 struct wake_up_reply reply
;
874 /* check if we're waking current suspend wait */
875 if (thread
->context
&& thread
->suspend_cookie
== cookie
876 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
878 if (!thread
->context
->regs
[CTX_NATIVE
].flags
&& !thread
->context
->regs
[CTX_WOW
].flags
)
880 release_object( thread
->context
);
881 thread
->context
= NULL
;
883 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
886 memset( &reply
, 0, sizeof(reply
) );
887 reply
.cookie
= cookie
;
888 reply
.signaled
= signaled
;
889 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
892 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
893 else if (errno
== EPIPE
)
894 kill_thread( thread
, 0 ); /* normal death */
896 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
900 /* attempt to wake up a thread */
901 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
902 int wake_thread( struct thread
*thread
)
907 for (count
= 0; thread
->wait
; count
++)
909 if ((signaled
= check_wait( thread
)) == -1) break;
911 cookie
= thread
->wait
->cookie
;
912 signaled
= end_wait( thread
, signaled
);
913 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
914 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
916 if (!count
) count
= -1;
923 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
924 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
926 struct thread_wait
*wait
= entry
->wait
;
927 struct thread
*thread
= wait
->thread
;
931 if (thread
->wait
!= wait
) return 0; /* not the current wait */
932 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
934 assert( wait
->select
!= SELECT_WAIT_ALL
);
936 cookie
= wait
->cookie
;
937 signaled
= end_wait( thread
, entry
- wait
->queues
);
938 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
940 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
941 wake_thread( thread
); /* check other waits too */
946 /* thread wait timeout */
947 static void thread_timeout( void *ptr
)
949 struct thread_wait
*wait
= ptr
;
950 struct thread
*thread
= wait
->thread
;
951 client_ptr_t cookie
= wait
->cookie
;
954 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
955 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
957 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
958 end_wait( thread
, STATUS_TIMEOUT
);
961 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
962 /* check if other objects have become signaled in the meantime */
963 wake_thread( thread
);
966 /* try signaling an event flag, a semaphore or a mutex */
967 static int signal_object( obj_handle_t handle
)
972 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
975 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
976 release_object( obj
);
981 /* select on a list of handles */
982 static int select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
983 int flags
, abstime_t when
)
987 struct object
*object
;
989 switch (select_op
->op
)
992 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return 1;
996 case SELECT_WAIT_ALL
:
997 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
998 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
1000 set_error( STATUS_INVALID_PARAMETER
);
1003 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
1007 case SELECT_SIGNAL_AND_WAIT
:
1008 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
1010 if (select_op
->signal_and_wait
.signal
)
1012 if (!signal_object( select_op
->signal_and_wait
.signal
))
1014 end_wait( current
, get_error() );
1017 /* check if we woke ourselves up */
1018 if (!current
->wait
) return 1;
1022 case SELECT_KEYED_EVENT_WAIT
:
1023 case SELECT_KEYED_EVENT_RELEASE
:
1024 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1025 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1026 if (!object
) return 1;
1027 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1028 release_object( object
);
1030 current
->wait
->key
= select_op
->keyed_event
.key
;
1034 set_error( STATUS_INVALID_PARAMETER
);
1038 if ((ret
= check_wait( current
)) != -1)
1040 /* condition is already satisfied */
1041 set_error( end_wait( current
, ret
));
1045 /* now we need to wait */
1046 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1048 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1049 thread_timeout
, current
->wait
)))
1051 end_wait( current
, get_error() );
1055 current
->wait
->cookie
= cookie
;
1056 set_error( STATUS_PENDING
);
1060 /* attempt to wake threads sleeping on the object wait queue */
1061 void wake_up( struct object
*obj
, int max
)
1066 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1068 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1069 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1070 if (ret
> 0 && max
&& !--max
) break;
1071 /* restart at the head of the list since a wake up can change the object wait queue */
1072 ptr
= &obj
->wait_queue
;
1076 /* return the apc queue to use for a given apc type */
1077 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1084 return &thread
->user_apc
;
1086 return &thread
->system_apc
;
1090 /* check if thread is currently waiting for a (system) apc */
1091 static inline int is_in_apc_wait( struct thread
*thread
)
1093 return (thread
->process
->suspend
|| thread
->suspend
||
1094 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1097 /* queue an existing APC to a given thread */
1098 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1102 if (thread
&& thread
->state
== TERMINATED
&& process
)
1105 if (!thread
) /* find a suitable thread inside the process */
1107 struct thread
*candidate
;
1109 /* first try to find a waiting thread */
1110 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1112 if (candidate
->state
== TERMINATED
) continue;
1113 if (is_in_apc_wait( candidate
))
1121 /* then use the first one that accepts a signal */
1122 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1124 if (send_thread_signal( candidate
, SIGUSR1
))
1131 if (!thread
) return 0; /* nothing found */
1132 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1136 if (thread
->state
== TERMINATED
) return 0;
1137 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1138 /* send signal for system APCs if needed */
1139 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1141 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1143 /* cancel a possible previous APC with the same owner */
1144 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1148 list_add_tail( queue
, &apc
->entry
);
1149 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1150 wake_thread( thread
);
1155 /* queue an async procedure call */
1156 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1158 struct thread_apc
*apc
;
1161 if ((apc
= create_apc( owner
, call_data
)))
1163 ret
= queue_apc( process
, thread
, apc
);
1164 release_object( apc
);
1169 /* cancel the async procedure call owned by a specific object */
1170 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1172 struct thread_apc
*apc
;
1173 struct list
*queue
= get_apc_queue( thread
, type
);
1175 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1177 if (apc
->owner
!= owner
) continue;
1178 list_remove( &apc
->entry
);
1180 wake_up( &apc
->obj
, 0 );
1181 release_object( apc
);
1186 /* remove the head apc from the queue; the returned object must be released by the caller */
1187 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1189 struct thread_apc
*apc
= NULL
;
1190 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1194 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1200 /* clear an APC queue, cancelling all the APCs on it */
1201 static void clear_apc_queue( struct list
*queue
)
1205 while ((ptr
= list_head( queue
)))
1207 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1208 list_remove( &apc
->entry
);
1210 wake_up( &apc
->obj
, 0 );
1211 release_object( apc
);
1215 /* add an fd to the inflight list */
1216 /* return list index, or -1 on error */
1217 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1221 if (server
== -1) return -1;
1228 /* first check if we already have an entry for this fd */
1229 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1230 if (thread
->inflight
[i
].client
== client
)
1232 close( thread
->inflight
[i
].server
);
1233 thread
->inflight
[i
].server
= server
;
1237 /* now find a free spot to store it */
1238 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1239 if (thread
->inflight
[i
].client
== -1)
1241 thread
->inflight
[i
].client
= client
;
1242 thread
->inflight
[i
].server
= server
;
1250 /* get an inflight fd and purge it from the list */
1251 /* the fd must be closed when no longer used */
1252 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1256 if (client
== -1) return -1;
1260 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1262 if (thread
->inflight
[i
].client
== client
)
1264 ret
= thread
->inflight
[i
].server
;
1265 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1269 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1273 /* kill a thread on the spot */
1274 void kill_thread( struct thread
*thread
, int violent_death
)
1276 if (thread
->state
== TERMINATED
) return; /* already killed */
1277 thread
->state
= TERMINATED
;
1278 thread
->exit_time
= current_time
;
1279 if (current
== thread
) current
= NULL
;
1281 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1282 thread
->id
, thread
->exit_code
);
1285 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1286 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1287 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1290 kill_console_processes( thread
, 0 );
1291 abandon_mutexes( thread
);
1292 wake_up( &thread
->obj
, 0 );
1293 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1294 cleanup_thread( thread
);
1295 remove_process_thread( thread
->process
, thread
);
1296 release_object( thread
);
1299 /* copy parts of a context structure */
1300 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1302 assert( to
->machine
== from
->machine
);
1303 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1304 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1305 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1306 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1307 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1308 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1309 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1312 /* gets the current impersonation token */
1313 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1316 return thread
->token
;
1318 return thread
->process
->token
;
1321 /* create a new thread */
1322 DECL_HANDLER(new_thread
)
1324 struct thread
*thread
;
1325 struct process
*process
;
1326 struct unicode_str name
;
1327 const struct security_descriptor
*sd
;
1328 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1329 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1331 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1333 if (request_fd
!= -1) close( request_fd
);
1337 if (process
!= current
->process
)
1339 if (request_fd
!= -1) /* can't create a request fd in a different process */
1341 close( request_fd
);
1342 set_error( STATUS_INVALID_PARAMETER
);
1345 if (process
->running_threads
) /* only the initial thread can be created in another process */
1347 set_error( STATUS_ACCESS_DENIED
);
1351 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1353 if (request_fd
!= -1) close( request_fd
);
1354 set_error( STATUS_INVALID_HANDLE
);
1358 if ((thread
= create_thread( request_fd
, process
, sd
)))
1360 thread
->system_regs
= current
->system_regs
;
1361 if (req
->suspend
) thread
->suspend
++;
1362 reply
->tid
= get_thread_id( thread
);
1363 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1364 req
->access
, objattr
->attributes
)))
1366 /* thread object will be released when the thread gets killed */
1369 kill_thread( thread
, 1 );
1372 release_object( process
);
1375 static int init_thread( struct thread
*thread
, int reply_fd
, int wait_fd
)
1377 if ((reply_fd
= thread_get_inflight_fd( thread
, reply_fd
)) == -1)
1379 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1382 if ((wait_fd
= thread_get_inflight_fd( thread
, wait_fd
)) == -1)
1384 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1388 if (thread
->reply_fd
) /* already initialised */
1390 set_error( STATUS_INVALID_PARAMETER
);
1394 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1396 thread
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, &thread
->obj
, 0 );
1397 thread
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, &thread
->obj
, 0 );
1398 return thread
->reply_fd
&& thread
->wait_fd
;
1401 if (reply_fd
!= -1) close( reply_fd
);
1402 if (wait_fd
!= -1) close( wait_fd
);
1406 /* initialize the first thread of a new process */
1407 DECL_HANDLER(init_first_thread
)
1409 struct process
*process
= current
->process
;
1411 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1413 current
->unix_pid
= process
->unix_pid
= req
->unix_pid
;
1414 current
->unix_tid
= req
->unix_tid
;
1416 if (!process
->parent_id
)
1417 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1419 set_thread_affinity( current
, current
->affinity
);
1421 debug_level
= max( debug_level
, req
->debug_level
);
1423 reply
->pid
= get_process_id( process
);
1424 reply
->tid
= get_thread_id( current
);
1425 reply
->session_id
= process
->session_id
;
1426 reply
->info_size
= get_process_startup_info_size( process
);
1427 reply
->server_start
= server_start_time
;
1428 set_reply_data( supported_machines
,
1429 min( supported_machines_count
* sizeof(unsigned short), get_reply_max_size() ));
1432 /* initialize a new thread */
1433 DECL_HANDLER(init_thread
)
1435 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1437 if (!is_valid_address(req
->teb
))
1439 set_error( STATUS_INVALID_PARAMETER
);
1443 current
->unix_pid
= current
->process
->unix_pid
;
1444 current
->unix_tid
= req
->unix_tid
;
1445 current
->teb
= req
->teb
;
1446 current
->entry_point
= req
->entry
;
1448 init_thread_context( current
);
1449 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1450 set_thread_affinity( current
, current
->affinity
);
1452 reply
->suspend
= (current
->suspend
|| current
->process
->suspend
|| current
->context
!= NULL
);
1455 /* terminate a thread */
1456 DECL_HANDLER(terminate_thread
)
1458 struct thread
*thread
;
1460 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1462 thread
->exit_code
= req
->exit_code
;
1463 if (thread
!= current
) kill_thread( thread
, 1 );
1464 else reply
->self
= 1;
1465 release_object( thread
);
1469 /* open a handle to a thread */
1470 DECL_HANDLER(open_thread
)
1472 struct thread
*thread
= get_thread_from_id( req
->tid
);
1477 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1478 release_object( thread
);
1482 /* fetch information about a thread */
1483 DECL_HANDLER(get_thread_info
)
1485 struct thread
*thread
;
1486 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1488 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1489 thread
= get_thread_from_handle( req
->handle
, access
);
1492 reply
->pid
= get_process_id( thread
->process
);
1493 reply
->tid
= get_thread_id( thread
);
1494 reply
->teb
= thread
->teb
;
1495 reply
->entry_point
= thread
->entry_point
;
1496 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1497 reply
->priority
= thread
->priority
;
1498 reply
->affinity
= thread
->affinity
;
1499 reply
->last
= thread
->process
->running_threads
== 1;
1500 reply
->suspend_count
= thread
->suspend
;
1501 reply
->dbg_hidden
= thread
->dbg_hidden
;
1502 reply
->desc_len
= thread
->desc_len
;
1504 if (thread
->desc
&& get_reply_max_size())
1506 if (thread
->desc_len
<= get_reply_max_size())
1507 set_reply_data( thread
->desc
, thread
->desc_len
);
1509 set_error( STATUS_BUFFER_TOO_SMALL
);
1512 release_object( thread
);
1516 /* fetch information about thread times */
1517 DECL_HANDLER(get_thread_times
)
1519 struct thread
*thread
;
1521 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1523 reply
->creation_time
= thread
->creation_time
;
1524 reply
->exit_time
= thread
->exit_time
;
1525 reply
->unix_pid
= thread
->unix_pid
;
1526 reply
->unix_tid
= thread
->unix_tid
;
1528 release_object( thread
);
1532 /* set information about a thread */
1533 DECL_HANDLER(set_thread_info
)
1535 struct thread
*thread
;
1537 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1539 set_thread_info( thread
, req
);
1540 release_object( thread
);
1544 /* suspend a thread */
1545 DECL_HANDLER(suspend_thread
)
1547 struct thread
*thread
;
1549 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1551 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1552 else reply
->count
= suspend_thread( thread
);
1553 release_object( thread
);
1557 /* resume a thread */
1558 DECL_HANDLER(resume_thread
)
1560 struct thread
*thread
;
1562 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1564 reply
->count
= resume_thread( thread
);
1565 release_object( thread
);
1569 /* select on a handle list */
1570 DECL_HANDLER(select
)
1572 select_op_t select_op
;
1573 data_size_t op_size
, ctx_size
;
1574 struct context
*ctx
;
1575 struct thread_apc
*apc
;
1576 const apc_result_t
*result
= get_req_data();
1577 unsigned int ctx_count
;
1579 if (get_req_data_size() < sizeof(*result
)) goto invalid_param
;
1580 if (get_req_data_size() - sizeof(*result
) < req
->size
) goto invalid_param
;
1581 if (req
->size
& 3) goto invalid_param
;
1582 ctx_size
= get_req_data_size() - sizeof(*result
) - req
->size
;
1583 ctx_count
= ctx_size
/ sizeof(context_t
);
1584 if (ctx_count
* sizeof(context_t
) != ctx_size
) goto invalid_param
;
1585 if (ctx_count
> 1 + (current
->process
->machine
!= native_machine
)) goto invalid_param
;
1589 const context_t
*native_context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1590 const context_t
*wow_context
= (ctx_count
> 1) ? native_context
+ 1 : NULL
;
1592 if (current
->context
&& current
->context
->status
!= STATUS_PENDING
) goto invalid_param
;
1594 if (native_context
->machine
== native_machine
)
1596 if (wow_context
&& wow_context
->machine
!= current
->process
->machine
) goto invalid_param
;
1598 else if (native_context
->machine
== current
->process
->machine
)
1600 if (wow_context
) goto invalid_param
;
1601 wow_context
= native_context
;
1602 native_context
= NULL
;
1604 else goto invalid_param
;
1606 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1608 ctx
= current
->context
;
1611 copy_context( &ctx
->regs
[CTX_NATIVE
], native_context
,
1612 native_context
->flags
& ~(ctx
->regs
[CTX_NATIVE
].flags
| system_flags
) );
1616 ctx
->regs
[CTX_WOW
].machine
= current
->process
->machine
;
1617 copy_context( &ctx
->regs
[CTX_WOW
], wow_context
, wow_context
->flags
& ~ctx
->regs
[CTX_WOW
].flags
);
1619 else if (ctx
->regs
[CTX_PENDING
].flags
)
1621 unsigned int flags
= ctx
->regs
[CTX_PENDING
].flags
& ~ctx
->regs
[CTX_NATIVE
].flags
;
1622 copy_context( &ctx
->regs
[CTX_NATIVE
], &ctx
->regs
[CTX_PENDING
], flags
);
1623 ctx
->regs
[CTX_NATIVE
].flags
|= flags
;
1625 ctx
->regs
[CTX_PENDING
].flags
= 0;
1626 ctx
->status
= STATUS_SUCCESS
;
1627 current
->suspend_cookie
= req
->cookie
;
1628 wake_up( &ctx
->obj
, 0 );
1631 if (!req
->cookie
) goto invalid_param
;
1633 op_size
= min( req
->size
, sizeof(select_op
) );
1634 memset( &select_op
, 0, sizeof(select_op
) );
1635 memcpy( &select_op
, result
+ 1, op_size
);
1637 /* first store results of previous apc */
1640 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1641 0, &thread_apc_ops
))) return;
1642 apc
->result
= *result
;
1644 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1646 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1647 apc
->caller
->process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1648 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1649 apc
->result
.create_thread
.handle
= handle
;
1650 clear_error(); /* ignore errors from the above calls */
1652 wake_up( &apc
->obj
, 0 );
1653 close_handle( current
->process
, req
->prev_apc
);
1654 release_object( apc
);
1657 reply
->signaled
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1659 if (get_error() == STATUS_USER_APC
)
1661 apc
= thread_dequeue_apc( current
, 0 );
1662 reply
->call
= apc
->call
;
1663 release_object( apc
);
1665 else if (get_error() == STATUS_KERNEL_APC
)
1667 apc
= thread_dequeue_apc( current
, 1 );
1668 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1669 reply
->call
= apc
->call
;
1673 wake_up( &apc
->obj
, 0 );
1675 release_object( apc
);
1677 else if (reply
->signaled
&& get_reply_max_size() >= sizeof(context_t
) &&
1678 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1680 ctx
= current
->context
;
1681 if (ctx
->regs
[CTX_NATIVE
].flags
|| ctx
->regs
[CTX_WOW
].flags
)
1683 data_size_t size
= (ctx
->regs
[CTX_WOW
].flags
? 2 : 1) * sizeof(context_t
);
1684 unsigned int flags
= system_flags
& ctx
->regs
[CTX_NATIVE
].flags
;
1685 if (flags
) set_thread_context( current
, &ctx
->regs
[CTX_NATIVE
], flags
);
1686 set_reply_data( ctx
->regs
, min( size
, get_reply_max_size() ));
1688 release_object( ctx
);
1689 current
->context
= NULL
;
1694 set_error( STATUS_INVALID_PARAMETER
);
1697 /* queue an APC for a thread or process */
1698 DECL_HANDLER(queue_apc
)
1700 struct thread
*thread
= NULL
;
1701 struct process
*process
= NULL
;
1702 struct thread_apc
*apc
;
1704 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1706 switch (apc
->call
.type
)
1710 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1712 case APC_VIRTUAL_ALLOC
:
1713 case APC_VIRTUAL_FREE
:
1714 case APC_VIRTUAL_PROTECT
:
1715 case APC_VIRTUAL_FLUSH
:
1716 case APC_VIRTUAL_LOCK
:
1717 case APC_VIRTUAL_UNLOCK
:
1718 case APC_UNMAP_VIEW
:
1719 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1721 case APC_VIRTUAL_QUERY
:
1722 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1725 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1726 if (process
&& process
!= current
->process
)
1728 /* duplicate the handle into the target process */
1729 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1730 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1731 if (handle
) apc
->call
.map_view
.handle
= handle
;
1734 release_object( process
);
1739 case APC_CREATE_THREAD
:
1740 case APC_BREAK_PROCESS
:
1741 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1743 case APC_DUP_HANDLE
:
1744 process
= get_process_from_handle( req
->handle
, PROCESS_DUP_HANDLE
);
1745 if (process
&& process
!= current
->process
)
1747 /* duplicate the destination process handle into the target process */
1748 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.dup_handle
.dst_process
,
1749 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1750 if (handle
) apc
->call
.dup_handle
.dst_process
= handle
;
1753 release_object( process
);
1759 set_error( STATUS_INVALID_PARAMETER
);
1765 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_UNSUCCESSFUL
);
1766 release_object( thread
);
1770 reply
->self
= (process
== current
->process
);
1773 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1776 if (queue_apc( process
, NULL
, apc
))
1778 apc
->caller
= (struct thread
*)grab_object( current
);
1779 reply
->handle
= handle
;
1783 close_handle( current
->process
, handle
);
1784 set_error( STATUS_PROCESS_IS_TERMINATING
);
1788 release_object( process
);
1791 release_object( apc
);
1794 /* Get the result of an APC call */
1795 DECL_HANDLER(get_apc_result
)
1797 struct thread_apc
*apc
;
1799 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1800 0, &thread_apc_ops
))) return;
1802 if (apc
->executed
) reply
->result
= apc
->result
;
1803 else set_error( STATUS_PENDING
);
1805 /* close the handle directly to avoid an extra round-trip */
1806 close_handle( current
->process
, req
->handle
);
1807 release_object( apc
);
1810 /* retrieve the current context of a thread */
1811 DECL_HANDLER(get_thread_context
)
1813 struct context
*thread_context
= NULL
;
1814 struct thread
*thread
;
1817 if (get_reply_max_size() < 2 * sizeof(context_t
))
1819 set_error( STATUS_INVALID_PARAMETER
);
1825 if (!(thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->context
,
1828 close_handle( current
->process
, req
->context
); /* avoid extra server call */
1832 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1833 if (req
->machine
!= native_machine
&& req
->machine
!= thread
->process
->machine
)
1834 set_error( STATUS_INVALID_PARAMETER
);
1835 else if (thread
->state
!= RUNNING
)
1836 set_error( STATUS_UNSUCCESSFUL
);
1839 reply
->self
= (thread
== current
);
1840 if (thread
!= current
) stop_thread( thread
);
1841 if (thread
->context
)
1843 /* make sure that system regs are valid in thread context */
1844 if (thread
->unix_tid
!= -1 && (system_flags
& ~thread
->context
->regs
[CTX_NATIVE
].flags
))
1845 get_thread_context( thread
, &thread
->context
->regs
[CTX_NATIVE
], system_flags
);
1846 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1848 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1850 assert( reply
->self
);
1851 memset( context
, 0, sizeof(context_t
) );
1852 context
->machine
= native_machine
;
1853 if (system_flags
) get_thread_context( thread
, context
, system_flags
);
1856 release_object( thread
);
1857 if (!thread_context
) return;
1860 if (!thread_context
->status
)
1862 unsigned int native_flags
= req
->flags
, wow_flags
= 0;
1864 if (req
->machine
== thread_context
->regs
[CTX_WOW
].machine
)
1866 native_flags
= req
->flags
& always_native_flags
;
1867 wow_flags
= req
->flags
& ~always_native_flags
;
1869 if ((context
= set_reply_data_size( (!!native_flags
+ !!wow_flags
) * sizeof(context_t
) )))
1873 memset( context
, 0, sizeof(*context
) );
1874 context
->machine
= thread_context
->regs
[CTX_NATIVE
].machine
;
1875 copy_context( context
, &thread_context
->regs
[CTX_NATIVE
], native_flags
);
1876 context
->flags
= native_flags
;
1881 memset( context
, 0, sizeof(*context
) );
1882 context
->machine
= thread_context
->regs
[CTX_WOW
].machine
;
1883 copy_context( context
, &thread_context
->regs
[CTX_WOW
], wow_flags
);
1884 context
->flags
= wow_flags
;
1890 set_error( thread_context
->status
);
1891 if (thread_context
->status
== STATUS_PENDING
)
1892 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1895 release_object( thread_context
);
1898 /* set the current context of a thread */
1899 DECL_HANDLER(set_thread_context
)
1901 struct thread
*thread
;
1902 const context_t
*contexts
= get_req_data();
1903 unsigned int ctx_count
= get_req_data_size() / sizeof(context_t
);
1905 if (!ctx_count
|| ctx_count
> 2 || ctx_count
* sizeof(context_t
) != get_req_data_size())
1907 set_error( STATUS_INVALID_PARAMETER
);
1911 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1912 reply
->self
= (thread
== current
);
1914 if (contexts
[CTX_NATIVE
].machine
!= native_machine
||
1915 (ctx_count
== 2 && contexts
[CTX_WOW
].machine
!= thread
->process
->machine
))
1916 set_error( STATUS_INVALID_PARAMETER
);
1917 else if (thread
->state
!= TERMINATED
)
1919 unsigned int ctx
= CTX_NATIVE
;
1920 const context_t
*context
= &contexts
[CTX_NATIVE
];
1921 unsigned int flags
= system_flags
& context
->flags
;
1922 unsigned int native_flags
= always_native_flags
& context
->flags
;
1924 if (thread
!= current
) stop_thread( thread
);
1925 else if (flags
) set_thread_context( thread
, context
, flags
);
1926 if (thread
->context
&& !get_error())
1930 /* If the target thread doesn't have a WoW context, set native instead.
1931 * If we don't know yet whether we have a WoW context, store native context
1932 * in CTX_PENDING and update when the target thread sends its context(s). */
1933 if (thread
->context
->status
!= STATUS_PENDING
)
1935 ctx
= thread
->context
->regs
[CTX_WOW
].machine
? CTX_WOW
: CTX_NATIVE
;
1936 context
= &contexts
[ctx
];
1938 else ctx
= CTX_PENDING
;
1940 flags
= context
->flags
;
1941 if (native_flags
&& ctx
!= CTX_NATIVE
) /* some regs are always set from the native context */
1943 copy_context( &thread
->context
->regs
[CTX_NATIVE
], &contexts
[CTX_NATIVE
], native_flags
);
1944 thread
->context
->regs
[CTX_NATIVE
].flags
|= native_flags
;
1945 flags
&= ~native_flags
;
1947 copy_context( &thread
->context
->regs
[ctx
], context
, flags
);
1948 thread
->context
->regs
[ctx
].flags
|= flags
;
1951 else set_error( STATUS_UNSUCCESSFUL
);
1953 release_object( thread
);
1956 /* fetch a selector entry for a thread */
1957 DECL_HANDLER(get_selector_entry
)
1959 struct thread
*thread
;
1960 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1962 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1963 release_object( thread
);
1967 /* Iterate thread list for process. Use global thread list to also
1968 * return terminated but not yet destroyed threads. */
1969 DECL_HANDLER(get_next_thread
)
1971 struct thread
*thread
;
1972 struct process
*process
;
1977 set_error( STATUS_INVALID_PARAMETER
);
1981 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1986 ptr
= req
->flags
? list_tail( &thread_list
) : list_head( &thread_list
);
1988 else if ((thread
= get_thread_from_handle( req
->last
, 0 )))
1990 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
1991 : list_next( &thread_list
, &thread
->entry
);
1992 release_object( thread
);
1996 release_object( process
);
2002 thread
= LIST_ENTRY( ptr
, struct thread
, entry
);
2003 if (thread
->process
== process
)
2005 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
2006 release_object( process
);
2009 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
2010 : list_next( &thread_list
, &thread
->entry
);
2012 set_error( STATUS_NO_MORE_ENTRIES
);
2013 release_object( process
);