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
[2]; /* context data */
124 #define CTX_NATIVE 0 /* context for native machine */
125 #define CTX_WOW 1 /* context if thread is inside WoW */
127 /* flags for registers that always need to be set from the server side */
128 static const unsigned int system_flags
= SERVER_CTX_DEBUG_REGISTERS
;
130 static void dump_context( struct object
*obj
, int verbose
);
131 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
133 static const struct object_ops context_ops
=
135 sizeof(struct context
), /* size */
137 dump_context
, /* dump */
138 add_queue
, /* add_queue */
139 remove_queue
, /* remove_queue */
140 context_signaled
, /* signaled */
141 no_satisfied
, /* satisfied */
142 no_signal
, /* signal */
143 no_get_fd
, /* get_fd */
144 default_map_access
, /* map_access */
145 default_get_sd
, /* get_sd */
146 default_set_sd
, /* set_sd */
147 no_get_full_name
, /* get_full_name */
148 no_lookup_name
, /* lookup_name */
149 no_link_name
, /* link_name */
150 NULL
, /* unlink_name */
151 no_open_file
, /* open_file */
152 no_kernel_obj_list
, /* get_kernel_obj_list */
153 no_close_handle
, /* close_handle */
154 no_destroy
/* destroy */
158 /* thread operations */
160 static const WCHAR thread_name
[] = {'T','h','r','e','a','d'};
162 struct type_descr thread_type
=
164 { thread_name
, sizeof(thread_name
) }, /* name */
165 THREAD_ALL_ACCESS
, /* valid_access */
167 STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
,
168 STANDARD_RIGHTS_WRITE
| THREAD_SET_LIMITED_INFORMATION
| THREAD_SET_INFORMATION
169 | THREAD_SET_CONTEXT
| THREAD_SUSPEND_RESUME
| THREAD_TERMINATE
| 0x04,
170 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_RESUME
| THREAD_QUERY_LIMITED_INFORMATION
,
175 static void dump_thread( struct object
*obj
, int verbose
);
176 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
177 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
178 static void thread_poll_event( struct fd
*fd
, int event
);
179 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
180 static void destroy_thread( struct object
*obj
);
182 static const struct object_ops thread_ops
=
184 sizeof(struct thread
), /* size */
185 &thread_type
, /* type */
186 dump_thread
, /* dump */
187 add_queue
, /* add_queue */
188 remove_queue
, /* remove_queue */
189 thread_signaled
, /* signaled */
190 no_satisfied
, /* satisfied */
191 no_signal
, /* signal */
192 no_get_fd
, /* get_fd */
193 thread_map_access
, /* map_access */
194 default_get_sd
, /* get_sd */
195 default_set_sd
, /* set_sd */
196 no_get_full_name
, /* get_full_name */
197 no_lookup_name
, /* lookup_name */
198 no_link_name
, /* link_name */
199 NULL
, /* unlink_name */
200 no_open_file
, /* open_file */
201 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
202 no_close_handle
, /* close_handle */
203 destroy_thread
/* destroy */
206 static const struct fd_ops thread_fd_ops
=
208 NULL
, /* get_poll_events */
209 thread_poll_event
, /* poll_event */
211 NULL
, /* get_fd_type */
213 NULL
, /* queue_async */
214 NULL
/* reselect_async */
217 static struct list thread_list
= LIST_INIT(thread_list
);
219 /* initialize the structure for a newly allocated thread */
220 static inline void init_thread_structure( struct thread
*thread
)
224 thread
->unix_pid
= -1; /* not known yet */
225 thread
->unix_tid
= -1; /* not known yet */
226 thread
->context
= NULL
;
228 thread
->entry_point
= 0;
229 thread
->system_regs
= 0;
230 thread
->queue
= NULL
;
233 thread
->req_data
= NULL
;
234 thread
->req_toread
= 0;
235 thread
->reply_data
= NULL
;
236 thread
->reply_towrite
= 0;
237 thread
->request_fd
= NULL
;
238 thread
->reply_fd
= NULL
;
239 thread
->wait_fd
= NULL
;
240 thread
->state
= RUNNING
;
241 thread
->exit_code
= 0;
242 thread
->priority
= 0;
244 thread
->dbg_hidden
= 0;
245 thread
->desktop_users
= 0;
246 thread
->token
= NULL
;
248 thread
->desc_len
= 0;
250 thread
->creation_time
= current_time
;
251 thread
->exit_time
= 0;
253 list_init( &thread
->mutex_list
);
254 list_init( &thread
->system_apc
);
255 list_init( &thread
->user_apc
);
256 list_init( &thread
->kernel_object
);
258 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
259 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
262 /* check if address looks valid for a client-side data structure (TEB etc.) */
263 static inline int is_valid_address( client_ptr_t addr
)
265 return addr
&& !(addr
% sizeof(int));
269 /* dump a context on stdout for debugging purposes */
270 static void dump_context( struct object
*obj
, int verbose
)
272 struct context
*context
= (struct context
*)obj
;
273 assert( obj
->ops
== &context_ops
);
275 fprintf( stderr
, "context flags=%x/%x\n",
276 context
->regs
[CTX_NATIVE
].flags
, context
->regs
[CTX_WOW
].flags
);
280 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
282 struct context
*context
= (struct context
*)obj
;
283 return context
->status
!= STATUS_PENDING
;
287 static struct context
*create_thread_context( struct thread
*thread
)
289 struct context
*context
;
290 if (!(context
= alloc_object( &context_ops
))) return NULL
;
291 context
->status
= STATUS_PENDING
;
292 memset( &context
->regs
, 0, sizeof(context
->regs
) );
293 context
->regs
[CTX_NATIVE
].machine
= native_machine
;
298 /* create a new thread */
299 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
301 struct desktop
*desktop
;
302 struct thread
*thread
;
307 if (pipe( request_pipe
) == -1)
312 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
314 close( request_pipe
[0] );
315 close( request_pipe
[1] );
318 close( request_pipe
[1] );
319 fd
= request_pipe
[0];
322 if (process
->is_terminating
)
325 set_error( STATUS_PROCESS_IS_TERMINATING
);
329 if (!(thread
= alloc_object( &thread_ops
)))
335 init_thread_structure( thread
);
337 thread
->process
= (struct process
*)grab_object( process
);
339 thread
->affinity
= process
->affinity
;
340 if (!current
) current
= thread
;
342 list_add_tail( &thread_list
, &thread
->entry
);
344 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
345 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
346 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
350 release_object( thread
);
353 if (!(thread
->id
= alloc_ptid( thread
)))
356 release_object( thread
);
359 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
361 release_object( thread
);
365 if (process
->desktop
)
367 if (!(desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error(); /* ignore errors */
370 set_thread_default_desktop( thread
, desktop
, process
->desktop
);
371 release_object( desktop
);
375 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
376 add_process_thread( thread
->process
, thread
);
380 /* handle a client event */
381 static void thread_poll_event( struct fd
*fd
, int event
)
383 struct thread
*thread
= get_fd_user( fd
);
384 assert( thread
->obj
.ops
== &thread_ops
);
386 grab_object( thread
);
387 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
388 else if (event
& POLLIN
) read_request( thread
);
389 else if (event
& POLLOUT
) write_reply( thread
);
390 release_object( thread
);
393 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
395 struct thread
*thread
= (struct thread
*)obj
;
396 return &thread
->kernel_object
;
399 /* cleanup everything that is no longer needed by a dead thread */
400 /* used by destroy_thread and kill_thread */
401 static void cleanup_thread( struct thread
*thread
)
407 thread
->context
->status
= STATUS_ACCESS_DENIED
;
408 wake_up( &thread
->context
->obj
, 0 );
409 release_object( thread
->context
);
410 thread
->context
= NULL
;
412 clear_apc_queue( &thread
->system_apc
);
413 clear_apc_queue( &thread
->user_apc
);
414 free( thread
->req_data
);
415 free( thread
->reply_data
);
416 if (thread
->request_fd
) release_object( thread
->request_fd
);
417 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
418 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
419 cleanup_clipboard_thread(thread
);
420 destroy_thread_windows( thread
);
421 free_msg_queue( thread
);
422 release_thread_desktop( thread
, 1 );
423 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
425 if (thread
->inflight
[i
].client
!= -1)
427 close( thread
->inflight
[i
].server
);
428 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
431 free( thread
->desc
);
432 thread
->req_data
= NULL
;
433 thread
->reply_data
= NULL
;
434 thread
->request_fd
= NULL
;
435 thread
->reply_fd
= NULL
;
436 thread
->wait_fd
= NULL
;
439 thread
->desc_len
= 0;
442 /* destroy a thread when its refcount is 0 */
443 static void destroy_thread( struct object
*obj
)
445 struct thread
*thread
= (struct thread
*)obj
;
446 assert( obj
->ops
== &thread_ops
);
448 list_remove( &thread
->entry
);
449 cleanup_thread( thread
);
450 release_object( thread
->process
);
451 if (thread
->id
) free_ptid( thread
->id
);
452 if (thread
->token
) release_object( thread
->token
);
455 /* dump a thread on stdout for debugging purposes */
456 static void dump_thread( struct object
*obj
, int verbose
)
458 struct thread
*thread
= (struct thread
*)obj
;
459 assert( obj
->ops
== &thread_ops
);
461 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
462 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
465 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
467 struct thread
*mythread
= (struct thread
*)obj
;
468 return (mythread
->state
== TERMINATED
);
471 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
473 access
= default_map_access( obj
, access
);
474 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
475 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
479 static void dump_thread_apc( struct object
*obj
, int verbose
)
481 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
482 assert( obj
->ops
== &thread_apc_ops
);
484 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
487 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
489 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
490 return apc
->executed
;
493 static void thread_apc_destroy( struct object
*obj
)
495 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
497 if (apc
->caller
) release_object( apc
->caller
);
500 if (apc
->result
.type
== APC_ASYNC_IO
)
501 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
502 else if (apc
->call
.type
== APC_ASYNC_IO
)
503 async_set_result( apc
->owner
, apc
->call
.async_io
.status
, 0 );
504 release_object( apc
->owner
);
508 /* queue an async procedure call */
509 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
511 struct thread_apc
*apc
;
513 if ((apc
= alloc_object( &thread_apc_ops
)))
515 if (call_data
) apc
->call
= *call_data
;
516 else apc
->call
.type
= APC_NONE
;
520 apc
->result
.type
= APC_NONE
;
521 if (owner
) grab_object( owner
);
526 /* get a thread pointer from a thread id (and increment the refcount) */
527 struct thread
*get_thread_from_id( thread_id_t id
)
529 struct object
*obj
= get_ptid_entry( id
);
531 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
532 set_error( STATUS_INVALID_CID
);
536 /* get a thread from a handle (and increment the refcount) */
537 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
539 return (struct thread
*)get_handle_obj( current
->process
, handle
,
540 access
, &thread_ops
);
543 /* find a thread from a Unix tid */
544 struct thread
*get_thread_from_tid( int tid
)
546 struct thread
*thread
;
548 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
550 if (thread
->unix_tid
== tid
) return thread
;
555 /* find a thread from a Unix pid */
556 struct thread
*get_thread_from_pid( int pid
)
558 struct thread
*thread
;
560 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
562 if (thread
->unix_pid
== pid
) return thread
;
567 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
570 #ifdef HAVE_SCHED_SETAFFINITY
571 if (thread
->unix_tid
!= -1)
578 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
579 if (affinity
& mask
) CPU_SET( i
, &set
);
581 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
584 if (!ret
) thread
->affinity
= affinity
;
588 affinity_t
get_thread_affinity( struct thread
*thread
)
591 #ifdef HAVE_SCHED_SETAFFINITY
592 if (thread
->unix_tid
!= -1)
597 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
598 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
599 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
602 if (!mask
) mask
= ~(affinity_t
)0;
606 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
607 #define THREAD_PRIORITY_REALTIME_LOWEST -7
609 /* set all information about a thread */
610 static void set_thread_info( struct thread
*thread
,
611 const struct set_thread_info_request
*req
)
613 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
615 int max
= THREAD_PRIORITY_HIGHEST
;
616 int min
= THREAD_PRIORITY_LOWEST
;
617 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
619 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
620 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
622 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
623 req
->priority
== THREAD_PRIORITY_IDLE
||
624 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
625 thread
->priority
= req
->priority
;
627 set_error( STATUS_INVALID_PARAMETER
);
629 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
631 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
632 set_error( STATUS_INVALID_PARAMETER
);
633 else if (thread
->state
== TERMINATED
)
634 set_error( STATUS_THREAD_IS_TERMINATING
);
635 else if (set_thread_affinity( thread
, req
->affinity
))
638 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
639 security_set_thread_token( thread
, req
->token
);
640 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
641 thread
->entry_point
= req
->entry_point
;
642 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
643 thread
->dbg_hidden
= 1;
644 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
647 data_size_t desc_len
= get_req_data_size();
651 if ((desc
= mem_alloc( desc_len
)))
653 memcpy( desc
, get_req_data(), desc_len
);
654 free( thread
->desc
);
656 thread
->desc_len
= desc_len
;
661 free( thread
->desc
);
663 thread
->desc_len
= 0;
668 /* stop a thread (at the Unix level) */
669 void stop_thread( struct thread
*thread
)
671 if (thread
->context
) return; /* already suspended, no need for a signal */
672 if (!(thread
->context
= create_thread_context( thread
))) return;
673 /* can't stop a thread while initialisation is in progress */
674 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
677 /* suspend a thread */
678 int suspend_thread( struct thread
*thread
)
680 int old_count
= thread
->suspend
;
681 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
683 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
685 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
689 /* resume a thread */
690 int resume_thread( struct thread
*thread
)
692 int old_count
= thread
->suspend
;
693 if (thread
->suspend
> 0)
695 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
696 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
701 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
702 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
706 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
710 /* remove a thread from an object wait queue */
711 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
713 list_remove( &entry
->entry
);
714 release_object( obj
);
717 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
719 return entry
->wait
->thread
;
722 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
724 return entry
->wait
->select
;
727 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
729 return entry
->wait
->key
;
732 void make_wait_abandoned( struct wait_queue_entry
*entry
)
734 entry
->wait
->abandoned
= 1;
737 void set_wait_status( struct wait_queue_entry
*entry
, int status
)
739 entry
->wait
->status
= status
;
743 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
745 struct thread_wait
*wait
= thread
->wait
;
746 struct wait_queue_entry
*entry
;
750 thread
->wait
= wait
->next
;
752 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
754 wait
->status
= status
;
755 if (wait
->select
== SELECT_WAIT_ALL
)
757 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
758 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
762 entry
= wait
->queues
+ status
;
763 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
765 status
= wait
->status
;
766 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
768 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
769 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
770 if (wait
->user
) remove_timeout_user( wait
->user
);
775 /* build the thread wait structure */
776 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
777 int flags
, abstime_t when
)
779 struct thread_wait
*wait
;
780 struct wait_queue_entry
*entry
;
783 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
784 wait
->next
= current
->wait
;
785 wait
->thread
= current
;
788 wait
->select
= select_op
->op
;
793 current
->wait
= wait
;
795 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
797 struct object
*obj
= objects
[i
];
799 if (!obj
->ops
->add_queue( obj
, entry
))
802 end_wait( current
, get_error() );
809 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
810 int flags
, abstime_t when
)
812 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
816 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
818 for (i
= 0; i
< count
; i
++)
819 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
822 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
824 while (i
> 0) release_object( objects
[--i
] );
828 /* check if the thread waiting condition is satisfied */
829 static int check_wait( struct thread
*thread
)
832 struct thread_wait
*wait
= thread
->wait
;
833 struct wait_queue_entry
*entry
;
837 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
838 return STATUS_KERNEL_APC
;
840 /* Suspended threads may not acquire locks, but they can run system APCs */
841 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
843 if (wait
->select
== SELECT_WAIT_ALL
)
846 /* Note: we must check them all anyway, as some objects may
847 * want to do something when signaled, even if others are not */
848 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
849 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
850 if (!not_ok
) return STATUS_WAIT_0
;
854 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
855 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
858 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
859 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
860 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
864 /* send the wakeup signal to a thread */
865 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
867 struct wake_up_reply reply
;
870 /* check if we're waking current suspend wait */
871 if (thread
->context
&& thread
->suspend_cookie
== cookie
872 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
874 if (!thread
->context
->regs
[CTX_NATIVE
].flags
&& !thread
->context
->regs
[CTX_WOW
].flags
)
876 release_object( thread
->context
);
877 thread
->context
= NULL
;
879 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
882 memset( &reply
, 0, sizeof(reply
) );
883 reply
.cookie
= cookie
;
884 reply
.signaled
= signaled
;
885 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
888 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
889 else if (errno
== EPIPE
)
890 kill_thread( thread
, 0 ); /* normal death */
892 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
896 /* attempt to wake up a thread */
897 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
898 int wake_thread( struct thread
*thread
)
903 for (count
= 0; thread
->wait
; count
++)
905 if ((signaled
= check_wait( thread
)) == -1) break;
907 cookie
= thread
->wait
->cookie
;
908 signaled
= end_wait( thread
, signaled
);
909 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
910 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
912 if (!count
) count
= -1;
919 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
920 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
922 struct thread_wait
*wait
= entry
->wait
;
923 struct thread
*thread
= wait
->thread
;
927 if (thread
->wait
!= wait
) return 0; /* not the current wait */
928 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
930 assert( wait
->select
!= SELECT_WAIT_ALL
);
932 cookie
= wait
->cookie
;
933 signaled
= end_wait( thread
, entry
- wait
->queues
);
934 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
936 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
937 wake_thread( thread
); /* check other waits too */
942 /* thread wait timeout */
943 static void thread_timeout( void *ptr
)
945 struct thread_wait
*wait
= ptr
;
946 struct thread
*thread
= wait
->thread
;
947 client_ptr_t cookie
= wait
->cookie
;
950 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
951 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
953 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
954 end_wait( thread
, STATUS_TIMEOUT
);
957 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
958 /* check if other objects have become signaled in the meantime */
959 wake_thread( thread
);
962 /* try signaling an event flag, a semaphore or a mutex */
963 static int signal_object( obj_handle_t handle
)
968 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
971 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
972 release_object( obj
);
977 /* select on a list of handles */
978 static int select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
979 int flags
, abstime_t when
)
983 struct object
*object
;
985 switch (select_op
->op
)
988 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return 1;
992 case SELECT_WAIT_ALL
:
993 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
994 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
996 set_error( STATUS_INVALID_PARAMETER
);
999 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
1003 case SELECT_SIGNAL_AND_WAIT
:
1004 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
1006 if (select_op
->signal_and_wait
.signal
)
1008 if (!signal_object( select_op
->signal_and_wait
.signal
))
1010 end_wait( current
, get_error() );
1013 /* check if we woke ourselves up */
1014 if (!current
->wait
) return 1;
1018 case SELECT_KEYED_EVENT_WAIT
:
1019 case SELECT_KEYED_EVENT_RELEASE
:
1020 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1021 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1022 if (!object
) return 1;
1023 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1024 release_object( object
);
1026 current
->wait
->key
= select_op
->keyed_event
.key
;
1030 set_error( STATUS_INVALID_PARAMETER
);
1034 if ((ret
= check_wait( current
)) != -1)
1036 /* condition is already satisfied */
1037 set_error( end_wait( current
, ret
));
1041 /* now we need to wait */
1042 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1044 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1045 thread_timeout
, current
->wait
)))
1047 end_wait( current
, get_error() );
1051 current
->wait
->cookie
= cookie
;
1052 set_error( STATUS_PENDING
);
1056 /* attempt to wake threads sleeping on the object wait queue */
1057 void wake_up( struct object
*obj
, int max
)
1062 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1064 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1065 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1066 if (ret
> 0 && max
&& !--max
) break;
1067 /* restart at the head of the list since a wake up can change the object wait queue */
1068 ptr
= &obj
->wait_queue
;
1072 /* return the apc queue to use for a given apc type */
1073 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1080 return &thread
->user_apc
;
1082 return &thread
->system_apc
;
1086 /* check if thread is currently waiting for a (system) apc */
1087 static inline int is_in_apc_wait( struct thread
*thread
)
1089 return (thread
->process
->suspend
|| thread
->suspend
||
1090 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1093 /* queue an existing APC to a given thread */
1094 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1098 if (thread
&& thread
->state
== TERMINATED
&& process
)
1101 if (!thread
) /* find a suitable thread inside the process */
1103 struct thread
*candidate
;
1105 /* first try to find a waiting thread */
1106 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1108 if (candidate
->state
== TERMINATED
) continue;
1109 if (is_in_apc_wait( candidate
))
1117 /* then use the first one that accepts a signal */
1118 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1120 if (send_thread_signal( candidate
, SIGUSR1
))
1127 if (!thread
) return 0; /* nothing found */
1128 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1132 if (thread
->state
== TERMINATED
) return 0;
1133 if (!(queue
= get_apc_queue( thread
, apc
->call
.type
))) return 1;
1134 /* send signal for system APCs if needed */
1135 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1137 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1139 /* cancel a possible previous APC with the same owner */
1140 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1144 list_add_tail( queue
, &apc
->entry
);
1145 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1146 wake_thread( thread
);
1151 /* queue an async procedure call */
1152 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1154 struct thread_apc
*apc
;
1157 if ((apc
= create_apc( owner
, call_data
)))
1159 ret
= queue_apc( process
, thread
, apc
);
1160 release_object( apc
);
1165 /* cancel the async procedure call owned by a specific object */
1166 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1168 struct thread_apc
*apc
;
1169 struct list
*queue
= get_apc_queue( thread
, type
);
1171 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1173 if (apc
->owner
!= owner
) continue;
1174 list_remove( &apc
->entry
);
1176 wake_up( &apc
->obj
, 0 );
1177 release_object( apc
);
1182 /* remove the head apc from the queue; the returned object must be released by the caller */
1183 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1185 struct thread_apc
*apc
= NULL
;
1186 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1190 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1196 /* clear an APC queue, cancelling all the APCs on it */
1197 static void clear_apc_queue( struct list
*queue
)
1201 while ((ptr
= list_head( queue
)))
1203 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1204 list_remove( &apc
->entry
);
1206 wake_up( &apc
->obj
, 0 );
1207 release_object( apc
);
1211 /* add an fd to the inflight list */
1212 /* return list index, or -1 on error */
1213 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1217 if (server
== -1) return -1;
1224 /* first check if we already have an entry for this fd */
1225 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1226 if (thread
->inflight
[i
].client
== client
)
1228 close( thread
->inflight
[i
].server
);
1229 thread
->inflight
[i
].server
= server
;
1233 /* now find a free spot to store it */
1234 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1235 if (thread
->inflight
[i
].client
== -1)
1237 thread
->inflight
[i
].client
= client
;
1238 thread
->inflight
[i
].server
= server
;
1246 /* get an inflight fd and purge it from the list */
1247 /* the fd must be closed when no longer used */
1248 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1252 if (client
== -1) return -1;
1256 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1258 if (thread
->inflight
[i
].client
== client
)
1260 ret
= thread
->inflight
[i
].server
;
1261 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1265 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1269 /* kill a thread on the spot */
1270 void kill_thread( struct thread
*thread
, int violent_death
)
1272 if (thread
->state
== TERMINATED
) return; /* already killed */
1273 thread
->state
= TERMINATED
;
1274 thread
->exit_time
= current_time
;
1275 if (current
== thread
) current
= NULL
;
1277 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1278 thread
->id
, thread
->exit_code
);
1281 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1282 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1283 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1286 kill_console_processes( thread
, 0 );
1287 abandon_mutexes( thread
);
1288 wake_up( &thread
->obj
, 0 );
1289 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1290 cleanup_thread( thread
);
1291 remove_process_thread( thread
->process
, thread
);
1292 release_object( thread
);
1295 /* copy parts of a context structure */
1296 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1298 assert( to
->machine
== from
->machine
);
1299 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1300 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1301 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1302 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1303 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1304 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1305 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1308 /* gets the current impersonation token */
1309 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1312 return thread
->token
;
1314 return thread
->process
->token
;
1317 /* create a new thread */
1318 DECL_HANDLER(new_thread
)
1320 struct thread
*thread
;
1321 struct process
*process
;
1322 struct unicode_str name
;
1323 const struct security_descriptor
*sd
;
1324 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1325 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1327 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1329 if (request_fd
!= -1) close( request_fd
);
1333 if (process
!= current
->process
)
1335 if (request_fd
!= -1) /* can't create a request fd in a different process */
1337 close( request_fd
);
1338 set_error( STATUS_INVALID_PARAMETER
);
1341 if (process
->running_threads
) /* only the initial thread can be created in another process */
1343 set_error( STATUS_ACCESS_DENIED
);
1347 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1349 if (request_fd
!= -1) close( request_fd
);
1350 set_error( STATUS_INVALID_HANDLE
);
1354 if ((thread
= create_thread( request_fd
, process
, sd
)))
1356 thread
->system_regs
= current
->system_regs
;
1357 if (req
->flags
& THREAD_CREATE_FLAGS_CREATE_SUSPENDED
) thread
->suspend
++;
1358 thread
->dbg_hidden
= !!(req
->flags
& THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER
);
1359 reply
->tid
= get_thread_id( thread
);
1360 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1361 req
->access
, objattr
->attributes
)))
1363 /* thread object will be released when the thread gets killed */
1366 kill_thread( thread
, 1 );
1369 release_object( process
);
1372 static int init_thread( struct thread
*thread
, int reply_fd
, int wait_fd
)
1374 if ((reply_fd
= thread_get_inflight_fd( thread
, reply_fd
)) == -1)
1376 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1379 if ((wait_fd
= thread_get_inflight_fd( thread
, wait_fd
)) == -1)
1381 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1385 if (thread
->reply_fd
) /* already initialised */
1387 set_error( STATUS_INVALID_PARAMETER
);
1391 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1393 thread
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, &thread
->obj
, 0 );
1394 thread
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, &thread
->obj
, 0 );
1395 return thread
->reply_fd
&& thread
->wait_fd
;
1398 if (reply_fd
!= -1) close( reply_fd
);
1399 if (wait_fd
!= -1) close( wait_fd
);
1403 /* initialize the first thread of a new process */
1404 DECL_HANDLER(init_first_thread
)
1406 struct process
*process
= current
->process
;
1408 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1410 current
->unix_pid
= process
->unix_pid
= req
->unix_pid
;
1411 current
->unix_tid
= req
->unix_tid
;
1413 if (!process
->parent_id
)
1414 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1416 set_thread_affinity( current
, current
->affinity
);
1418 debug_level
= max( debug_level
, req
->debug_level
);
1420 reply
->pid
= get_process_id( process
);
1421 reply
->tid
= get_thread_id( current
);
1422 reply
->session_id
= process
->session_id
;
1423 reply
->info_size
= get_process_startup_info_size( process
);
1424 reply
->server_start
= server_start_time
;
1425 set_reply_data( supported_machines
,
1426 min( supported_machines_count
* sizeof(unsigned short), get_reply_max_size() ));
1429 /* initialize a new thread */
1430 DECL_HANDLER(init_thread
)
1432 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1434 if (!is_valid_address(req
->teb
))
1436 set_error( STATUS_INVALID_PARAMETER
);
1440 current
->unix_pid
= current
->process
->unix_pid
;
1441 current
->unix_tid
= req
->unix_tid
;
1442 current
->teb
= req
->teb
;
1443 current
->entry_point
= req
->entry
;
1445 init_thread_context( current
);
1446 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1447 set_thread_affinity( current
, current
->affinity
);
1449 reply
->suspend
= (current
->suspend
|| current
->process
->suspend
|| current
->context
!= NULL
);
1452 /* terminate a thread */
1453 DECL_HANDLER(terminate_thread
)
1455 struct thread
*thread
;
1457 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1459 thread
->exit_code
= req
->exit_code
;
1460 if (thread
!= current
) kill_thread( thread
, 1 );
1461 else reply
->self
= 1;
1462 cancel_terminating_thread_asyncs( thread
);
1463 release_object( thread
);
1467 /* open a handle to a thread */
1468 DECL_HANDLER(open_thread
)
1470 struct thread
*thread
= get_thread_from_id( req
->tid
);
1475 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1476 release_object( thread
);
1480 /* fetch information about a thread */
1481 DECL_HANDLER(get_thread_info
)
1483 struct thread
*thread
;
1484 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1486 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1487 thread
= get_thread_from_handle( req
->handle
, access
);
1490 reply
->pid
= get_process_id( thread
->process
);
1491 reply
->tid
= get_thread_id( thread
);
1492 reply
->teb
= thread
->teb
;
1493 reply
->entry_point
= thread
->entry_point
;
1494 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1495 reply
->priority
= thread
->priority
;
1496 reply
->affinity
= thread
->affinity
;
1497 reply
->last
= thread
->process
->running_threads
== 1;
1498 reply
->suspend_count
= thread
->suspend
;
1499 reply
->desc_len
= thread
->desc_len
;
1501 if (thread
->dbg_hidden
)
1502 reply
->flags
|= GET_THREAD_INFO_FLAG_DBG_HIDDEN
;
1503 if (thread
->state
== TERMINATED
)
1504 reply
->flags
|= GET_THREAD_INFO_FLAG_TERMINATED
;
1506 if (thread
->desc
&& get_reply_max_size())
1508 if (thread
->desc_len
<= get_reply_max_size())
1509 set_reply_data( thread
->desc
, thread
->desc_len
);
1511 set_error( STATUS_BUFFER_TOO_SMALL
);
1514 release_object( thread
);
1518 /* fetch information about thread times */
1519 DECL_HANDLER(get_thread_times
)
1521 struct thread
*thread
;
1523 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1525 reply
->creation_time
= thread
->creation_time
;
1526 reply
->exit_time
= thread
->exit_time
;
1527 reply
->unix_pid
= thread
->unix_pid
;
1528 reply
->unix_tid
= thread
->unix_tid
;
1530 release_object( thread
);
1534 /* set information about a thread */
1535 DECL_HANDLER(set_thread_info
)
1537 struct thread
*thread
;
1538 unsigned int access
= (req
->mask
== SET_THREAD_INFO_DESCRIPTION
) ? THREAD_SET_LIMITED_INFORMATION
1539 : THREAD_SET_INFORMATION
;
1541 if ((thread
= get_thread_from_handle( req
->handle
, access
)))
1543 set_thread_info( thread
, req
);
1544 release_object( thread
);
1548 /* suspend a thread */
1549 DECL_HANDLER(suspend_thread
)
1551 struct thread
*thread
;
1553 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1555 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1556 else reply
->count
= suspend_thread( thread
);
1557 release_object( thread
);
1561 /* resume a thread */
1562 DECL_HANDLER(resume_thread
)
1564 struct thread
*thread
;
1566 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1568 reply
->count
= resume_thread( thread
);
1569 release_object( thread
);
1573 /* select on a handle list */
1574 DECL_HANDLER(select
)
1576 select_op_t select_op
;
1577 data_size_t op_size
, ctx_size
;
1578 struct context
*ctx
;
1579 struct thread_apc
*apc
;
1580 const apc_result_t
*result
= get_req_data();
1581 unsigned int ctx_count
;
1583 if (get_req_data_size() < sizeof(*result
)) goto invalid_param
;
1584 if (get_req_data_size() - sizeof(*result
) < req
->size
) goto invalid_param
;
1585 if (req
->size
& 3) goto invalid_param
;
1586 ctx_size
= get_req_data_size() - sizeof(*result
) - req
->size
;
1587 ctx_count
= ctx_size
/ sizeof(context_t
);
1588 if (ctx_count
* sizeof(context_t
) != ctx_size
) goto invalid_param
;
1589 if (ctx_count
> 1 + (current
->process
->machine
!= native_machine
)) goto invalid_param
;
1593 const context_t
*native_context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1594 const context_t
*wow_context
= (ctx_count
> 1) ? native_context
+ 1 : NULL
;
1596 if (native_context
->machine
== native_machine
)
1598 if (wow_context
&& wow_context
->machine
!= current
->process
->machine
) goto invalid_param
;
1600 else if (native_context
->machine
== current
->process
->machine
)
1602 if (wow_context
) goto invalid_param
;
1603 wow_context
= native_context
;
1604 native_context
= NULL
;
1606 else goto invalid_param
;
1608 if ((ctx
= current
->context
))
1610 if (ctx
->status
!= STATUS_PENDING
) goto invalid_param
;
1611 /* if context was modified in pending state, discard irrelevant changes */
1612 if (wow_context
) ctx
->regs
[CTX_NATIVE
].flags
&= ~ctx
->regs
[CTX_WOW
].flags
;
1613 else ctx
->regs
[CTX_WOW
].flags
= ctx
->regs
[CTX_WOW
].machine
= 0;
1615 else if (!(current
->context
= create_thread_context( current
))) return;
1617 ctx
= current
->context
;
1620 copy_context( &ctx
->regs
[CTX_NATIVE
], native_context
,
1621 native_context
->flags
& ~(ctx
->regs
[CTX_NATIVE
].flags
| system_flags
) );
1625 ctx
->regs
[CTX_WOW
].machine
= current
->process
->machine
;
1626 copy_context( &ctx
->regs
[CTX_WOW
], wow_context
, wow_context
->flags
& ~ctx
->regs
[CTX_WOW
].flags
);
1628 ctx
->status
= STATUS_SUCCESS
;
1629 current
->suspend_cookie
= req
->cookie
;
1630 wake_up( &ctx
->obj
, 0 );
1633 if (!req
->cookie
) goto invalid_param
;
1635 op_size
= min( req
->size
, sizeof(select_op
) );
1636 memset( &select_op
, 0, sizeof(select_op
) );
1637 memcpy( &select_op
, result
+ 1, op_size
);
1639 /* first store results of previous apc */
1642 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1643 0, &thread_apc_ops
))) return;
1644 apc
->result
= *result
;
1646 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1648 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1649 apc
->caller
->process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1650 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1651 apc
->result
.create_thread
.handle
= handle
;
1652 clear_error(); /* ignore errors from the above calls */
1654 wake_up( &apc
->obj
, 0 );
1655 close_handle( current
->process
, req
->prev_apc
);
1656 release_object( apc
);
1659 reply
->signaled
= select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1661 if (get_error() == STATUS_USER_APC
&& get_reply_max_size() >= sizeof(apc_call_t
))
1663 apc
= thread_dequeue_apc( current
, 0 );
1664 set_reply_data( &apc
->call
, sizeof(apc
->call
) );
1665 release_object( apc
);
1667 else if (get_error() == STATUS_KERNEL_APC
&& get_reply_max_size() >= sizeof(apc_call_t
))
1669 apc
= thread_dequeue_apc( current
, 1 );
1670 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1672 set_reply_data( &apc
->call
, sizeof(apc
->call
) );
1677 wake_up( &apc
->obj
, 0 );
1679 release_object( apc
);
1681 else if (reply
->signaled
&& get_reply_max_size() >= sizeof(apc_call_t
) + sizeof(context_t
) &&
1682 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1684 ctx
= current
->context
;
1685 if (ctx
->regs
[CTX_NATIVE
].flags
|| ctx
->regs
[CTX_WOW
].flags
)
1688 data_size_t size
= sizeof(*data
) + (ctx
->regs
[CTX_WOW
].flags
? 2 : 1) * sizeof(context_t
);
1689 unsigned int flags
= system_flags
& ctx
->regs
[CTX_NATIVE
].flags
;
1691 if (flags
) set_thread_context( current
, &ctx
->regs
[CTX_NATIVE
], flags
);
1692 size
= min( size
, get_reply_max_size() );
1693 if ((data
= set_reply_data_size( size
)))
1695 memset( data
, 0, sizeof(*data
) );
1696 memcpy( data
+ 1, ctx
->regs
, size
- sizeof(*data
) );
1699 release_object( ctx
);
1700 current
->context
= NULL
;
1705 set_error( STATUS_INVALID_PARAMETER
);
1708 /* queue an APC for a thread or process */
1709 DECL_HANDLER(queue_apc
)
1711 struct thread
*thread
= NULL
;
1712 struct process
*process
= NULL
;
1713 struct thread_apc
*apc
;
1714 const apc_call_t
*call
= get_req_data();
1716 if (get_req_data_size() < sizeof(*call
)) call
= NULL
;
1718 if (!(apc
= create_apc( NULL
, call
))) return;
1720 switch (apc
->call
.type
)
1724 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1726 case APC_VIRTUAL_ALLOC
:
1727 case APC_VIRTUAL_ALLOC_EX
:
1728 case APC_VIRTUAL_FREE
:
1729 case APC_VIRTUAL_PROTECT
:
1730 case APC_VIRTUAL_FLUSH
:
1731 case APC_VIRTUAL_LOCK
:
1732 case APC_VIRTUAL_UNLOCK
:
1733 case APC_UNMAP_VIEW
:
1734 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1736 case APC_VIRTUAL_QUERY
:
1737 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1740 case APC_MAP_VIEW_EX
:
1741 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1742 if (process
&& process
!= current
->process
)
1744 /* duplicate the handle into the target process */
1745 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1746 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1747 if (handle
) apc
->call
.map_view
.handle
= handle
;
1750 release_object( process
);
1755 case APC_CREATE_THREAD
:
1756 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1758 case APC_DUP_HANDLE
:
1759 process
= get_process_from_handle( req
->handle
, PROCESS_DUP_HANDLE
);
1760 if (process
&& process
!= current
->process
)
1762 /* duplicate the destination process handle into the target process */
1763 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.dup_handle
.dst_process
,
1764 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1765 if (handle
) apc
->call
.dup_handle
.dst_process
= handle
;
1768 release_object( process
);
1774 set_error( STATUS_INVALID_PARAMETER
);
1780 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_UNSUCCESSFUL
);
1781 release_object( thread
);
1785 reply
->self
= (process
== current
->process
);
1788 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1791 if (queue_apc( process
, NULL
, apc
))
1793 apc
->caller
= (struct thread
*)grab_object( current
);
1794 reply
->handle
= handle
;
1798 close_handle( current
->process
, handle
);
1799 set_error( STATUS_PROCESS_IS_TERMINATING
);
1803 release_object( process
);
1806 release_object( apc
);
1809 /* Get the result of an APC call */
1810 DECL_HANDLER(get_apc_result
)
1812 struct thread_apc
*apc
;
1814 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1815 0, &thread_apc_ops
))) return;
1817 if (apc
->executed
) reply
->result
= apc
->result
;
1818 else set_error( STATUS_PENDING
);
1820 /* close the handle directly to avoid an extra round-trip */
1821 close_handle( current
->process
, req
->handle
);
1822 release_object( apc
);
1825 /* retrieve the current context of a thread */
1826 DECL_HANDLER(get_thread_context
)
1828 struct context
*thread_context
= NULL
;
1829 struct thread
*thread
;
1832 if (get_reply_max_size() < 2 * sizeof(context_t
))
1834 set_error( STATUS_INVALID_PARAMETER
);
1840 if (!(thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->context
,
1843 close_handle( current
->process
, req
->context
); /* avoid extra server call */
1847 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1848 if (req
->machine
!= native_machine
&& req
->machine
!= thread
->process
->machine
)
1849 set_error( STATUS_INVALID_PARAMETER
);
1850 else if (thread
->state
!= RUNNING
)
1851 set_error( STATUS_UNSUCCESSFUL
);
1854 reply
->self
= (thread
== current
);
1855 if (thread
!= current
) stop_thread( thread
);
1856 if (thread
->context
)
1858 /* make sure that system regs are valid in thread context */
1859 if (thread
->unix_tid
!= -1 && (system_flags
& ~thread
->context
->regs
[CTX_NATIVE
].flags
))
1860 get_thread_context( thread
, &thread
->context
->regs
[CTX_NATIVE
], system_flags
);
1861 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1863 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1865 assert( reply
->self
);
1866 memset( context
, 0, sizeof(context_t
) );
1867 context
->machine
= native_machine
;
1868 if (system_flags
) get_thread_context( thread
, context
, system_flags
);
1871 release_object( thread
);
1872 if (!thread_context
) return;
1875 if (!thread_context
->status
)
1877 unsigned int native_flags
= req
->flags
, wow_flags
= 0;
1879 if (req
->machine
== thread_context
->regs
[CTX_WOW
].machine
)
1881 native_flags
= req
->native_flags
;
1882 wow_flags
= req
->flags
& ~native_flags
;
1884 if ((context
= set_reply_data_size( (!!native_flags
+ !!wow_flags
) * sizeof(context_t
) )))
1888 memset( context
, 0, sizeof(*context
) );
1889 context
->machine
= thread_context
->regs
[CTX_NATIVE
].machine
;
1890 copy_context( context
, &thread_context
->regs
[CTX_NATIVE
], native_flags
);
1891 context
->flags
= native_flags
;
1896 memset( context
, 0, sizeof(*context
) );
1897 context
->machine
= thread_context
->regs
[CTX_WOW
].machine
;
1898 copy_context( context
, &thread_context
->regs
[CTX_WOW
], wow_flags
);
1899 context
->flags
= wow_flags
;
1905 set_error( thread_context
->status
);
1906 if (thread_context
->status
== STATUS_PENDING
)
1907 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1910 release_object( thread_context
);
1913 /* set the current context of a thread */
1914 DECL_HANDLER(set_thread_context
)
1916 struct thread
*thread
;
1917 const context_t
*contexts
= get_req_data();
1918 unsigned int ctx_count
= get_req_data_size() / sizeof(context_t
);
1920 if (!ctx_count
|| ctx_count
> 2 || ctx_count
* sizeof(context_t
) != get_req_data_size())
1922 set_error( STATUS_INVALID_PARAMETER
);
1926 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1927 reply
->self
= (thread
== current
);
1929 if (contexts
[CTX_NATIVE
].machine
!= native_machine
||
1930 (ctx_count
== 2 && contexts
[CTX_WOW
].machine
!= thread
->process
->machine
))
1931 set_error( STATUS_INVALID_PARAMETER
);
1932 else if (thread
->state
!= TERMINATED
)
1934 unsigned int flags
= system_flags
& contexts
[CTX_NATIVE
].flags
;
1936 if (thread
!= current
) stop_thread( thread
);
1937 else if (flags
) set_thread_context( thread
, &contexts
[CTX_NATIVE
], flags
);
1939 if (thread
->context
&& !get_error())
1941 /* If context is in a pending state, we don't know if we will use WoW or native
1942 * context, so store both and discard irrevelant one in select request. */
1943 const int is_pending
= thread
->context
->status
== STATUS_PENDING
;
1944 unsigned int native_flags
= contexts
[CTX_NATIVE
].flags
;
1946 if (ctx_count
== 2 && (is_pending
|| thread
->context
->regs
[CTX_WOW
].machine
))
1948 context_t
*ctx
= &thread
->context
->regs
[CTX_WOW
];
1950 /* some regs are always set from the native context */
1951 flags
= contexts
[CTX_WOW
].flags
& ~req
->native_flags
;
1952 if (is_pending
) ctx
->machine
= contexts
[CTX_WOW
].machine
;
1953 else native_flags
&= req
->native_flags
;
1955 copy_context( ctx
, &contexts
[CTX_WOW
], flags
);
1956 ctx
->flags
|= flags
;
1961 context_t
*ctx
= &thread
->context
->regs
[CTX_NATIVE
];
1962 copy_context( ctx
, &contexts
[CTX_NATIVE
], native_flags
);
1963 ctx
->flags
|= native_flags
;
1967 else set_error( STATUS_UNSUCCESSFUL
);
1969 release_object( thread
);
1972 /* fetch a selector entry for a thread */
1973 DECL_HANDLER(get_selector_entry
)
1975 struct thread
*thread
;
1976 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1978 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1979 release_object( thread
);
1983 /* Iterate thread list for process. Use global thread list to also
1984 * return terminated but not yet destroyed threads. */
1985 DECL_HANDLER(get_next_thread
)
1987 struct thread
*thread
;
1988 struct process
*process
;
1993 set_error( STATUS_INVALID_PARAMETER
);
1997 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
2002 ptr
= req
->flags
? list_tail( &thread_list
) : list_head( &thread_list
);
2004 else if ((thread
= get_thread_from_handle( req
->last
, 0 )))
2006 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
2007 : list_next( &thread_list
, &thread
->entry
);
2008 release_object( thread
);
2012 release_object( process
);
2018 thread
= LIST_ENTRY( ptr
, struct thread
, entry
);
2019 if (thread
->process
== process
)
2021 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
2022 release_object( process
);
2025 ptr
= req
->flags
? list_prev( &thread_list
, &thread
->entry
)
2026 : list_next( &thread_list
, &thread
->entry
);
2028 set_error( STATUS_NO_MORE_ENTRIES
);
2029 release_object( process
);