2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include <sys/types.h>
36 /* FreeBSD needs this for cpu_set_t instead of its cpuset_t */
37 #define _WITH_CPU_SET_T
42 #define WIN32_NO_STATUS
59 struct thread_wait
*next
; /* next wait structure for this thread */
60 struct thread
*thread
; /* owner thread */
61 int count
; /* count of objects */
64 enum select_op select
;
65 client_ptr_t key
; /* wait key for keyed events */
66 client_ptr_t cookie
; /* magic cookie to return to client */
68 struct timeout_user
*user
;
69 int status
; /* status to return (unless STATUS_PENDING) */
70 struct wait_queue_entry queues
[1];
73 /* asynchronous procedure calls */
77 struct object obj
; /* object header */
78 struct list entry
; /* queue linked list */
79 struct thread
*caller
; /* thread that queued this apc */
80 struct object
*owner
; /* object that queued this apc */
81 int executed
; /* has it been executed by the client? */
82 apc_call_t call
; /* call arguments */
83 apc_result_t result
; /* call results once executed */
86 static void dump_thread_apc( struct object
*obj
, int verbose
);
87 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
88 static void thread_apc_destroy( struct object
*obj
);
89 static void clear_apc_queue( struct list
*queue
);
91 static const struct object_ops thread_apc_ops
=
93 sizeof(struct thread_apc
), /* size */
95 dump_thread_apc
, /* dump */
96 add_queue
, /* add_queue */
97 remove_queue
, /* remove_queue */
98 thread_apc_signaled
, /* signaled */
99 no_satisfied
, /* satisfied */
100 no_signal
, /* signal */
101 no_get_fd
, /* get_fd */
102 default_map_access
, /* map_access */
103 default_get_sd
, /* get_sd */
104 default_set_sd
, /* set_sd */
105 no_get_full_name
, /* get_full_name */
106 no_lookup_name
, /* lookup_name */
107 no_link_name
, /* link_name */
108 NULL
, /* unlink_name */
109 no_open_file
, /* open_file */
110 no_kernel_obj_list
, /* get_kernel_obj_list */
111 no_close_handle
, /* close_handle */
112 thread_apc_destroy
/* destroy */
116 /* thread CPU context */
120 struct object obj
; /* object header */
121 unsigned int status
; /* status of the context */
122 context_t regs
[3]; /* context data */
124 #define CTX_NATIVE 0 /* context for native machine */
125 #define CTX_WOW 1 /* context if thread is inside WoW */
126 #define CTX_PENDING 2 /* pending native context when we don't know whether thread is inside WoW */
128 /* flags for registers that always need to be set from the server side */
129 static const unsigned int system_flags
= SERVER_CTX_DEBUG_REGISTERS
;
131 static void dump_context( struct object
*obj
, int verbose
);
132 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
134 static const struct object_ops context_ops
=
136 sizeof(struct context
), /* size */
138 dump_context
, /* dump */
139 add_queue
, /* add_queue */
140 remove_queue
, /* remove_queue */
141 context_signaled
, /* signaled */
142 no_satisfied
, /* satisfied */
143 no_signal
, /* signal */
144 no_get_fd
, /* get_fd */
145 default_map_access
, /* map_access */
146 default_get_sd
, /* get_sd */
147 default_set_sd
, /* set_sd */
148 no_get_full_name
, /* get_full_name */
149 no_lookup_name
, /* lookup_name */
150 no_link_name
, /* link_name */
151 NULL
, /* unlink_name */
152 no_open_file
, /* open_file */
153 no_kernel_obj_list
, /* get_kernel_obj_list */
154 no_close_handle
, /* close_handle */
155 no_destroy
/* destroy */
159 /* thread operations */
161 static const WCHAR thread_name
[] = {'T','h','r','e','a','d'};
163 struct type_descr thread_type
=
165 { thread_name
, sizeof(thread_name
) }, /* name */
166 THREAD_ALL_ACCESS
, /* valid_access */
168 STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
,
169 STANDARD_RIGHTS_WRITE
| THREAD_SET_LIMITED_INFORMATION
| THREAD_SET_INFORMATION
170 | THREAD_SET_CONTEXT
| THREAD_SUSPEND_RESUME
| THREAD_TERMINATE
| 0x04,
171 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_RESUME
| THREAD_QUERY_LIMITED_INFORMATION
,
176 static void dump_thread( struct object
*obj
, int verbose
);
177 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
178 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
179 static void thread_poll_event( struct fd
*fd
, int event
);
180 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
181 static void destroy_thread( struct object
*obj
);
183 static const struct object_ops thread_ops
=
185 sizeof(struct thread
), /* size */
186 &thread_type
, /* type */
187 dump_thread
, /* dump */
188 add_queue
, /* add_queue */
189 remove_queue
, /* remove_queue */
190 thread_signaled
, /* signaled */
191 no_satisfied
, /* satisfied */
192 no_signal
, /* signal */
193 no_get_fd
, /* get_fd */
194 thread_map_access
, /* map_access */
195 default_get_sd
, /* get_sd */
196 default_set_sd
, /* set_sd */
197 no_get_full_name
, /* get_full_name */
198 no_lookup_name
, /* lookup_name */
199 no_link_name
, /* link_name */
200 NULL
, /* unlink_name */
201 no_open_file
, /* open_file */
202 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
203 no_close_handle
, /* close_handle */
204 destroy_thread
/* destroy */
207 static const struct fd_ops thread_fd_ops
=
209 NULL
, /* get_poll_events */
210 thread_poll_event
, /* poll_event */
212 NULL
, /* get_fd_type */
214 NULL
, /* queue_async */
215 NULL
/* reselect_async */
218 static struct list thread_list
= LIST_INIT(thread_list
);
220 /* initialize the structure for a newly allocated thread */
221 static inline void init_thread_structure( struct thread
*thread
)
225 thread
->unix_pid
= -1; /* not known yet */
226 thread
->unix_tid
= -1; /* not known yet */
227 thread
->context
= NULL
;
229 thread
->entry_point
= 0;
230 thread
->system_regs
= 0;
231 thread
->queue
= NULL
;
234 thread
->req_data
= NULL
;
235 thread
->req_toread
= 0;
236 thread
->reply_data
= NULL
;
237 thread
->reply_towrite
= 0;
238 thread
->request_fd
= NULL
;
239 thread
->reply_fd
= NULL
;
240 thread
->wait_fd
= NULL
;
241 thread
->state
= RUNNING
;
242 thread
->exit_code
= 0;
243 thread
->priority
= 0;
245 thread
->dbg_hidden
= 0;
246 thread
->desktop_users
= 0;
247 thread
->token
= NULL
;
249 thread
->desc_len
= 0;
251 thread
->creation_time
= current_time
;
252 thread
->exit_time
= 0;
254 list_init( &thread
->mutex_list
);
255 list_init( &thread
->system_apc
);
256 list_init( &thread
->user_apc
);
257 list_init( &thread
->kernel_object
);
259 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
260 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
263 /* check if address looks valid for a client-side data structure (TEB etc.) */
264 static inline int is_valid_address( client_ptr_t addr
)
266 return addr
&& !(addr
% sizeof(int));
270 /* dump a context on stdout for debugging purposes */
271 static void dump_context( struct object
*obj
, int verbose
)
273 struct context
*context
= (struct context
*)obj
;
274 assert( obj
->ops
== &context_ops
);
276 fprintf( stderr
, "context flags=%x/%x\n",
277 context
->regs
[CTX_NATIVE
].flags
, context
->regs
[CTX_WOW
].flags
);
281 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
283 struct context
*context
= (struct context
*)obj
;
284 return context
->status
!= STATUS_PENDING
;
288 static struct context
*create_thread_context( struct thread
*thread
)
290 struct context
*context
;
291 if (!(context
= alloc_object( &context_ops
))) return NULL
;
292 context
->status
= STATUS_PENDING
;
293 memset( &context
->regs
, 0, sizeof(context
->regs
) );
294 context
->regs
[CTX_NATIVE
].machine
= native_machine
;
295 context
->regs
[CTX_PENDING
].machine
= native_machine
;
300 /* create a new thread */
301 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
303 struct desktop
*desktop
;
304 struct thread
*thread
;
309 if (pipe( request_pipe
) == -1)
314 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
316 close( request_pipe
[0] );
317 close( request_pipe
[1] );
320 close( request_pipe
[1] );
321 fd
= request_pipe
[0];
324 if (process
->is_terminating
)
327 set_error( STATUS_PROCESS_IS_TERMINATING
);
331 if (!(thread
= alloc_object( &thread_ops
)))
337 init_thread_structure( thread
);
339 thread
->process
= (struct process
*)grab_object( process
);
341 thread
->affinity
= process
->affinity
;
342 if (!current
) current
= thread
;
344 list_add_tail( &thread_list
, &thread
->entry
);
346 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
347 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
348 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
352 release_object( thread
);
355 if (!(thread
->id
= alloc_ptid( thread
)))
358 release_object( thread
);
361 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
363 release_object( thread
);
367 if (process
->desktop
)
369 if (!(desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error(); /* ignore errors */
372 set_thread_default_desktop( thread
, desktop
, process
->desktop
);
373 release_object( desktop
);
377 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
378 add_process_thread( thread
->process
, thread
);
382 /* handle a client event */
383 static void thread_poll_event( struct fd
*fd
, int event
)
385 struct thread
*thread
= get_fd_user( fd
);
386 assert( thread
->obj
.ops
== &thread_ops
);
388 grab_object( thread
);
389 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
390 else if (event
& POLLIN
) read_request( thread
);
391 else if (event
& POLLOUT
) write_reply( thread
);
392 release_object( thread
);
395 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
397 struct thread
*thread
= (struct thread
*)obj
;
398 return &thread
->kernel_object
;
401 /* cleanup everything that is no longer needed by a dead thread */
402 /* used by destroy_thread and kill_thread */
403 static void cleanup_thread( struct thread
*thread
)
409 thread
->context
->status
= STATUS_ACCESS_DENIED
;
410 wake_up( &thread
->context
->obj
, 0 );
411 release_object( thread
->context
);
412 thread
->context
= NULL
;
414 clear_apc_queue( &thread
->system_apc
);
415 clear_apc_queue( &thread
->user_apc
);
416 free( thread
->req_data
);
417 free( thread
->reply_data
);
418 if (thread
->request_fd
) release_object( thread
->request_fd
);
419 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
420 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
421 cleanup_clipboard_thread(thread
);
422 destroy_thread_windows( thread
);
423 free_msg_queue( thread
);
424 release_thread_desktop( thread
, 1 );
425 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
427 if (thread
->inflight
[i
].client
!= -1)
429 close( thread
->inflight
[i
].server
);
430 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
433 free( thread
->desc
);
434 thread
->req_data
= NULL
;
435 thread
->reply_data
= NULL
;
436 thread
->request_fd
= NULL
;
437 thread
->reply_fd
= NULL
;
438 thread
->wait_fd
= NULL
;
441 thread
->desc_len
= 0;
444 /* destroy a thread when its refcount is 0 */
445 static void destroy_thread( struct object
*obj
)
447 struct thread
*thread
= (struct thread
*)obj
;
448 assert( obj
->ops
== &thread_ops
);
450 list_remove( &thread
->entry
);
451 cleanup_thread( thread
);
452 release_object( thread
->process
);
453 if (thread
->id
) free_ptid( thread
->id
);
454 if (thread
->token
) release_object( thread
->token
);
457 /* dump a thread on stdout for debugging purposes */
458 static void dump_thread( struct object
*obj
, int verbose
)
460 struct thread
*thread
= (struct thread
*)obj
;
461 assert( obj
->ops
== &thread_ops
);
463 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
464 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
467 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
469 struct thread
*mythread
= (struct thread
*)obj
;
470 return (mythread
->state
== TERMINATED
);
473 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
475 access
= default_map_access( obj
, access
);
476 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
477 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
481 static void dump_thread_apc( struct object
*obj
, int verbose
)
483 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
484 assert( obj
->ops
== &thread_apc_ops
);
486 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
489 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
491 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
492 return apc
->executed
;
495 static void thread_apc_destroy( struct object
*obj
)
497 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
499 if (apc
->caller
) release_object( apc
->caller
);
502 if (apc
->result
.type
== APC_ASYNC_IO
)
503 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
504 else if (apc
->call
.type
== APC_ASYNC_IO
)
505 async_set_result( apc
->owner
, apc
->call
.async_io
.status
, 0 );
506 release_object( apc
->owner
);
510 /* queue an async procedure call */
511 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
513 struct thread_apc
*apc
;
515 if ((apc
= alloc_object( &thread_apc_ops
)))
517 apc
->call
= *call_data
;
521 apc
->result
.type
= APC_NONE
;
522 if (owner
) grab_object( owner
);
527 /* get a thread pointer from a thread id (and increment the refcount) */
528 struct thread
*get_thread_from_id( thread_id_t id
)
530 struct object
*obj
= get_ptid_entry( id
);
532 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
533 set_error( STATUS_INVALID_CID
);
537 /* get a thread from a handle (and increment the refcount) */
538 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
540 return (struct thread
*)get_handle_obj( current
->process
, handle
,
541 access
, &thread_ops
);
544 /* find a thread from a Unix tid */
545 struct thread
*get_thread_from_tid( int tid
)
547 struct thread
*thread
;
549 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
551 if (thread
->unix_tid
== tid
) return thread
;
556 /* find a thread from a Unix pid */
557 struct thread
*get_thread_from_pid( int pid
)
559 struct thread
*thread
;
561 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
563 if (thread
->unix_pid
== pid
) return thread
;
568 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
571 #ifdef HAVE_SCHED_SETAFFINITY
572 if (thread
->unix_tid
!= -1)
579 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
580 if (affinity
& mask
) CPU_SET( i
, &set
);
582 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
585 if (!ret
) thread
->affinity
= affinity
;
589 affinity_t
get_thread_affinity( struct thread
*thread
)
592 #ifdef HAVE_SCHED_SETAFFINITY
593 if (thread
->unix_tid
!= -1)
598 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
599 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
600 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
603 if (!mask
) mask
= ~(affinity_t
)0;
607 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
608 #define THREAD_PRIORITY_REALTIME_LOWEST -7
610 /* set all information about a thread */
611 static void set_thread_info( struct thread
*thread
,
612 const struct set_thread_info_request
*req
)
614 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
616 int max
= THREAD_PRIORITY_HIGHEST
;
617 int min
= THREAD_PRIORITY_LOWEST
;
618 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
620 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
621 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
623 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
624 req
->priority
== THREAD_PRIORITY_IDLE
||
625 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
626 thread
->priority
= req
->priority
;
628 set_error( STATUS_INVALID_PARAMETER
);
630 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
632 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
633 set_error( STATUS_INVALID_PARAMETER
);
634 else if (thread
->state
== TERMINATED
)
635 set_error( STATUS_THREAD_IS_TERMINATING
);
636 else if (set_thread_affinity( thread
, req
->affinity
))
639 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
640 security_set_thread_token( thread
, req
->token
);
641 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
642 thread
->entry_point
= req
->entry_point
;
643 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
644 thread
->dbg_hidden
= 1;
645 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
648 data_size_t desc_len
= get_req_data_size();
652 if ((desc
= mem_alloc( desc_len
)))
654 memcpy( desc
, get_req_data(), desc_len
);
655 free( thread
->desc
);
657 thread
->desc_len
= desc_len
;
662 free( thread
->desc
);
664 thread
->desc_len
= 0;
669 /* stop a thread (at the Unix level) */
670 void stop_thread( struct thread
*thread
)
672 if (thread
->context
) return; /* already suspended, no need for a signal */
673 if (!(thread
->context
= create_thread_context( thread
))) return;
674 /* can't stop a thread while initialisation is in progress */
675 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
678 /* suspend a thread */
679 int suspend_thread( struct thread
*thread
)
681 int old_count
= thread
->suspend
;
682 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
684 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
686 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
690 /* resume a thread */
691 int resume_thread( struct thread
*thread
)
693 int old_count
= thread
->suspend
;
694 if (thread
->suspend
> 0)
696 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
697 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
702 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
703 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
707 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
711 /* remove a thread from an object wait queue */
712 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
714 list_remove( &entry
->entry
);
715 release_object( obj
);
718 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
720 return entry
->wait
->thread
;
723 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
725 return entry
->wait
->select
;
728 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
730 return entry
->wait
->key
;
733 void make_wait_abandoned( struct wait_queue_entry
*entry
)
735 entry
->wait
->abandoned
= 1;
738 void set_wait_status( struct wait_queue_entry
*entry
, int status
)
740 entry
->wait
->status
= status
;
744 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
746 struct thread_wait
*wait
= thread
->wait
;
747 struct wait_queue_entry
*entry
;
751 thread
->wait
= wait
->next
;
753 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
755 wait
->status
= status
;
756 if (wait
->select
== SELECT_WAIT_ALL
)
758 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
759 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
763 entry
= wait
->queues
+ status
;
764 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
766 status
= wait
->status
;
767 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
769 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
770 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
771 if (wait
->user
) remove_timeout_user( wait
->user
);
776 /* build the thread wait structure */
777 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
778 int flags
, abstime_t when
)
780 struct thread_wait
*wait
;
781 struct wait_queue_entry
*entry
;
784 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
785 wait
->next
= current
->wait
;
786 wait
->thread
= current
;
789 wait
->select
= select_op
->op
;
794 current
->wait
= wait
;
796 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
798 struct object
*obj
= objects
[i
];
800 if (!obj
->ops
->add_queue( obj
, entry
))
803 end_wait( current
, get_error() );
810 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
811 int flags
, abstime_t when
)
813 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
817 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
819 for (i
= 0; i
< count
; i
++)
820 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
823 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
825 while (i
> 0) release_object( objects
[--i
] );
829 /* check if the thread waiting condition is satisfied */
830 static int check_wait( struct thread
*thread
)
833 struct thread_wait
*wait
= thread
->wait
;
834 struct wait_queue_entry
*entry
;
838 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
839 return STATUS_KERNEL_APC
;
841 /* Suspended threads may not acquire locks, but they can run system APCs */
842 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
844 if (wait
->select
== SELECT_WAIT_ALL
)
847 /* Note: we must check them all anyway, as some objects may
848 * want to do something when signaled, even if others are not */
849 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
850 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
851 if (!not_ok
) return STATUS_WAIT_0
;
855 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
856 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
859 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
860 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
861 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
865 /* send the wakeup signal to a thread */
866 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
868 struct wake_up_reply reply
;
871 /* check if we're waking current suspend wait */
872 if (thread
->context
&& thread
->suspend_cookie
== cookie
873 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
875 if (!thread
->context
->regs
[CTX_NATIVE
].flags
&& !thread
->context
->regs
[CTX_WOW
].flags
)
877 release_object( thread
->context
);
878 thread
->context
= NULL
;
880 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
883 memset( &reply
, 0, sizeof(reply
) );
884 reply
.cookie
= cookie
;
885 reply
.signaled
= signaled
;
886 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
889 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
890 else if (errno
== EPIPE
)
891 kill_thread( thread
, 0 ); /* normal death */
893 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
897 /* attempt to wake up a thread */
898 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
899 int wake_thread( struct thread
*thread
)
904 for (count
= 0; thread
->wait
; count
++)
906 if ((signaled
= check_wait( thread
)) == -1) break;
908 cookie
= thread
->wait
->cookie
;
909 signaled
= end_wait( thread
, signaled
);
910 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
911 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
913 if (!count
) count
= -1;
920 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
921 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
923 struct thread_wait
*wait
= entry
->wait
;
924 struct thread
*thread
= wait
->thread
;
928 if (thread
->wait
!= wait
) return 0; /* not the current wait */
929 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
931 assert( wait
->select
!= SELECT_WAIT_ALL
);
933 cookie
= wait
->cookie
;
934 signaled
= end_wait( thread
, entry
- wait
->queues
);
935 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
937 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
938 wake_thread( thread
); /* check other waits too */
943 /* thread wait timeout */
944 static void thread_timeout( void *ptr
)
946 struct thread_wait
*wait
= ptr
;
947 struct thread
*thread
= wait
->thread
;
948 client_ptr_t cookie
= wait
->cookie
;
951 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
952 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
954 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
955 end_wait( thread
, STATUS_TIMEOUT
);
958 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
959 /* check if other objects have become signaled in the meantime */
960 wake_thread( thread
);
963 /* try signaling an event flag, a semaphore or a mutex */
964 static int signal_object( obj_handle_t handle
)
969 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
972 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
973 release_object( obj
);
978 /* select on a list of handles */
979 static int select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
980 int flags
, abstime_t when
)
984 struct object
*object
;
986 switch (select_op
->op
)
989 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return 1;
993 case SELECT_WAIT_ALL
:
994 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
995 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
997 set_error( STATUS_INVALID_PARAMETER
);
1000 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
1004 case SELECT_SIGNAL_AND_WAIT
:
1005 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
1007 if (select_op
->signal_and_wait
.signal
)
1009 if (!signal_object( select_op
->signal_and_wait
.signal
))
1011 end_wait( current
, get_error() );
1014 /* check if we woke ourselves up */
1015 if (!current
->wait
) return 1;
1019 case SELECT_KEYED_EVENT_WAIT
:
1020 case SELECT_KEYED_EVENT_RELEASE
:
1021 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1022 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1023 if (!object
) return 1;
1024 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1025 release_object( object
);
1027 current
->wait
->key
= select_op
->keyed_event
.key
;
1031 set_error( STATUS_INVALID_PARAMETER
);
1035 if ((ret
= check_wait( current
)) != -1)
1037 /* condition is already satisfied */
1038 set_error( end_wait( current
, ret
));
1042 /* now we need to wait */
1043 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1045 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1046 thread_timeout
, current
->wait
)))
1048 end_wait( current
, get_error() );
1052 current
->wait
->cookie
= cookie
;
1053 set_error( STATUS_PENDING
);
1057 /* attempt to wake threads sleeping on the object wait queue */
1058 void wake_up( struct object
*obj
, int max
)
1063 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1065 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1066 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1067 if (ret
> 0 && max
&& !--max
) break;
1068 /* restart at the head of the list since a wake up can change the object wait queue */
1069 ptr
= &obj
->wait_queue
;
1073 /* return the apc queue to use for a given apc type */
1074 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1081 return &thread
->user_apc
;
1083 return &thread
->system_apc
;
1087 /* check if thread is currently waiting for a (system) apc */
1088 static inline int is_in_apc_wait( struct thread
*thread
)
1090 return (thread
->process
->suspend
|| thread
->suspend
||
1091 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1094 /* queue an existing APC to a given thread */
1095 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1099 if (thread
&& thread
->state
== TERMINATED
&& process
)
1102 if (!thread
) /* find a suitable thread inside the process */
1104 struct thread
*candidate
;
1106 /* first try to find a waiting thread */
1107 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1109 if (candidate
->state
== TERMINATED
) continue;
1110 if (is_in_apc_wait( candidate
))
1118 /* then use the first one that accepts a signal */
1119 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1121 if (send_thread_signal( candidate
, SIGUSR1
))
1128 if (!thread
) return 0; /* nothing found */
1129 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1133 if (thread
->state
== TERMINATED
) return 0;
1134 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1135 /* send signal for system APCs if needed */
1136 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1138 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1140 /* cancel a possible previous APC with the same owner */
1141 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1145 list_add_tail( queue
, &apc
->entry
);
1146 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1147 wake_thread( thread
);
1152 /* queue an async procedure call */
1153 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1155 struct thread_apc
*apc
;
1158 if ((apc
= create_apc( owner
, call_data
)))
1160 ret
= queue_apc( process
, thread
, apc
);
1161 release_object( apc
);
1166 /* cancel the async procedure call owned by a specific object */
1167 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1169 struct thread_apc
*apc
;
1170 struct list
*queue
= get_apc_queue( thread
, type
);
1172 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1174 if (apc
->owner
!= owner
) continue;
1175 list_remove( &apc
->entry
);
1177 wake_up( &apc
->obj
, 0 );
1178 release_object( apc
);
1183 /* remove the head apc from the queue; the returned object must be released by the caller */
1184 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1186 struct thread_apc
*apc
= NULL
;
1187 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1191 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1197 /* clear an APC queue, cancelling all the APCs on it */
1198 static void clear_apc_queue( struct list
*queue
)
1202 while ((ptr
= list_head( queue
)))
1204 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1205 list_remove( &apc
->entry
);
1207 wake_up( &apc
->obj
, 0 );
1208 release_object( apc
);
1212 /* add an fd to the inflight list */
1213 /* return list index, or -1 on error */
1214 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1218 if (server
== -1) return -1;
1225 /* first check if we already have an entry for this fd */
1226 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1227 if (thread
->inflight
[i
].client
== client
)
1229 close( thread
->inflight
[i
].server
);
1230 thread
->inflight
[i
].server
= server
;
1234 /* now find a free spot to store it */
1235 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1236 if (thread
->inflight
[i
].client
== -1)
1238 thread
->inflight
[i
].client
= client
;
1239 thread
->inflight
[i
].server
= server
;
1247 /* get an inflight fd and purge it from the list */
1248 /* the fd must be closed when no longer used */
1249 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1253 if (client
== -1) return -1;
1257 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1259 if (thread
->inflight
[i
].client
== client
)
1261 ret
= thread
->inflight
[i
].server
;
1262 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1266 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1270 /* kill a thread on the spot */
1271 void kill_thread( struct thread
*thread
, int violent_death
)
1273 if (thread
->state
== TERMINATED
) return; /* already killed */
1274 thread
->state
= TERMINATED
;
1275 thread
->exit_time
= current_time
;
1276 if (current
== thread
) current
= NULL
;
1278 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1279 thread
->id
, thread
->exit_code
);
1282 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1283 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1284 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1287 kill_console_processes( thread
, 0 );
1288 abandon_mutexes( thread
);
1289 wake_up( &thread
->obj
, 0 );
1290 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1291 cleanup_thread( thread
);
1292 remove_process_thread( thread
->process
, thread
);
1293 release_object( thread
);
1296 /* copy parts of a context structure */
1297 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1299 assert( to
->machine
== from
->machine
);
1300 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1301 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1302 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1303 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1304 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1305 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1306 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1309 /* gets the current impersonation token */
1310 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1313 return thread
->token
;
1315 return thread
->process
->token
;
1318 /* create a new thread */
1319 DECL_HANDLER(new_thread
)
1321 struct thread
*thread
;
1322 struct process
*process
;
1323 struct unicode_str name
;
1324 const struct security_descriptor
*sd
;
1325 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1326 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1328 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1330 if (request_fd
!= -1) close( request_fd
);
1334 if (process
!= current
->process
)
1336 if (request_fd
!= -1) /* can't create a request fd in a different process */
1338 close( request_fd
);
1339 set_error( STATUS_INVALID_PARAMETER
);
1342 if (process
->running_threads
) /* only the initial thread can be created in another process */
1344 set_error( STATUS_ACCESS_DENIED
);
1348 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1350 if (request_fd
!= -1) close( request_fd
);
1351 set_error( STATUS_INVALID_HANDLE
);
1355 if ((thread
= create_thread( request_fd
, process
, sd
)))
1357 thread
->system_regs
= current
->system_regs
;
1358 if (req
->flags
& THREAD_CREATE_FLAGS_CREATE_SUSPENDED
) thread
->suspend
++;
1359 thread
->dbg_hidden
= !!(req
->flags
& THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER
);
1360 reply
->tid
= get_thread_id( thread
);
1361 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1362 req
->access
, objattr
->attributes
)))
1364 /* thread object will be released when the thread gets killed */
1367 kill_thread( thread
, 1 );
1370 release_object( process
);
1373 static int init_thread( struct thread
*thread
, int reply_fd
, int wait_fd
)
1375 if ((reply_fd
= thread_get_inflight_fd( thread
, reply_fd
)) == -1)
1377 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1380 if ((wait_fd
= thread_get_inflight_fd( thread
, wait_fd
)) == -1)
1382 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1386 if (thread
->reply_fd
) /* already initialised */
1388 set_error( STATUS_INVALID_PARAMETER
);
1392 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1394 thread
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, &thread
->obj
, 0 );
1395 thread
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, &thread
->obj
, 0 );
1396 return thread
->reply_fd
&& thread
->wait_fd
;
1399 if (reply_fd
!= -1) close( reply_fd
);
1400 if (wait_fd
!= -1) close( wait_fd
);
1404 /* initialize the first thread of a new process */
1405 DECL_HANDLER(init_first_thread
)
1407 struct process
*process
= current
->process
;
1409 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1411 current
->unix_pid
= process
->unix_pid
= req
->unix_pid
;
1412 current
->unix_tid
= req
->unix_tid
;
1414 if (!process
->parent_id
)
1415 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1417 set_thread_affinity( current
, current
->affinity
);
1419 debug_level
= max( debug_level
, req
->debug_level
);
1421 reply
->pid
= get_process_id( process
);
1422 reply
->tid
= get_thread_id( current
);
1423 reply
->session_id
= process
->session_id
;
1424 reply
->info_size
= get_process_startup_info_size( process
);
1425 reply
->server_start
= server_start_time
;
1426 set_reply_data( supported_machines
,
1427 min( supported_machines_count
* sizeof(unsigned short), get_reply_max_size() ));
1430 /* initialize a new thread */
1431 DECL_HANDLER(init_thread
)
1433 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1435 if (!is_valid_address(req
->teb
))
1437 set_error( STATUS_INVALID_PARAMETER
);
1441 current
->unix_pid
= current
->process
->unix_pid
;
1442 current
->unix_tid
= req
->unix_tid
;
1443 current
->teb
= req
->teb
;
1444 current
->entry_point
= req
->entry
;
1446 init_thread_context( current
);
1447 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1448 set_thread_affinity( current
, current
->affinity
);
1450 reply
->suspend
= (current
->suspend
|| current
->process
->suspend
|| current
->context
!= NULL
);
1453 /* terminate a thread */
1454 DECL_HANDLER(terminate_thread
)
1456 struct thread
*thread
;
1458 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1460 thread
->exit_code
= req
->exit_code
;
1461 if (thread
!= current
) kill_thread( thread
, 1 );
1462 else reply
->self
= 1;
1463 cancel_terminating_thread_asyncs( thread
);
1464 release_object( thread
);
1468 /* open a handle to a thread */
1469 DECL_HANDLER(open_thread
)
1471 struct thread
*thread
= get_thread_from_id( req
->tid
);
1476 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1477 release_object( thread
);
1481 /* fetch information about a thread */
1482 DECL_HANDLER(get_thread_info
)
1484 struct thread
*thread
;
1485 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1487 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1488 thread
= get_thread_from_handle( req
->handle
, access
);
1491 reply
->pid
= get_process_id( thread
->process
);
1492 reply
->tid
= get_thread_id( thread
);
1493 reply
->teb
= thread
->teb
;
1494 reply
->entry_point
= thread
->entry_point
;
1495 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1496 reply
->priority
= thread
->priority
;
1497 reply
->affinity
= thread
->affinity
;
1498 reply
->last
= thread
->process
->running_threads
== 1;
1499 reply
->suspend_count
= thread
->suspend
;
1500 reply
->dbg_hidden
= thread
->dbg_hidden
;
1501 reply
->desc_len
= thread
->desc_len
;
1503 if (thread
->desc
&& get_reply_max_size())
1505 if (thread
->desc_len
<= get_reply_max_size())
1506 set_reply_data( thread
->desc
, thread
->desc_len
);
1508 set_error( STATUS_BUFFER_TOO_SMALL
);
1511 release_object( thread
);
1515 /* fetch information about thread times */
1516 DECL_HANDLER(get_thread_times
)
1518 struct thread
*thread
;
1520 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1522 reply
->creation_time
= thread
->creation_time
;
1523 reply
->exit_time
= thread
->exit_time
;
1524 reply
->unix_pid
= thread
->unix_pid
;
1525 reply
->unix_tid
= thread
->unix_tid
;
1527 release_object( thread
);
1531 /* set information about a thread */
1532 DECL_HANDLER(set_thread_info
)
1534 struct thread
*thread
;
1535 unsigned int access
= (req
->mask
== SET_THREAD_INFO_DESCRIPTION
) ? THREAD_SET_LIMITED_INFORMATION
1536 : THREAD_SET_INFORMATION
;
1538 if ((thread
= get_thread_from_handle( req
->handle
, access
)))
1540 set_thread_info( thread
, req
);
1541 release_object( thread
);
1545 /* suspend a thread */
1546 DECL_HANDLER(suspend_thread
)
1548 struct thread
*thread
;
1550 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1552 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1553 else reply
->count
= suspend_thread( thread
);
1554 release_object( thread
);
1558 /* resume a thread */
1559 DECL_HANDLER(resume_thread
)
1561 struct thread
*thread
;
1563 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1565 reply
->count
= resume_thread( thread
);
1566 release_object( thread
);
1570 /* select on a handle list */
1571 DECL_HANDLER(select
)
1573 select_op_t select_op
;
1574 data_size_t op_size
, ctx_size
;
1575 struct context
*ctx
;
1576 struct thread_apc
*apc
;
1577 const apc_result_t
*result
= get_req_data();
1578 unsigned int ctx_count
;
1580 if (get_req_data_size() < sizeof(*result
)) goto invalid_param
;
1581 if (get_req_data_size() - sizeof(*result
) < req
->size
) goto invalid_param
;
1582 if (req
->size
& 3) goto invalid_param
;
1583 ctx_size
= get_req_data_size() - sizeof(*result
) - req
->size
;
1584 ctx_count
= ctx_size
/ sizeof(context_t
);
1585 if (ctx_count
* sizeof(context_t
) != ctx_size
) goto invalid_param
;
1586 if (ctx_count
> 1 + (current
->process
->machine
!= native_machine
)) goto invalid_param
;
1590 const context_t
*native_context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1591 const context_t
*wow_context
= (ctx_count
> 1) ? native_context
+ 1 : NULL
;
1593 if (current
->context
&& current
->context
->status
!= STATUS_PENDING
) goto invalid_param
;
1595 if (native_context
->machine
== native_machine
)
1597 if (wow_context
&& wow_context
->machine
!= current
->process
->machine
) goto invalid_param
;
1599 else if (native_context
->machine
== current
->process
->machine
)
1601 if (wow_context
) goto invalid_param
;
1602 wow_context
= native_context
;
1603 native_context
= NULL
;
1605 else goto invalid_param
;
1607 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1609 ctx
= current
->context
;
1612 copy_context( &ctx
->regs
[CTX_NATIVE
], native_context
,
1613 native_context
->flags
& ~(ctx
->regs
[CTX_NATIVE
].flags
| system_flags
) );
1617 ctx
->regs
[CTX_WOW
].machine
= current
->process
->machine
;
1618 copy_context( &ctx
->regs
[CTX_WOW
], wow_context
, wow_context
->flags
& ~ctx
->regs
[CTX_WOW
].flags
);
1620 else if (ctx
->regs
[CTX_PENDING
].flags
)
1622 unsigned int flags
= ctx
->regs
[CTX_PENDING
].flags
& ~ctx
->regs
[CTX_NATIVE
].flags
;
1623 copy_context( &ctx
->regs
[CTX_NATIVE
], &ctx
->regs
[CTX_PENDING
], flags
);
1624 ctx
->regs
[CTX_NATIVE
].flags
|= flags
;
1626 ctx
->regs
[CTX_PENDING
].flags
= 0;
1627 ctx
->status
= STATUS_SUCCESS
;
1628 current
->suspend_cookie
= req
->cookie
;
1629 wake_up( &ctx
->obj
, 0 );
1632 if (!req
->cookie
) goto invalid_param
;
1634 op_size
= min( req
->size
, sizeof(select_op
) );
1635 memset( &select_op
, 0, sizeof(select_op
) );
1636 memcpy( &select_op
, result
+ 1, op_size
);
1638 /* first store results of previous apc */
1641 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1642 0, &thread_apc_ops
))) return;
1643 apc
->result
= *result
;
1645 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1647 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1648 apc
->caller
->process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1649 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1650 apc
->result
.create_thread
.handle
= handle
;
1651 clear_error(); /* ignore errors from the above calls */
1653 wake_up( &apc
->obj
, 0 );
1654 close_handle( current
->process
, req
->prev_apc
);
1655 release_object( apc
);
1658 reply
->signaled
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1660 if (get_error() == STATUS_USER_APC
)
1662 apc
= thread_dequeue_apc( current
, 0 );
1663 reply
->call
= apc
->call
;
1664 release_object( apc
);
1666 else if (get_error() == STATUS_KERNEL_APC
)
1668 apc
= thread_dequeue_apc( current
, 1 );
1669 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1670 reply
->call
= apc
->call
;
1674 wake_up( &apc
->obj
, 0 );
1676 release_object( apc
);
1678 else if (reply
->signaled
&& get_reply_max_size() >= sizeof(context_t
) &&
1679 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1681 ctx
= current
->context
;
1682 if (ctx
->regs
[CTX_NATIVE
].flags
|| ctx
->regs
[CTX_WOW
].flags
)
1684 data_size_t size
= (ctx
->regs
[CTX_WOW
].flags
? 2 : 1) * sizeof(context_t
);
1685 unsigned int flags
= system_flags
& ctx
->regs
[CTX_NATIVE
].flags
;
1686 if (flags
) set_thread_context( current
, &ctx
->regs
[CTX_NATIVE
], flags
);
1687 set_reply_data( ctx
->regs
, min( size
, get_reply_max_size() ));
1689 release_object( ctx
);
1690 current
->context
= NULL
;
1695 set_error( STATUS_INVALID_PARAMETER
);
1698 /* queue an APC for a thread or process */
1699 DECL_HANDLER(queue_apc
)
1701 struct thread
*thread
= NULL
;
1702 struct process
*process
= NULL
;
1703 struct thread_apc
*apc
;
1705 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1707 switch (apc
->call
.type
)
1711 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1713 case APC_VIRTUAL_ALLOC
:
1714 case APC_VIRTUAL_ALLOC_EX
:
1715 case APC_VIRTUAL_FREE
:
1716 case APC_VIRTUAL_PROTECT
:
1717 case APC_VIRTUAL_FLUSH
:
1718 case APC_VIRTUAL_LOCK
:
1719 case APC_VIRTUAL_UNLOCK
:
1720 case APC_UNMAP_VIEW
:
1721 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1723 case APC_VIRTUAL_QUERY
:
1724 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1727 case APC_MAP_VIEW_EX
:
1728 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1729 if (process
&& process
!= current
->process
)
1731 /* duplicate the handle into the target process */
1732 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1733 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1734 if (handle
) apc
->call
.map_view
.handle
= handle
;
1737 release_object( process
);
1742 case APC_CREATE_THREAD
:
1743 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1745 case APC_DUP_HANDLE
:
1746 process
= get_process_from_handle( req
->handle
, PROCESS_DUP_HANDLE
);
1747 if (process
&& process
!= current
->process
)
1749 /* duplicate the destination process handle into the target process */
1750 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.dup_handle
.dst_process
,
1751 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1752 if (handle
) apc
->call
.dup_handle
.dst_process
= handle
;
1755 release_object( process
);
1761 set_error( STATUS_INVALID_PARAMETER
);
1767 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_UNSUCCESSFUL
);
1768 release_object( thread
);
1772 reply
->self
= (process
== current
->process
);
1775 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1778 if (queue_apc( process
, NULL
, apc
))
1780 apc
->caller
= (struct thread
*)grab_object( current
);
1781 reply
->handle
= handle
;
1785 close_handle( current
->process
, handle
);
1786 set_error( STATUS_PROCESS_IS_TERMINATING
);
1790 release_object( process
);
1793 release_object( apc
);
1796 /* Get the result of an APC call */
1797 DECL_HANDLER(get_apc_result
)
1799 struct thread_apc
*apc
;
1801 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1802 0, &thread_apc_ops
))) return;
1804 if (apc
->executed
) reply
->result
= apc
->result
;
1805 else set_error( STATUS_PENDING
);
1807 /* close the handle directly to avoid an extra round-trip */
1808 close_handle( current
->process
, req
->handle
);
1809 release_object( apc
);
1812 /* retrieve the current context of a thread */
1813 DECL_HANDLER(get_thread_context
)
1815 struct context
*thread_context
= NULL
;
1816 struct thread
*thread
;
1819 if (get_reply_max_size() < 2 * sizeof(context_t
))
1821 set_error( STATUS_INVALID_PARAMETER
);
1827 if (!(thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->context
,
1830 close_handle( current
->process
, req
->context
); /* avoid extra server call */
1834 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1835 if (req
->machine
!= native_machine
&& req
->machine
!= thread
->process
->machine
)
1836 set_error( STATUS_INVALID_PARAMETER
);
1837 else if (thread
->state
!= RUNNING
)
1838 set_error( STATUS_UNSUCCESSFUL
);
1841 reply
->self
= (thread
== current
);
1842 if (thread
!= current
) stop_thread( thread
);
1843 if (thread
->context
)
1845 /* make sure that system regs are valid in thread context */
1846 if (thread
->unix_tid
!= -1 && (system_flags
& ~thread
->context
->regs
[CTX_NATIVE
].flags
))
1847 get_thread_context( thread
, &thread
->context
->regs
[CTX_NATIVE
], system_flags
);
1848 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1850 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1852 assert( reply
->self
);
1853 memset( context
, 0, sizeof(context_t
) );
1854 context
->machine
= native_machine
;
1855 if (system_flags
) get_thread_context( thread
, context
, system_flags
);
1858 release_object( thread
);
1859 if (!thread_context
) return;
1862 if (!thread_context
->status
)
1864 unsigned int native_flags
= req
->flags
, wow_flags
= 0;
1866 if (req
->machine
== thread_context
->regs
[CTX_WOW
].machine
)
1868 native_flags
= req
->native_flags
;
1869 wow_flags
= req
->flags
& ~native_flags
;
1871 if ((context
= set_reply_data_size( (!!native_flags
+ !!wow_flags
) * sizeof(context_t
) )))
1875 memset( context
, 0, sizeof(*context
) );
1876 context
->machine
= thread_context
->regs
[CTX_NATIVE
].machine
;
1877 copy_context( context
, &thread_context
->regs
[CTX_NATIVE
], native_flags
);
1878 context
->flags
= native_flags
;
1883 memset( context
, 0, sizeof(*context
) );
1884 context
->machine
= thread_context
->regs
[CTX_WOW
].machine
;
1885 copy_context( context
, &thread_context
->regs
[CTX_WOW
], wow_flags
);
1886 context
->flags
= wow_flags
;
1892 set_error( thread_context
->status
);
1893 if (thread_context
->status
== STATUS_PENDING
)
1894 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1897 release_object( thread_context
);
1900 /* set the current context of a thread */
1901 DECL_HANDLER(set_thread_context
)
1903 struct thread
*thread
;
1904 const context_t
*contexts
= get_req_data();
1905 unsigned int ctx_count
= get_req_data_size() / sizeof(context_t
);
1907 if (!ctx_count
|| ctx_count
> 2 || ctx_count
* sizeof(context_t
) != get_req_data_size())
1909 set_error( STATUS_INVALID_PARAMETER
);
1913 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1914 reply
->self
= (thread
== current
);
1916 if (contexts
[CTX_NATIVE
].machine
!= native_machine
||
1917 (ctx_count
== 2 && contexts
[CTX_WOW
].machine
!= thread
->process
->machine
))
1918 set_error( STATUS_INVALID_PARAMETER
);
1919 else if (thread
->state
!= TERMINATED
)
1921 unsigned int ctx
= CTX_NATIVE
;
1922 const context_t
*context
= &contexts
[CTX_NATIVE
];
1923 unsigned int flags
= system_flags
& context
->flags
;
1924 unsigned int native_flags
= context
->flags
& req
->native_flags
;
1926 if (thread
!= current
) stop_thread( thread
);
1927 else if (flags
) set_thread_context( thread
, context
, flags
);
1928 if (thread
->context
&& !get_error())
1932 /* If the target thread doesn't have a WoW context, set native instead.
1933 * If we don't know yet whether we have a WoW context, store native context
1934 * in CTX_PENDING and update when the target thread sends its context(s). */
1935 if (thread
->context
->status
!= STATUS_PENDING
)
1937 ctx
= thread
->context
->regs
[CTX_WOW
].machine
? CTX_WOW
: CTX_NATIVE
;
1938 context
= &contexts
[ctx
];
1940 else ctx
= CTX_PENDING
;
1942 flags
= context
->flags
;
1943 if (native_flags
&& ctx
!= CTX_NATIVE
) /* some regs are always set from the native context */
1945 copy_context( &thread
->context
->regs
[CTX_NATIVE
], &contexts
[CTX_NATIVE
], native_flags
);
1946 thread
->context
->regs
[CTX_NATIVE
].flags
|= native_flags
;
1947 flags
&= ~native_flags
;
1949 copy_context( &thread
->context
->regs
[ctx
], context
, flags
);
1950 thread
->context
->regs
[ctx
].flags
|= flags
;
1953 else set_error( STATUS_UNSUCCESSFUL
);
1955 release_object( thread
);
1958 /* fetch a selector entry for a thread */
1959 DECL_HANDLER(get_selector_entry
)
1961 struct thread
*thread
;
1962 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1964 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1965 release_object( thread
);
1969 /* Iterate thread list for process. Use global thread list to also
1970 * return terminated but not yet destroyed threads. */
1971 DECL_HANDLER(get_next_thread
)
1973 struct thread
*thread
;
1974 struct process
*process
;
1979 set_error( STATUS_INVALID_PARAMETER
);
1983 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1988 ptr
= req
->flags
? list_tail( &thread_list
) : list_head( &thread_list
);
1990 else if ((thread
= get_thread_from_handle( req
->last
, 0 )))
1992 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
1993 : list_next( &thread_list
, &thread
->entry
);
1994 release_object( thread
);
1998 release_object( process
);
2004 thread
= LIST_ENTRY( ptr
, struct thread
, entry
);
2005 if (thread
->process
== process
)
2007 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
2008 release_object( process
);
2011 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
2012 : list_next( &thread_list
, &thread
->entry
);
2014 set_error( STATUS_NO_MORE_ENTRIES
);
2015 release_object( process
);