2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #include <sys/types.h>
43 #define WIN32_NO_STATUS
57 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86
);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus
= CPU_FLAG(CPU_x86_64
) | CPU_FLAG(CPU_x86
);
60 #elif defined(__powerpc__)
61 static const unsigned int supported_cpus
= CPU_FLAG(CPU_POWERPC
);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ARM
);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus
= CPU_FLAG(CPU_ARM64
) | CPU_FLAG(CPU_ARM
);
67 #error Unsupported CPU
74 struct thread_wait
*next
; /* next wait structure for this thread */
75 struct thread
*thread
; /* owner thread */
76 int count
; /* count of objects */
79 enum select_op select
;
80 client_ptr_t key
; /* wait key for keyed events */
81 client_ptr_t cookie
; /* magic cookie to return to client */
83 struct timeout_user
*user
;
84 struct wait_queue_entry queues
[1];
87 /* asynchronous procedure calls */
91 struct object obj
; /* object header */
92 struct list entry
; /* queue linked list */
93 struct thread
*caller
; /* thread that queued this apc */
94 struct object
*owner
; /* object that queued this apc */
95 int executed
; /* has it been executed by the client? */
96 apc_call_t call
; /* call arguments */
97 apc_result_t result
; /* call results once executed */
100 static void dump_thread_apc( struct object
*obj
, int verbose
);
101 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
102 static void thread_apc_destroy( struct object
*obj
);
103 static void clear_apc_queue( struct list
*queue
);
105 static const struct object_ops thread_apc_ops
=
107 sizeof(struct thread_apc
), /* size */
108 dump_thread_apc
, /* dump */
109 no_get_type
, /* get_type */
110 add_queue
, /* add_queue */
111 remove_queue
, /* remove_queue */
112 thread_apc_signaled
, /* signaled */
113 no_satisfied
, /* satisfied */
114 no_signal
, /* signal */
115 no_get_fd
, /* get_fd */
116 no_map_access
, /* map_access */
117 default_get_sd
, /* get_sd */
118 default_set_sd
, /* set_sd */
119 no_get_full_name
, /* get_full_name */
120 no_lookup_name
, /* lookup_name */
121 no_link_name
, /* link_name */
122 NULL
, /* unlink_name */
123 no_open_file
, /* open_file */
124 no_kernel_obj_list
, /* get_kernel_obj_list */
125 no_close_handle
, /* close_handle */
126 thread_apc_destroy
/* destroy */
130 /* thread CPU context */
134 struct object obj
; /* object header */
135 unsigned int status
; /* status of the context */
136 context_t regs
; /* context data */
139 static void dump_context( struct object
*obj
, int verbose
);
140 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
142 static const struct object_ops context_ops
=
144 sizeof(struct context
), /* size */
145 dump_context
, /* dump */
146 no_get_type
, /* get_type */
147 add_queue
, /* add_queue */
148 remove_queue
, /* remove_queue */
149 context_signaled
, /* signaled */
150 no_satisfied
, /* satisfied */
151 no_signal
, /* signal */
152 no_get_fd
, /* get_fd */
153 no_map_access
, /* map_access */
154 default_get_sd
, /* get_sd */
155 default_set_sd
, /* set_sd */
156 no_get_full_name
, /* get_full_name */
157 no_lookup_name
, /* lookup_name */
158 no_link_name
, /* link_name */
159 NULL
, /* unlink_name */
160 no_open_file
, /* open_file */
161 no_kernel_obj_list
, /* get_kernel_obj_list */
162 no_close_handle
, /* close_handle */
163 no_destroy
/* destroy */
167 /* thread operations */
169 static void dump_thread( struct object
*obj
, int verbose
);
170 static struct object_type
*thread_get_type( struct object
*obj
);
171 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
172 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
173 static void thread_poll_event( struct fd
*fd
, int event
);
174 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
175 static void destroy_thread( struct object
*obj
);
177 static const struct object_ops thread_ops
=
179 sizeof(struct thread
), /* size */
180 dump_thread
, /* dump */
181 thread_get_type
, /* get_type */
182 add_queue
, /* add_queue */
183 remove_queue
, /* remove_queue */
184 thread_signaled
, /* signaled */
185 no_satisfied
, /* satisfied */
186 no_signal
, /* signal */
187 no_get_fd
, /* get_fd */
188 thread_map_access
, /* map_access */
189 default_get_sd
, /* get_sd */
190 default_set_sd
, /* set_sd */
191 no_get_full_name
, /* get_full_name */
192 no_lookup_name
, /* lookup_name */
193 no_link_name
, /* link_name */
194 NULL
, /* unlink_name */
195 no_open_file
, /* open_file */
196 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
197 no_close_handle
, /* close_handle */
198 destroy_thread
/* destroy */
201 static const struct fd_ops thread_fd_ops
=
203 NULL
, /* get_poll_events */
204 thread_poll_event
, /* poll_event */
206 NULL
, /* get_fd_type */
208 NULL
, /* queue_async */
209 NULL
/* reselect_async */
212 static struct list thread_list
= LIST_INIT(thread_list
);
214 /* initialize the structure for a newly allocated thread */
215 static inline void init_thread_structure( struct thread
*thread
)
219 thread
->unix_pid
= -1; /* not known yet */
220 thread
->unix_tid
= -1; /* not known yet */
221 thread
->context
= NULL
;
223 thread
->entry_point
= 0;
224 thread
->system_regs
= 0;
225 thread
->queue
= NULL
;
228 thread
->req_data
= NULL
;
229 thread
->req_toread
= 0;
230 thread
->reply_data
= NULL
;
231 thread
->reply_towrite
= 0;
232 thread
->request_fd
= NULL
;
233 thread
->reply_fd
= NULL
;
234 thread
->wait_fd
= NULL
;
235 thread
->state
= RUNNING
;
236 thread
->exit_code
= 0;
237 thread
->priority
= 0;
239 thread
->dbg_hidden
= 0;
240 thread
->desktop_users
= 0;
241 thread
->token
= NULL
;
243 thread
->desc_len
= 0;
245 thread
->creation_time
= current_time
;
246 thread
->exit_time
= 0;
248 list_init( &thread
->mutex_list
);
249 list_init( &thread
->system_apc
);
250 list_init( &thread
->user_apc
);
251 list_init( &thread
->kernel_object
);
253 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
254 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
257 /* check if address looks valid for a client-side data structure (TEB etc.) */
258 static inline int is_valid_address( client_ptr_t addr
)
260 return addr
&& !(addr
% sizeof(int));
264 /* dump a context on stdout for debugging purposes */
265 static void dump_context( struct object
*obj
, int verbose
)
267 struct context
*context
= (struct context
*)obj
;
268 assert( obj
->ops
== &context_ops
);
270 fprintf( stderr
, "context flags=%x\n", context
->regs
.flags
);
274 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
276 struct context
*context
= (struct context
*)obj
;
277 return context
->status
!= STATUS_PENDING
;
281 static struct context
*create_thread_context( struct thread
*thread
)
283 struct context
*context
;
284 if (!(context
= alloc_object( &context_ops
))) return NULL
;
285 context
->status
= STATUS_PENDING
;
286 memset( &context
->regs
, 0, sizeof(context
->regs
) );
287 context
->regs
.cpu
= thread
->process
->cpu
;
292 /* create a new thread */
293 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
295 struct thread
*thread
;
300 if (pipe( request_pipe
) == -1)
305 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
307 close( request_pipe
[0] );
308 close( request_pipe
[1] );
311 close( request_pipe
[1] );
312 fd
= request_pipe
[0];
315 if (process
->is_terminating
)
318 set_error( STATUS_PROCESS_IS_TERMINATING
);
322 if (!(thread
= alloc_object( &thread_ops
)))
328 init_thread_structure( thread
);
330 thread
->process
= (struct process
*)grab_object( process
);
331 thread
->desktop
= process
->desktop
;
332 thread
->affinity
= process
->affinity
;
333 if (!current
) current
= thread
;
335 list_add_tail( &thread_list
, &thread
->entry
);
337 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
338 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
339 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
343 release_object( thread
);
346 if (!(thread
->id
= alloc_ptid( thread
)))
349 release_object( thread
);
352 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
354 release_object( thread
);
358 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
359 add_process_thread( thread
->process
, thread
);
363 /* handle a client event */
364 static void thread_poll_event( struct fd
*fd
, int event
)
366 struct thread
*thread
= get_fd_user( fd
);
367 assert( thread
->obj
.ops
== &thread_ops
);
369 grab_object( thread
);
370 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
371 else if (event
& POLLIN
) read_request( thread
);
372 else if (event
& POLLOUT
) write_reply( thread
);
373 release_object( thread
);
376 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
378 struct thread
*thread
= (struct thread
*)obj
;
379 return &thread
->kernel_object
;
382 /* cleanup everything that is no longer needed by a dead thread */
383 /* used by destroy_thread and kill_thread */
384 static void cleanup_thread( struct thread
*thread
)
390 thread
->context
->status
= STATUS_ACCESS_DENIED
;
391 wake_up( &thread
->context
->obj
, 0 );
392 release_object( thread
->context
);
393 thread
->context
= NULL
;
395 clear_apc_queue( &thread
->system_apc
);
396 clear_apc_queue( &thread
->user_apc
);
397 free( thread
->req_data
);
398 free( thread
->reply_data
);
399 if (thread
->request_fd
) release_object( thread
->request_fd
);
400 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
401 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
402 cleanup_clipboard_thread(thread
);
403 destroy_thread_windows( thread
);
404 free_msg_queue( thread
);
405 close_thread_desktop( thread
);
406 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
408 if (thread
->inflight
[i
].client
!= -1)
410 close( thread
->inflight
[i
].server
);
411 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
414 free( thread
->desc
);
415 thread
->req_data
= NULL
;
416 thread
->reply_data
= NULL
;
417 thread
->request_fd
= NULL
;
418 thread
->reply_fd
= NULL
;
419 thread
->wait_fd
= NULL
;
422 thread
->desc_len
= 0;
425 /* destroy a thread when its refcount is 0 */
426 static void destroy_thread( struct object
*obj
)
428 struct thread
*thread
= (struct thread
*)obj
;
429 assert( obj
->ops
== &thread_ops
);
431 list_remove( &thread
->entry
);
432 cleanup_thread( thread
);
433 release_object( thread
->process
);
434 if (thread
->id
) free_ptid( thread
->id
);
435 if (thread
->token
) release_object( thread
->token
);
438 /* dump a thread on stdout for debugging purposes */
439 static void dump_thread( struct object
*obj
, int verbose
)
441 struct thread
*thread
= (struct thread
*)obj
;
442 assert( obj
->ops
== &thread_ops
);
444 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
445 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
448 static struct object_type
*thread_get_type( struct object
*obj
)
450 static const WCHAR name
[] = {'T','h','r','e','a','d'};
451 static const struct unicode_str str
= { name
, sizeof(name
) };
452 return get_object_type( &str
);
455 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
457 struct thread
*mythread
= (struct thread
*)obj
;
458 return (mythread
->state
== TERMINATED
);
461 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
463 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
;
464 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
|
465 THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
;
466 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_QUERY_LIMITED_INFORMATION
;
467 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
469 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
470 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
472 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
475 static void dump_thread_apc( struct object
*obj
, int verbose
)
477 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
478 assert( obj
->ops
== &thread_apc_ops
);
480 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
483 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
485 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
486 return apc
->executed
;
489 static void thread_apc_destroy( struct object
*obj
)
491 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
492 if (apc
->caller
) release_object( apc
->caller
);
493 if (apc
->owner
) release_object( apc
->owner
);
496 /* queue an async procedure call */
497 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
499 struct thread_apc
*apc
;
501 if ((apc
= alloc_object( &thread_apc_ops
)))
503 apc
->call
= *call_data
;
507 apc
->result
.type
= APC_NONE
;
508 if (owner
) grab_object( owner
);
513 /* get a thread pointer from a thread id (and increment the refcount) */
514 struct thread
*get_thread_from_id( thread_id_t id
)
516 struct object
*obj
= get_ptid_entry( id
);
518 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
519 set_error( STATUS_INVALID_CID
);
523 /* get a thread from a handle (and increment the refcount) */
524 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
526 return (struct thread
*)get_handle_obj( current
->process
, handle
,
527 access
, &thread_ops
);
530 /* find a thread from a Unix tid */
531 struct thread
*get_thread_from_tid( int tid
)
533 struct thread
*thread
;
535 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
537 if (thread
->unix_tid
== tid
) return thread
;
542 /* find a thread from a Unix pid */
543 struct thread
*get_thread_from_pid( int pid
)
545 struct thread
*thread
;
547 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
549 if (thread
->unix_pid
== pid
) return thread
;
554 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
557 #ifdef HAVE_SCHED_SETAFFINITY
558 if (thread
->unix_tid
!= -1)
565 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
566 if (affinity
& mask
) CPU_SET( i
, &set
);
568 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
571 if (!ret
) thread
->affinity
= affinity
;
575 affinity_t
get_thread_affinity( struct thread
*thread
)
578 #ifdef HAVE_SCHED_SETAFFINITY
579 if (thread
->unix_tid
!= -1)
584 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
585 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
586 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
589 if (!mask
) mask
= ~(affinity_t
)0;
593 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
594 #define THREAD_PRIORITY_REALTIME_LOWEST -7
596 /* set all information about a thread */
597 static void set_thread_info( struct thread
*thread
,
598 const struct set_thread_info_request
*req
)
600 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
602 int max
= THREAD_PRIORITY_HIGHEST
;
603 int min
= THREAD_PRIORITY_LOWEST
;
604 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
606 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
607 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
609 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
610 req
->priority
== THREAD_PRIORITY_IDLE
||
611 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
612 thread
->priority
= req
->priority
;
614 set_error( STATUS_INVALID_PARAMETER
);
616 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
618 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
619 set_error( STATUS_INVALID_PARAMETER
);
620 else if (thread
->state
== TERMINATED
)
621 set_error( STATUS_THREAD_IS_TERMINATING
);
622 else if (set_thread_affinity( thread
, req
->affinity
))
625 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
626 security_set_thread_token( thread
, req
->token
);
627 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
628 thread
->entry_point
= req
->entry_point
;
629 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
630 thread
->dbg_hidden
= 1;
631 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
634 data_size_t desc_len
= get_req_data_size();
638 if ((desc
= mem_alloc( desc_len
)))
640 memcpy( desc
, get_req_data(), desc_len
);
641 free( thread
->desc
);
643 thread
->desc_len
= desc_len
;
648 free( thread
->desc
);
650 thread
->desc_len
= 0;
655 /* stop a thread (at the Unix level) */
656 void stop_thread( struct thread
*thread
)
658 if (thread
->context
) return; /* already suspended, no need for a signal */
659 if (!(thread
->context
= create_thread_context( thread
))) return;
660 /* can't stop a thread while initialisation is in progress */
661 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
664 /* suspend a thread */
665 int suspend_thread( struct thread
*thread
)
667 int old_count
= thread
->suspend
;
668 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
670 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
672 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
676 /* resume a thread */
677 int resume_thread( struct thread
*thread
)
679 int old_count
= thread
->suspend
;
680 if (thread
->suspend
> 0)
682 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
683 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
688 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
689 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
693 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
697 /* remove a thread from an object wait queue */
698 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
700 list_remove( &entry
->entry
);
701 release_object( obj
);
704 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
706 return entry
->wait
->thread
;
709 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
711 return entry
->wait
->select
;
714 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
716 return entry
->wait
->key
;
719 void make_wait_abandoned( struct wait_queue_entry
*entry
)
721 entry
->wait
->abandoned
= 1;
725 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
727 struct thread_wait
*wait
= thread
->wait
;
728 struct wait_queue_entry
*entry
;
732 thread
->wait
= wait
->next
;
734 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
736 if (wait
->select
== SELECT_WAIT_ALL
)
738 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
739 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
743 entry
= wait
->queues
+ status
;
744 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
746 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
748 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
749 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
750 if (wait
->user
) remove_timeout_user( wait
->user
);
755 /* build the thread wait structure */
756 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
757 int flags
, abstime_t when
)
759 struct thread_wait
*wait
;
760 struct wait_queue_entry
*entry
;
763 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
764 wait
->next
= current
->wait
;
765 wait
->thread
= current
;
768 wait
->select
= select_op
->op
;
773 current
->wait
= wait
;
775 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
777 struct object
*obj
= objects
[i
];
779 if (!obj
->ops
->add_queue( obj
, entry
))
782 end_wait( current
, get_error() );
789 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
790 int flags
, abstime_t when
)
792 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
796 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
798 for (i
= 0; i
< count
; i
++)
799 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
802 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
804 while (i
> 0) release_object( objects
[--i
] );
808 /* check if the thread waiting condition is satisfied */
809 static int check_wait( struct thread
*thread
)
812 struct thread_wait
*wait
= thread
->wait
;
813 struct wait_queue_entry
*entry
;
817 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
818 return STATUS_KERNEL_APC
;
820 /* Suspended threads may not acquire locks, but they can run system APCs */
821 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
823 if (wait
->select
== SELECT_WAIT_ALL
)
826 /* Note: we must check them all anyway, as some objects may
827 * want to do something when signaled, even if others are not */
828 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
829 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
830 if (!not_ok
) return STATUS_WAIT_0
;
834 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
835 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
838 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
839 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
840 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
844 /* send the wakeup signal to a thread */
845 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
847 struct wake_up_reply reply
;
850 /* check if we're waking current suspend wait */
851 if (thread
->context
&& thread
->suspend_cookie
== cookie
852 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
854 if (!thread
->context
->regs
.flags
)
856 release_object( thread
->context
);
857 thread
->context
= NULL
;
859 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
862 memset( &reply
, 0, sizeof(reply
) );
863 reply
.cookie
= cookie
;
864 reply
.signaled
= signaled
;
865 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
868 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
869 else if (errno
== EPIPE
)
870 kill_thread( thread
, 0 ); /* normal death */
872 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
876 /* attempt to wake up a thread */
877 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
878 int wake_thread( struct thread
*thread
)
883 for (count
= 0; thread
->wait
; count
++)
885 if ((signaled
= check_wait( thread
)) == -1) break;
887 cookie
= thread
->wait
->cookie
;
888 signaled
= end_wait( thread
, signaled
);
889 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
890 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
892 if (!count
) count
= -1;
899 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
900 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
902 struct thread_wait
*wait
= entry
->wait
;
903 struct thread
*thread
= wait
->thread
;
907 if (thread
->wait
!= wait
) return 0; /* not the current wait */
908 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
910 assert( wait
->select
!= SELECT_WAIT_ALL
);
912 cookie
= wait
->cookie
;
913 signaled
= end_wait( thread
, entry
- wait
->queues
);
914 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
916 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
917 wake_thread( thread
); /* check other waits too */
922 /* thread wait timeout */
923 static void thread_timeout( void *ptr
)
925 struct thread_wait
*wait
= ptr
;
926 struct thread
*thread
= wait
->thread
;
927 client_ptr_t cookie
= wait
->cookie
;
930 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
931 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
933 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
934 end_wait( thread
, STATUS_TIMEOUT
);
937 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
938 /* check if other objects have become signaled in the meantime */
939 wake_thread( thread
);
942 /* try signaling an event flag, a semaphore or a mutex */
943 static int signal_object( obj_handle_t handle
)
948 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
951 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
952 release_object( obj
);
957 /* select on a list of handles */
958 static void select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
959 int flags
, abstime_t when
)
963 struct object
*object
;
965 switch (select_op
->op
)
968 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return;
972 case SELECT_WAIT_ALL
:
973 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
974 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
976 set_error( STATUS_INVALID_PARAMETER
);
979 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
983 case SELECT_SIGNAL_AND_WAIT
:
984 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
986 if (select_op
->signal_and_wait
.signal
)
988 if (!signal_object( select_op
->signal_and_wait
.signal
))
990 end_wait( current
, get_error() );
993 /* check if we woke ourselves up */
994 if (!current
->wait
) return;
998 case SELECT_KEYED_EVENT_WAIT
:
999 case SELECT_KEYED_EVENT_RELEASE
:
1000 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1001 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1002 if (!object
) return;
1003 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1004 release_object( object
);
1006 current
->wait
->key
= select_op
->keyed_event
.key
;
1010 set_error( STATUS_INVALID_PARAMETER
);
1014 if ((ret
= check_wait( current
)) != -1)
1016 /* condition is already satisfied */
1017 set_error( end_wait( current
, ret
));
1021 /* now we need to wait */
1022 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1024 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1025 thread_timeout
, current
->wait
)))
1027 end_wait( current
, get_error() );
1031 current
->wait
->cookie
= cookie
;
1032 set_error( STATUS_PENDING
);
1036 /* attempt to wake threads sleeping on the object wait queue */
1037 void wake_up( struct object
*obj
, int max
)
1042 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1044 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1045 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1046 if (ret
> 0 && max
&& !--max
) break;
1047 /* restart at the head of the list since a wake up can change the object wait queue */
1048 ptr
= &obj
->wait_queue
;
1052 /* return the apc queue to use for a given apc type */
1053 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1060 return &thread
->user_apc
;
1062 return &thread
->system_apc
;
1066 /* check if thread is currently waiting for a (system) apc */
1067 static inline int is_in_apc_wait( struct thread
*thread
)
1069 return (thread
->process
->suspend
|| thread
->suspend
||
1070 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1073 /* queue an existing APC to a given thread */
1074 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1078 if (thread
&& thread
->state
== TERMINATED
&& process
)
1081 if (!thread
) /* find a suitable thread inside the process */
1083 struct thread
*candidate
;
1085 /* first try to find a waiting thread */
1086 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1088 if (candidate
->state
== TERMINATED
) continue;
1089 if (is_in_apc_wait( candidate
))
1097 /* then use the first one that accepts a signal */
1098 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1100 if (send_thread_signal( candidate
, SIGUSR1
))
1107 if (!thread
) return 0; /* nothing found */
1108 queue
= get_apc_queue( thread
, apc
->call
.type
);
1112 if (thread
->state
== TERMINATED
) return 0;
1113 queue
= get_apc_queue( thread
, apc
->call
.type
);
1114 /* send signal for system APCs if needed */
1115 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1117 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1119 /* cancel a possible previous APC with the same owner */
1120 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1124 list_add_tail( queue
, &apc
->entry
);
1125 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1126 wake_thread( thread
);
1131 /* queue an async procedure call */
1132 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1134 struct thread_apc
*apc
;
1137 if ((apc
= create_apc( owner
, call_data
)))
1139 ret
= queue_apc( process
, thread
, apc
);
1140 release_object( apc
);
1145 /* cancel the async procedure call owned by a specific object */
1146 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1148 struct thread_apc
*apc
;
1149 struct list
*queue
= get_apc_queue( thread
, type
);
1151 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1153 if (apc
->owner
!= owner
) continue;
1154 list_remove( &apc
->entry
);
1156 wake_up( &apc
->obj
, 0 );
1157 release_object( apc
);
1162 /* remove the head apc from the queue; the returned object must be released by the caller */
1163 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1165 struct thread_apc
*apc
= NULL
;
1166 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1170 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1176 /* clear an APC queue, cancelling all the APCs on it */
1177 static void clear_apc_queue( struct list
*queue
)
1181 while ((ptr
= list_head( queue
)))
1183 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1184 list_remove( &apc
->entry
);
1186 wake_up( &apc
->obj
, 0 );
1187 release_object( apc
);
1191 /* add an fd to the inflight list */
1192 /* return list index, or -1 on error */
1193 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1197 if (server
== -1) return -1;
1204 /* first check if we already have an entry for this fd */
1205 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1206 if (thread
->inflight
[i
].client
== client
)
1208 close( thread
->inflight
[i
].server
);
1209 thread
->inflight
[i
].server
= server
;
1213 /* now find a free spot to store it */
1214 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1215 if (thread
->inflight
[i
].client
== -1)
1217 thread
->inflight
[i
].client
= client
;
1218 thread
->inflight
[i
].server
= server
;
1226 /* get an inflight fd and purge it from the list */
1227 /* the fd must be closed when no longer used */
1228 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1232 if (client
== -1) return -1;
1236 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1238 if (thread
->inflight
[i
].client
== client
)
1240 ret
= thread
->inflight
[i
].server
;
1241 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1245 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1249 /* kill a thread on the spot */
1250 void kill_thread( struct thread
*thread
, int violent_death
)
1252 if (thread
->state
== TERMINATED
) return; /* already killed */
1253 thread
->state
= TERMINATED
;
1254 thread
->exit_time
= current_time
;
1255 if (current
== thread
) current
= NULL
;
1257 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1258 thread
->id
, thread
->exit_code
);
1261 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1262 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1263 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1266 kill_console_processes( thread
, 0 );
1267 abandon_mutexes( thread
);
1268 wake_up( &thread
->obj
, 0 );
1269 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1270 cleanup_thread( thread
);
1271 remove_process_thread( thread
->process
, thread
);
1272 release_object( thread
);
1275 /* copy parts of a context structure */
1276 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1278 assert( to
->cpu
== from
->cpu
);
1279 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1280 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1281 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1282 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1283 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1284 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1285 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1288 /* return the context flags that correspond to system regs */
1289 /* (system regs are the ones we can't access on the client side) */
1290 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1294 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1295 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1296 case CPU_POWERPC
: return 0;
1297 case CPU_ARM
: return SERVER_CTX_DEBUG_REGISTERS
;
1298 case CPU_ARM64
: return SERVER_CTX_DEBUG_REGISTERS
;
1303 /* gets the current impersonation token */
1304 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1307 return thread
->token
;
1309 return thread
->process
->token
;
1312 /* check if a cpu type can be supported on this server */
1313 int is_cpu_supported( enum cpu_type cpu
)
1315 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1317 if (supported_cpus
& prefix_cpu_mask
& CPU_FLAG(cpu
)) return 1;
1318 if (!(supported_cpus
& prefix_cpu_mask
))
1319 set_error( STATUS_NOT_SUPPORTED
);
1320 else if (supported_cpus
& CPU_FLAG(cpu
))
1321 set_error( STATUS_INVALID_IMAGE_WIN_64
); /* server supports it but not the prefix */
1323 set_error( STATUS_INVALID_IMAGE_FORMAT
);
1327 /* return the cpu mask for supported cpus */
1328 unsigned int get_supported_cpu_mask(void)
1330 return supported_cpus
& get_prefix_cpu_mask();
1333 /* create a new thread */
1334 DECL_HANDLER(new_thread
)
1336 struct thread
*thread
;
1337 struct process
*process
;
1338 struct unicode_str name
;
1339 const struct security_descriptor
*sd
;
1340 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1341 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1343 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1345 if (request_fd
!= -1) close( request_fd
);
1349 if (process
!= current
->process
)
1351 if (request_fd
!= -1) /* can't create a request fd in a different process */
1353 close( request_fd
);
1354 set_error( STATUS_INVALID_PARAMETER
);
1357 if (process
->running_threads
) /* only the initial thread can be created in another process */
1359 set_error( STATUS_ACCESS_DENIED
);
1363 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1365 if (request_fd
!= -1) close( request_fd
);
1366 set_error( STATUS_INVALID_HANDLE
);
1370 if ((thread
= create_thread( request_fd
, process
, sd
)))
1372 thread
->system_regs
= current
->system_regs
;
1373 if (req
->suspend
) thread
->suspend
++;
1374 reply
->tid
= get_thread_id( thread
);
1375 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1376 req
->access
, objattr
->attributes
)))
1378 /* thread object will be released when the thread gets killed */
1381 kill_thread( thread
, 1 );
1384 release_object( process
);
1387 /* initialize a new thread */
1388 DECL_HANDLER(init_thread
)
1390 struct process
*process
= current
->process
;
1391 int wait_fd
, reply_fd
;
1393 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1395 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1398 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1400 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1404 if (current
->reply_fd
) /* already initialised */
1406 set_error( STATUS_INVALID_PARAMETER
);
1410 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1412 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1413 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1414 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1416 if (!is_valid_address(req
->teb
))
1418 set_error( STATUS_INVALID_PARAMETER
);
1422 current
->unix_pid
= req
->unix_pid
;
1423 current
->unix_tid
= req
->unix_tid
;
1424 current
->teb
= req
->teb
;
1425 current
->entry_point
= process
->peb
? req
->entry
: 0;
1427 if (!process
->peb
) /* first thread, initialize the process too */
1429 if (!is_cpu_supported( req
->cpu
)) return;
1430 process
->unix_pid
= current
->unix_pid
;
1431 process
->peb
= req
->entry
;
1432 process
->cpu
= req
->cpu
;
1433 reply
->info_size
= init_process( current
);
1434 if (!process
->parent_id
)
1435 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1437 set_thread_affinity( current
, current
->affinity
);
1441 if (req
->cpu
!= process
->cpu
)
1443 set_error( STATUS_INVALID_PARAMETER
);
1446 if (process
->unix_pid
!= current
->unix_pid
)
1447 process
->unix_pid
= -1; /* can happen with linuxthreads */
1448 init_thread_context( current
);
1449 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1450 set_thread_affinity( current
, current
->affinity
);
1452 debug_level
= max( debug_level
, req
->debug_level
);
1454 reply
->pid
= get_process_id( process
);
1455 reply
->tid
= get_thread_id( current
);
1456 reply
->version
= SERVER_PROTOCOL_VERSION
;
1457 reply
->server_start
= server_start_time
;
1458 reply
->all_cpus
= supported_cpus
& get_prefix_cpu_mask();
1459 reply
->suspend
= (current
->suspend
|| process
->suspend
|| current
->context
!= NULL
);
1463 if (reply_fd
!= -1) close( reply_fd
);
1464 if (wait_fd
!= -1) close( wait_fd
);
1467 /* terminate a thread */
1468 DECL_HANDLER(terminate_thread
)
1470 struct thread
*thread
;
1472 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1474 thread
->exit_code
= req
->exit_code
;
1475 if (thread
!= current
) kill_thread( thread
, 1 );
1476 else reply
->self
= 1;
1477 release_object( thread
);
1481 /* open a handle to a thread */
1482 DECL_HANDLER(open_thread
)
1484 struct thread
*thread
= get_thread_from_id( req
->tid
);
1489 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1490 release_object( thread
);
1494 /* fetch information about a thread */
1495 DECL_HANDLER(get_thread_info
)
1497 struct thread
*thread
;
1498 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1500 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1501 thread
= get_thread_from_handle( req
->handle
, access
);
1504 reply
->pid
= get_process_id( thread
->process
);
1505 reply
->tid
= get_thread_id( thread
);
1506 reply
->teb
= thread
->teb
;
1507 reply
->entry_point
= thread
->entry_point
;
1508 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1509 reply
->priority
= thread
->priority
;
1510 reply
->affinity
= thread
->affinity
;
1511 reply
->last
= thread
->process
->running_threads
== 1;
1512 reply
->suspend_count
= thread
->suspend
;
1513 reply
->dbg_hidden
= thread
->dbg_hidden
;
1514 reply
->desc_len
= thread
->desc_len
;
1516 if (thread
->desc
&& get_reply_max_size())
1518 if (thread
->desc_len
<= get_reply_max_size())
1519 set_reply_data( thread
->desc
, thread
->desc_len
);
1521 set_error( STATUS_BUFFER_TOO_SMALL
);
1524 release_object( thread
);
1528 /* fetch information about thread times */
1529 DECL_HANDLER(get_thread_times
)
1531 struct thread
*thread
;
1533 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1535 reply
->creation_time
= thread
->creation_time
;
1536 reply
->exit_time
= thread
->exit_time
;
1537 reply
->unix_pid
= thread
->unix_pid
;
1538 reply
->unix_tid
= thread
->unix_tid
;
1540 release_object( thread
);
1544 /* set information about a thread */
1545 DECL_HANDLER(set_thread_info
)
1547 struct thread
*thread
;
1549 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1551 set_thread_info( thread
, req
);
1552 release_object( thread
);
1556 /* suspend a thread */
1557 DECL_HANDLER(suspend_thread
)
1559 struct thread
*thread
;
1561 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1563 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1564 else reply
->count
= suspend_thread( thread
);
1565 release_object( thread
);
1569 /* resume a thread */
1570 DECL_HANDLER(resume_thread
)
1572 struct thread
*thread
;
1574 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1576 reply
->count
= resume_thread( thread
);
1577 release_object( thread
);
1581 /* select on a handle list */
1582 DECL_HANDLER(select
)
1584 select_op_t select_op
;
1585 data_size_t op_size
;
1586 struct thread_apc
*apc
;
1587 const apc_result_t
*result
= get_req_data();
1589 if (get_req_data_size() < sizeof(*result
) ||
1590 get_req_data_size() - sizeof(*result
) < req
->size
||
1593 set_error( STATUS_INVALID_PARAMETER
);
1597 if (get_req_data_size() - sizeof(*result
) - req
->size
== sizeof(context_t
))
1599 const context_t
*context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1600 if ((current
->context
&& current
->context
->status
!= STATUS_PENDING
) || context
->cpu
!= current
->process
->cpu
)
1602 set_error( STATUS_INVALID_PARAMETER
);
1606 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1607 copy_context( ¤t
->context
->regs
, context
,
1608 context
->flags
& ~(current
->context
->regs
.flags
| get_context_system_regs(current
->process
->cpu
)) );
1609 current
->context
->status
= STATUS_SUCCESS
;
1610 current
->suspend_cookie
= req
->cookie
;
1611 wake_up( ¤t
->context
->obj
, 0 );
1616 set_error( STATUS_INVALID_PARAMETER
);
1620 op_size
= min( req
->size
, sizeof(select_op
) );
1621 memset( &select_op
, 0, sizeof(select_op
) );
1622 memcpy( &select_op
, result
+ 1, op_size
);
1624 /* first store results of previous apc */
1627 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1628 0, &thread_apc_ops
))) return;
1629 apc
->result
= *result
;
1631 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1633 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1634 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1635 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1636 apc
->result
.create_thread
.handle
= handle
;
1637 clear_error(); /* ignore errors from the above calls */
1639 else if (apc
->result
.type
== APC_ASYNC_IO
)
1642 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
1644 wake_up( &apc
->obj
, 0 );
1645 close_handle( current
->process
, req
->prev_apc
);
1646 release_object( apc
);
1649 select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1651 while (get_error() == STATUS_USER_APC
)
1653 if (!(apc
= thread_dequeue_apc( current
, 0 )))
1655 /* Optimization: ignore APC_NONE calls, they are only used to
1656 * wake up a thread, but since we got here the thread woke up already.
1658 if (apc
->call
.type
!= APC_NONE
&&
1659 (reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1661 reply
->call
= apc
->call
;
1662 release_object( apc
);
1666 wake_up( &apc
->obj
, 0 );
1667 release_object( apc
);
1670 if (get_error() == STATUS_KERNEL_APC
)
1672 apc
= thread_dequeue_apc( current
, 1 );
1673 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1674 reply
->call
= apc
->call
;
1678 wake_up( &apc
->obj
, 0 );
1680 release_object( apc
);
1682 else if (get_error() != STATUS_PENDING
&& get_reply_max_size() == sizeof(context_t
) &&
1683 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1685 if (current
->context
->regs
.flags
)
1687 unsigned int system_flags
= get_context_system_regs(current
->process
->cpu
) &
1688 current
->context
->regs
.flags
;
1689 if (system_flags
) set_thread_context( current
, ¤t
->context
->regs
, system_flags
);
1690 set_reply_data( ¤t
->context
->regs
, sizeof(context_t
) );
1692 release_object( current
->context
);
1693 current
->context
= NULL
;
1697 /* queue an APC for a thread or process */
1698 DECL_HANDLER(queue_apc
)
1700 struct thread
*thread
= NULL
;
1701 struct process
*process
= NULL
;
1702 struct thread_apc
*apc
;
1704 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1706 switch (apc
->call
.type
)
1710 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1712 case APC_VIRTUAL_ALLOC
:
1713 case APC_VIRTUAL_FREE
:
1714 case APC_VIRTUAL_PROTECT
:
1715 case APC_VIRTUAL_FLUSH
:
1716 case APC_VIRTUAL_LOCK
:
1717 case APC_VIRTUAL_UNLOCK
:
1718 case APC_UNMAP_VIEW
:
1719 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1721 case APC_VIRTUAL_QUERY
:
1722 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1725 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1726 if (process
&& process
!= current
->process
)
1728 /* duplicate the handle into the target process */
1729 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1730 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1731 if (handle
) apc
->call
.map_view
.handle
= handle
;
1734 release_object( process
);
1739 case APC_CREATE_THREAD
:
1740 case APC_BREAK_PROCESS
:
1741 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1744 set_error( STATUS_INVALID_PARAMETER
);
1750 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1751 release_object( thread
);
1755 reply
->self
= (process
== current
->process
);
1758 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1761 if (queue_apc( process
, NULL
, apc
))
1763 apc
->caller
= (struct thread
*)grab_object( current
);
1764 reply
->handle
= handle
;
1768 close_handle( current
->process
, handle
);
1769 set_error( STATUS_PROCESS_IS_TERMINATING
);
1773 release_object( process
);
1776 release_object( apc
);
1779 /* Get the result of an APC call */
1780 DECL_HANDLER(get_apc_result
)
1782 struct thread_apc
*apc
;
1784 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1785 0, &thread_apc_ops
))) return;
1787 if (apc
->executed
) reply
->result
= apc
->result
;
1788 else set_error( STATUS_PENDING
);
1790 /* close the handle directly to avoid an extra round-trip */
1791 close_handle( current
->process
, req
->handle
);
1792 release_object( apc
);
1795 /* retrieve the current context of a thread */
1796 DECL_HANDLER(get_thread_context
)
1798 struct context
*thread_context
= NULL
;
1799 unsigned int system_flags
;
1800 struct thread
*thread
;
1803 if (get_reply_max_size() < sizeof(context_t
))
1805 set_error( STATUS_INVALID_PARAMETER
);
1809 if ((thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->handle
, 0, &context_ops
)))
1811 close_handle( current
->process
, req
->handle
); /* avoid extra server call */
1812 system_flags
= get_context_system_regs( thread_context
->regs
.cpu
);
1814 else if ((thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
)))
1817 system_flags
= get_context_system_regs( thread
->process
->cpu
);
1818 if (thread
->state
== RUNNING
)
1820 reply
->self
= (thread
== current
);
1821 if (thread
!= current
) stop_thread( thread
);
1822 if (thread
->context
)
1824 /* make sure that system regs are valid in thread context */
1825 if (thread
->unix_tid
!= -1 && (req
->flags
& system_flags
& ~thread
->context
->regs
.flags
))
1826 get_thread_context( thread
, &thread
->context
->regs
, req
->flags
& system_flags
);
1827 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1829 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1831 assert( reply
->self
);
1832 memset( context
, 0, sizeof(context_t
) );
1833 context
->cpu
= thread
->process
->cpu
;
1834 if (req
->flags
& system_flags
)
1836 get_thread_context( thread
, context
, req
->flags
& system_flags
);
1837 context
->flags
|= req
->flags
& system_flags
;
1841 else set_error( STATUS_UNSUCCESSFUL
);
1842 release_object( thread
);
1844 if (get_error() || !thread_context
) return;
1846 set_error( thread_context
->status
);
1847 if (!thread_context
->status
&& (context
= set_reply_data_size( sizeof(context_t
) )))
1849 memset( context
, 0, sizeof(context_t
) );
1850 context
->cpu
= thread_context
->regs
.cpu
;
1851 copy_context( context
, &thread_context
->regs
, req
->flags
);
1852 context
->flags
= req
->flags
;
1854 else if (thread_context
->status
== STATUS_PENDING
)
1856 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1859 release_object( thread_context
);
1862 /* set the current context of a thread */
1863 DECL_HANDLER(set_thread_context
)
1865 struct thread
*thread
;
1866 const context_t
*context
= get_req_data();
1868 if (get_req_data_size() < sizeof(context_t
))
1870 set_error( STATUS_INVALID_PARAMETER
);
1873 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1874 reply
->self
= (thread
== current
);
1876 if (thread
->state
== TERMINATED
) set_error( STATUS_UNSUCCESSFUL
);
1877 else if (context
->cpu
!= thread
->process
->cpu
) set_error( STATUS_INVALID_PARAMETER
);
1880 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1882 if (thread
!= current
) stop_thread( thread
);
1883 else if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1884 if (thread
->context
&& !get_error())
1886 copy_context( &thread
->context
->regs
, context
, context
->flags
);
1887 thread
->context
->regs
.flags
|= context
->flags
;
1891 release_object( thread
);
1894 /* fetch a selector entry for a thread */
1895 DECL_HANDLER(get_selector_entry
)
1897 struct thread
*thread
;
1898 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1900 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1901 release_object( thread
);