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 */
109 dump_thread_apc
, /* dump */
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 default_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 */
146 dump_context
, /* dump */
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 default_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 const WCHAR thread_name
[] = {'T','h','r','e','a','d'};
171 struct type_descr thread_type
=
173 { thread_name
, sizeof(thread_name
) }, /* name */
174 THREAD_ALL_ACCESS
, /* valid_access */
176 STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
,
177 STANDARD_RIGHTS_WRITE
| THREAD_SET_LIMITED_INFORMATION
| THREAD_SET_INFORMATION
178 | THREAD_SET_CONTEXT
| THREAD_SUSPEND_RESUME
| THREAD_TERMINATE
| 0x04,
179 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_RESUME
| THREAD_QUERY_LIMITED_INFORMATION
,
184 static void dump_thread( struct object
*obj
, int verbose
);
185 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
186 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
187 static void thread_poll_event( struct fd
*fd
, int event
);
188 static struct list
*thread_get_kernel_obj_list( struct object
*obj
);
189 static void destroy_thread( struct object
*obj
);
191 static const struct object_ops thread_ops
=
193 sizeof(struct thread
), /* size */
194 &thread_type
, /* type */
195 dump_thread
, /* dump */
196 add_queue
, /* add_queue */
197 remove_queue
, /* remove_queue */
198 thread_signaled
, /* signaled */
199 no_satisfied
, /* satisfied */
200 no_signal
, /* signal */
201 no_get_fd
, /* get_fd */
202 thread_map_access
, /* map_access */
203 default_get_sd
, /* get_sd */
204 default_set_sd
, /* set_sd */
205 no_get_full_name
, /* get_full_name */
206 no_lookup_name
, /* lookup_name */
207 no_link_name
, /* link_name */
208 NULL
, /* unlink_name */
209 no_open_file
, /* open_file */
210 thread_get_kernel_obj_list
, /* get_kernel_obj_list */
211 no_close_handle
, /* close_handle */
212 destroy_thread
/* destroy */
215 static const struct fd_ops thread_fd_ops
=
217 NULL
, /* get_poll_events */
218 thread_poll_event
, /* poll_event */
220 NULL
, /* get_fd_type */
222 NULL
, /* queue_async */
223 NULL
/* reselect_async */
226 static struct list thread_list
= LIST_INIT(thread_list
);
228 /* initialize the structure for a newly allocated thread */
229 static inline void init_thread_structure( struct thread
*thread
)
233 thread
->unix_pid
= -1; /* not known yet */
234 thread
->unix_tid
= -1; /* not known yet */
235 thread
->context
= NULL
;
237 thread
->entry_point
= 0;
238 thread
->system_regs
= 0;
239 thread
->queue
= NULL
;
242 thread
->req_data
= NULL
;
243 thread
->req_toread
= 0;
244 thread
->reply_data
= NULL
;
245 thread
->reply_towrite
= 0;
246 thread
->request_fd
= NULL
;
247 thread
->reply_fd
= NULL
;
248 thread
->wait_fd
= NULL
;
249 thread
->state
= RUNNING
;
250 thread
->exit_code
= 0;
251 thread
->priority
= 0;
253 thread
->dbg_hidden
= 0;
254 thread
->desktop_users
= 0;
255 thread
->token
= NULL
;
257 thread
->desc_len
= 0;
259 thread
->creation_time
= current_time
;
260 thread
->exit_time
= 0;
262 list_init( &thread
->mutex_list
);
263 list_init( &thread
->system_apc
);
264 list_init( &thread
->user_apc
);
265 list_init( &thread
->kernel_object
);
267 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
268 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
271 /* check if address looks valid for a client-side data structure (TEB etc.) */
272 static inline int is_valid_address( client_ptr_t addr
)
274 return addr
&& !(addr
% sizeof(int));
278 /* dump a context on stdout for debugging purposes */
279 static void dump_context( struct object
*obj
, int verbose
)
281 struct context
*context
= (struct context
*)obj
;
282 assert( obj
->ops
== &context_ops
);
284 fprintf( stderr
, "context flags=%x\n", context
->regs
.flags
);
288 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
290 struct context
*context
= (struct context
*)obj
;
291 return context
->status
!= STATUS_PENDING
;
295 static struct context
*create_thread_context( struct thread
*thread
)
297 struct context
*context
;
298 if (!(context
= alloc_object( &context_ops
))) return NULL
;
299 context
->status
= STATUS_PENDING
;
300 memset( &context
->regs
, 0, sizeof(context
->regs
) );
301 context
->regs
.cpu
= thread
->process
->cpu
;
306 /* create a new thread */
307 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
309 struct thread
*thread
;
314 if (pipe( request_pipe
) == -1)
319 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
321 close( request_pipe
[0] );
322 close( request_pipe
[1] );
325 close( request_pipe
[1] );
326 fd
= request_pipe
[0];
329 if (process
->is_terminating
)
332 set_error( STATUS_PROCESS_IS_TERMINATING
);
336 if (!(thread
= alloc_object( &thread_ops
)))
342 init_thread_structure( thread
);
344 thread
->process
= (struct process
*)grab_object( process
);
345 thread
->desktop
= process
->desktop
;
346 thread
->affinity
= process
->affinity
;
347 if (!current
) current
= thread
;
349 list_add_tail( &thread_list
, &thread
->entry
);
351 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
352 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
353 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
357 release_object( thread
);
360 if (!(thread
->id
= alloc_ptid( thread
)))
363 release_object( thread
);
366 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
368 release_object( thread
);
372 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
373 add_process_thread( thread
->process
, thread
);
377 /* handle a client event */
378 static void thread_poll_event( struct fd
*fd
, int event
)
380 struct thread
*thread
= get_fd_user( fd
);
381 assert( thread
->obj
.ops
== &thread_ops
);
383 grab_object( thread
);
384 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
385 else if (event
& POLLIN
) read_request( thread
);
386 else if (event
& POLLOUT
) write_reply( thread
);
387 release_object( thread
);
390 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
392 struct thread
*thread
= (struct thread
*)obj
;
393 return &thread
->kernel_object
;
396 /* cleanup everything that is no longer needed by a dead thread */
397 /* used by destroy_thread and kill_thread */
398 static void cleanup_thread( struct thread
*thread
)
404 thread
->context
->status
= STATUS_ACCESS_DENIED
;
405 wake_up( &thread
->context
->obj
, 0 );
406 release_object( thread
->context
);
407 thread
->context
= NULL
;
409 clear_apc_queue( &thread
->system_apc
);
410 clear_apc_queue( &thread
->user_apc
);
411 free( thread
->req_data
);
412 free( thread
->reply_data
);
413 if (thread
->request_fd
) release_object( thread
->request_fd
);
414 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
415 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
416 cleanup_clipboard_thread(thread
);
417 destroy_thread_windows( thread
);
418 free_msg_queue( thread
);
419 close_thread_desktop( thread
);
420 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
422 if (thread
->inflight
[i
].client
!= -1)
424 close( thread
->inflight
[i
].server
);
425 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
428 free( thread
->desc
);
429 thread
->req_data
= NULL
;
430 thread
->reply_data
= NULL
;
431 thread
->request_fd
= NULL
;
432 thread
->reply_fd
= NULL
;
433 thread
->wait_fd
= NULL
;
436 thread
->desc_len
= 0;
439 /* destroy a thread when its refcount is 0 */
440 static void destroy_thread( struct object
*obj
)
442 struct thread
*thread
= (struct thread
*)obj
;
443 assert( obj
->ops
== &thread_ops
);
445 list_remove( &thread
->entry
);
446 cleanup_thread( thread
);
447 release_object( thread
->process
);
448 if (thread
->id
) free_ptid( thread
->id
);
449 if (thread
->token
) release_object( thread
->token
);
452 /* dump a thread on stdout for debugging purposes */
453 static void dump_thread( struct object
*obj
, int verbose
)
455 struct thread
*thread
= (struct thread
*)obj
;
456 assert( obj
->ops
== &thread_ops
);
458 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
459 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
462 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
464 struct thread
*mythread
= (struct thread
*)obj
;
465 return (mythread
->state
== TERMINATED
);
468 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
470 access
= default_map_access( obj
, access
);
471 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
472 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
476 static void dump_thread_apc( struct object
*obj
, int verbose
)
478 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
479 assert( obj
->ops
== &thread_apc_ops
);
481 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
484 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
486 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
487 return apc
->executed
;
490 static void thread_apc_destroy( struct object
*obj
)
492 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
494 if (apc
->caller
) release_object( apc
->caller
);
497 if (apc
->result
.type
== APC_ASYNC_IO
)
498 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
499 else if (apc
->call
.type
== APC_ASYNC_IO
)
500 async_set_result( apc
->owner
, apc
->call
.async_io
.status
, 0 );
501 release_object( apc
->owner
);
505 /* queue an async procedure call */
506 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
508 struct thread_apc
*apc
;
510 if ((apc
= alloc_object( &thread_apc_ops
)))
512 apc
->call
= *call_data
;
516 apc
->result
.type
= APC_NONE
;
517 if (owner
) grab_object( owner
);
522 /* get a thread pointer from a thread id (and increment the refcount) */
523 struct thread
*get_thread_from_id( thread_id_t id
)
525 struct object
*obj
= get_ptid_entry( id
);
527 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
528 set_error( STATUS_INVALID_CID
);
532 /* get a thread from a handle (and increment the refcount) */
533 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
535 return (struct thread
*)get_handle_obj( current
->process
, handle
,
536 access
, &thread_ops
);
539 /* find a thread from a Unix tid */
540 struct thread
*get_thread_from_tid( int tid
)
542 struct thread
*thread
;
544 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
546 if (thread
->unix_tid
== tid
) return thread
;
551 /* find a thread from a Unix pid */
552 struct thread
*get_thread_from_pid( int pid
)
554 struct thread
*thread
;
556 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
558 if (thread
->unix_pid
== pid
) return thread
;
563 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
566 #ifdef HAVE_SCHED_SETAFFINITY
567 if (thread
->unix_tid
!= -1)
574 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
575 if (affinity
& mask
) CPU_SET( i
, &set
);
577 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
580 if (!ret
) thread
->affinity
= affinity
;
584 affinity_t
get_thread_affinity( struct thread
*thread
)
587 #ifdef HAVE_SCHED_SETAFFINITY
588 if (thread
->unix_tid
!= -1)
593 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
594 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
595 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
598 if (!mask
) mask
= ~(affinity_t
)0;
602 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
603 #define THREAD_PRIORITY_REALTIME_LOWEST -7
605 /* set all information about a thread */
606 static void set_thread_info( struct thread
*thread
,
607 const struct set_thread_info_request
*req
)
609 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
611 int max
= THREAD_PRIORITY_HIGHEST
;
612 int min
= THREAD_PRIORITY_LOWEST
;
613 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
615 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
616 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
618 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
619 req
->priority
== THREAD_PRIORITY_IDLE
||
620 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
621 thread
->priority
= req
->priority
;
623 set_error( STATUS_INVALID_PARAMETER
);
625 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
627 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
628 set_error( STATUS_INVALID_PARAMETER
);
629 else if (thread
->state
== TERMINATED
)
630 set_error( STATUS_THREAD_IS_TERMINATING
);
631 else if (set_thread_affinity( thread
, req
->affinity
))
634 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
635 security_set_thread_token( thread
, req
->token
);
636 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
637 thread
->entry_point
= req
->entry_point
;
638 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
639 thread
->dbg_hidden
= 1;
640 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
643 data_size_t desc_len
= get_req_data_size();
647 if ((desc
= mem_alloc( desc_len
)))
649 memcpy( desc
, get_req_data(), desc_len
);
650 free( thread
->desc
);
652 thread
->desc_len
= desc_len
;
657 free( thread
->desc
);
659 thread
->desc_len
= 0;
664 /* stop a thread (at the Unix level) */
665 void stop_thread( struct thread
*thread
)
667 if (thread
->context
) return; /* already suspended, no need for a signal */
668 if (!(thread
->context
= create_thread_context( thread
))) return;
669 /* can't stop a thread while initialisation is in progress */
670 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
673 /* suspend a thread */
674 int suspend_thread( struct thread
*thread
)
676 int old_count
= thread
->suspend
;
677 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
679 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
681 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
685 /* resume a thread */
686 int resume_thread( struct thread
*thread
)
688 int old_count
= thread
->suspend
;
689 if (thread
->suspend
> 0)
691 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
692 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
697 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
698 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
702 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
706 /* remove a thread from an object wait queue */
707 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
709 list_remove( &entry
->entry
);
710 release_object( obj
);
713 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
715 return entry
->wait
->thread
;
718 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
720 return entry
->wait
->select
;
723 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
725 return entry
->wait
->key
;
728 void make_wait_abandoned( struct wait_queue_entry
*entry
)
730 entry
->wait
->abandoned
= 1;
734 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
736 struct thread_wait
*wait
= thread
->wait
;
737 struct wait_queue_entry
*entry
;
741 thread
->wait
= wait
->next
;
743 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
745 if (wait
->select
== SELECT_WAIT_ALL
)
747 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
748 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
752 entry
= wait
->queues
+ status
;
753 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
755 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
757 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
758 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
759 if (wait
->user
) remove_timeout_user( wait
->user
);
764 /* build the thread wait structure */
765 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
766 int flags
, abstime_t when
)
768 struct thread_wait
*wait
;
769 struct wait_queue_entry
*entry
;
772 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
773 wait
->next
= current
->wait
;
774 wait
->thread
= current
;
777 wait
->select
= select_op
->op
;
782 current
->wait
= wait
;
784 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
786 struct object
*obj
= objects
[i
];
788 if (!obj
->ops
->add_queue( obj
, entry
))
791 end_wait( current
, get_error() );
798 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
799 int flags
, abstime_t when
)
801 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
805 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
807 for (i
= 0; i
< count
; i
++)
808 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
811 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
813 while (i
> 0) release_object( objects
[--i
] );
817 /* check if the thread waiting condition is satisfied */
818 static int check_wait( struct thread
*thread
)
821 struct thread_wait
*wait
= thread
->wait
;
822 struct wait_queue_entry
*entry
;
826 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
827 return STATUS_KERNEL_APC
;
829 /* Suspended threads may not acquire locks, but they can run system APCs */
830 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
832 if (wait
->select
== SELECT_WAIT_ALL
)
835 /* Note: we must check them all anyway, as some objects may
836 * want to do something when signaled, even if others are not */
837 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
838 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
839 if (!not_ok
) return STATUS_WAIT_0
;
843 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
844 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
847 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
848 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
849 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
853 /* send the wakeup signal to a thread */
854 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
856 struct wake_up_reply reply
;
859 /* check if we're waking current suspend wait */
860 if (thread
->context
&& thread
->suspend_cookie
== cookie
861 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
863 if (!thread
->context
->regs
.flags
)
865 release_object( thread
->context
);
866 thread
->context
= NULL
;
868 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
871 memset( &reply
, 0, sizeof(reply
) );
872 reply
.cookie
= cookie
;
873 reply
.signaled
= signaled
;
874 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
877 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
878 else if (errno
== EPIPE
)
879 kill_thread( thread
, 0 ); /* normal death */
881 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
885 /* attempt to wake up a thread */
886 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
887 int wake_thread( struct thread
*thread
)
892 for (count
= 0; thread
->wait
; count
++)
894 if ((signaled
= check_wait( thread
)) == -1) break;
896 cookie
= thread
->wait
->cookie
;
897 signaled
= end_wait( thread
, signaled
);
898 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
899 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
901 if (!count
) count
= -1;
908 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
909 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
911 struct thread_wait
*wait
= entry
->wait
;
912 struct thread
*thread
= wait
->thread
;
916 if (thread
->wait
!= wait
) return 0; /* not the current wait */
917 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
919 assert( wait
->select
!= SELECT_WAIT_ALL
);
921 cookie
= wait
->cookie
;
922 signaled
= end_wait( thread
, entry
- wait
->queues
);
923 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
925 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
926 wake_thread( thread
); /* check other waits too */
931 /* thread wait timeout */
932 static void thread_timeout( void *ptr
)
934 struct thread_wait
*wait
= ptr
;
935 struct thread
*thread
= wait
->thread
;
936 client_ptr_t cookie
= wait
->cookie
;
939 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
940 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
942 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
943 end_wait( thread
, STATUS_TIMEOUT
);
946 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
947 /* check if other objects have become signaled in the meantime */
948 wake_thread( thread
);
951 /* try signaling an event flag, a semaphore or a mutex */
952 static int signal_object( obj_handle_t handle
)
957 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
960 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
961 release_object( obj
);
966 /* select on a list of handles */
967 static void select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
968 int flags
, abstime_t when
)
972 struct object
*object
;
974 switch (select_op
->op
)
977 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return;
981 case SELECT_WAIT_ALL
:
982 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
983 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
985 set_error( STATUS_INVALID_PARAMETER
);
988 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
992 case SELECT_SIGNAL_AND_WAIT
:
993 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
995 if (select_op
->signal_and_wait
.signal
)
997 if (!signal_object( select_op
->signal_and_wait
.signal
))
999 end_wait( current
, get_error() );
1002 /* check if we woke ourselves up */
1003 if (!current
->wait
) return;
1007 case SELECT_KEYED_EVENT_WAIT
:
1008 case SELECT_KEYED_EVENT_RELEASE
:
1009 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1010 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1011 if (!object
) return;
1012 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1013 release_object( object
);
1015 current
->wait
->key
= select_op
->keyed_event
.key
;
1019 set_error( STATUS_INVALID_PARAMETER
);
1023 if ((ret
= check_wait( current
)) != -1)
1025 /* condition is already satisfied */
1026 set_error( end_wait( current
, ret
));
1030 /* now we need to wait */
1031 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1033 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1034 thread_timeout
, current
->wait
)))
1036 end_wait( current
, get_error() );
1040 current
->wait
->cookie
= cookie
;
1041 set_error( STATUS_PENDING
);
1045 /* attempt to wake threads sleeping on the object wait queue */
1046 void wake_up( struct object
*obj
, int max
)
1051 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1053 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1054 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1055 if (ret
> 0 && max
&& !--max
) break;
1056 /* restart at the head of the list since a wake up can change the object wait queue */
1057 ptr
= &obj
->wait_queue
;
1061 /* return the apc queue to use for a given apc type */
1062 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1069 return &thread
->user_apc
;
1071 return &thread
->system_apc
;
1075 /* check if thread is currently waiting for a (system) apc */
1076 static inline int is_in_apc_wait( struct thread
*thread
)
1078 return (thread
->process
->suspend
|| thread
->suspend
||
1079 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1082 /* queue an existing APC to a given thread */
1083 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1087 if (thread
&& thread
->state
== TERMINATED
&& process
)
1090 if (!thread
) /* find a suitable thread inside the process */
1092 struct thread
*candidate
;
1094 /* first try to find a waiting thread */
1095 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1097 if (candidate
->state
== TERMINATED
) continue;
1098 if (is_in_apc_wait( candidate
))
1106 /* then use the first one that accepts a signal */
1107 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1109 if (send_thread_signal( candidate
, SIGUSR1
))
1116 if (!thread
) return 0; /* nothing found */
1117 queue
= get_apc_queue( thread
, apc
->call
.type
);
1121 if (thread
->state
== TERMINATED
) return 0;
1122 queue
= get_apc_queue( thread
, apc
->call
.type
);
1123 /* send signal for system APCs if needed */
1124 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1126 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1128 /* cancel a possible previous APC with the same owner */
1129 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1133 list_add_tail( queue
, &apc
->entry
);
1134 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1135 wake_thread( thread
);
1140 /* queue an async procedure call */
1141 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1143 struct thread_apc
*apc
;
1146 if ((apc
= create_apc( owner
, call_data
)))
1148 ret
= queue_apc( process
, thread
, apc
);
1149 release_object( apc
);
1154 /* cancel the async procedure call owned by a specific object */
1155 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1157 struct thread_apc
*apc
;
1158 struct list
*queue
= get_apc_queue( thread
, type
);
1160 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1162 if (apc
->owner
!= owner
) continue;
1163 list_remove( &apc
->entry
);
1165 wake_up( &apc
->obj
, 0 );
1166 release_object( apc
);
1171 /* remove the head apc from the queue; the returned object must be released by the caller */
1172 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1174 struct thread_apc
*apc
= NULL
;
1175 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1179 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1185 /* clear an APC queue, cancelling all the APCs on it */
1186 static void clear_apc_queue( struct list
*queue
)
1190 while ((ptr
= list_head( queue
)))
1192 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1193 list_remove( &apc
->entry
);
1195 wake_up( &apc
->obj
, 0 );
1196 release_object( apc
);
1200 /* add an fd to the inflight list */
1201 /* return list index, or -1 on error */
1202 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1206 if (server
== -1) return -1;
1213 /* first check if we already have an entry for this fd */
1214 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1215 if (thread
->inflight
[i
].client
== client
)
1217 close( thread
->inflight
[i
].server
);
1218 thread
->inflight
[i
].server
= server
;
1222 /* now find a free spot to store it */
1223 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1224 if (thread
->inflight
[i
].client
== -1)
1226 thread
->inflight
[i
].client
= client
;
1227 thread
->inflight
[i
].server
= server
;
1235 /* get an inflight fd and purge it from the list */
1236 /* the fd must be closed when no longer used */
1237 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1241 if (client
== -1) return -1;
1245 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1247 if (thread
->inflight
[i
].client
== client
)
1249 ret
= thread
->inflight
[i
].server
;
1250 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1254 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1258 /* kill a thread on the spot */
1259 void kill_thread( struct thread
*thread
, int violent_death
)
1261 if (thread
->state
== TERMINATED
) return; /* already killed */
1262 thread
->state
= TERMINATED
;
1263 thread
->exit_time
= current_time
;
1264 if (current
== thread
) current
= NULL
;
1266 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1267 thread
->id
, thread
->exit_code
);
1270 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1271 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1272 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1275 kill_console_processes( thread
, 0 );
1276 abandon_mutexes( thread
);
1277 wake_up( &thread
->obj
, 0 );
1278 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1279 cleanup_thread( thread
);
1280 remove_process_thread( thread
->process
, thread
);
1281 release_object( thread
);
1284 /* copy parts of a context structure */
1285 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1287 assert( to
->cpu
== from
->cpu
);
1288 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1289 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1290 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1291 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1292 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1293 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1294 if (flags
& SERVER_CTX_YMM_REGISTERS
) to
->ymm
= from
->ymm
;
1297 /* return the context flags that correspond to system regs */
1298 /* (system regs are the ones we can't access on the client side) */
1299 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1303 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1304 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1305 case CPU_POWERPC
: return 0;
1306 case CPU_ARM
: return SERVER_CTX_DEBUG_REGISTERS
;
1307 case CPU_ARM64
: return SERVER_CTX_DEBUG_REGISTERS
;
1312 /* gets the current impersonation token */
1313 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1316 return thread
->token
;
1318 return thread
->process
->token
;
1321 /* check if a cpu type can be supported on this server */
1322 int is_cpu_supported( enum cpu_type cpu
)
1324 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1326 if (supported_cpus
& prefix_cpu_mask
& CPU_FLAG(cpu
)) return 1;
1327 if (!(supported_cpus
& prefix_cpu_mask
))
1328 set_error( STATUS_NOT_SUPPORTED
);
1329 else if (supported_cpus
& CPU_FLAG(cpu
))
1330 set_error( STATUS_INVALID_IMAGE_WIN_64
); /* server supports it but not the prefix */
1332 set_error( STATUS_INVALID_IMAGE_FORMAT
);
1336 /* return the cpu mask for supported cpus */
1337 unsigned int get_supported_cpu_mask(void)
1339 return supported_cpus
& get_prefix_cpu_mask();
1342 /* create a new thread */
1343 DECL_HANDLER(new_thread
)
1345 struct thread
*thread
;
1346 struct process
*process
;
1347 struct unicode_str name
;
1348 const struct security_descriptor
*sd
;
1349 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1350 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1352 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1354 if (request_fd
!= -1) close( request_fd
);
1358 if (process
!= current
->process
)
1360 if (request_fd
!= -1) /* can't create a request fd in a different process */
1362 close( request_fd
);
1363 set_error( STATUS_INVALID_PARAMETER
);
1366 if (process
->running_threads
) /* only the initial thread can be created in another process */
1368 set_error( STATUS_ACCESS_DENIED
);
1372 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1374 if (request_fd
!= -1) close( request_fd
);
1375 set_error( STATUS_INVALID_HANDLE
);
1379 if ((thread
= create_thread( request_fd
, process
, sd
)))
1381 thread
->system_regs
= current
->system_regs
;
1382 if (req
->suspend
) thread
->suspend
++;
1383 reply
->tid
= get_thread_id( thread
);
1384 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1385 req
->access
, objattr
->attributes
)))
1387 /* thread object will be released when the thread gets killed */
1390 kill_thread( thread
, 1 );
1393 release_object( process
);
1396 static int init_thread( struct thread
*thread
, int reply_fd
, int wait_fd
)
1398 if ((reply_fd
= thread_get_inflight_fd( thread
, reply_fd
)) == -1)
1400 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1403 if ((wait_fd
= thread_get_inflight_fd( thread
, wait_fd
)) == -1)
1405 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1409 if (thread
->reply_fd
) /* already initialised */
1411 set_error( STATUS_INVALID_PARAMETER
);
1415 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1417 thread
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, &thread
->obj
, 0 );
1418 thread
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, &thread
->obj
, 0 );
1419 return thread
->reply_fd
&& thread
->wait_fd
;
1422 if (reply_fd
!= -1) close( reply_fd
);
1423 if (wait_fd
!= -1) close( wait_fd
);
1427 /* initialize the first thread of a new process */
1428 DECL_HANDLER(init_first_thread
)
1430 struct process
*process
= current
->process
;
1432 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1434 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
))
1436 set_error( STATUS_INVALID_PARAMETER
);
1440 if (!is_cpu_supported( req
->cpu
)) return;
1442 current
->unix_pid
= process
->unix_pid
= req
->unix_pid
;
1443 current
->unix_tid
= req
->unix_tid
;
1444 current
->teb
= req
->teb
;
1445 process
->peb
= req
->peb
;
1446 process
->ldt_copy
= req
->ldt_copy
;
1447 process
->cpu
= req
->cpu
;
1449 if (!process
->parent_id
)
1450 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1452 set_thread_affinity( current
, current
->affinity
);
1454 debug_level
= max( debug_level
, req
->debug_level
);
1456 reply
->pid
= get_process_id( process
);
1457 reply
->tid
= get_thread_id( current
);
1458 reply
->info_size
= get_process_startup_info_size( process
);
1459 reply
->server_start
= server_start_time
;
1460 reply
->all_cpus
= supported_cpus
& get_prefix_cpu_mask();
1463 /* initialize a new thread */
1464 DECL_HANDLER(init_thread
)
1466 if (!init_thread( current
, req
->reply_fd
, req
->wait_fd
)) return;
1468 if (!is_valid_address(req
->teb
))
1470 set_error( STATUS_INVALID_PARAMETER
);
1474 current
->unix_pid
= current
->process
->unix_pid
;
1475 current
->unix_tid
= req
->unix_tid
;
1476 current
->teb
= req
->teb
;
1477 current
->entry_point
= req
->entry
;
1479 init_thread_context( current
);
1480 generate_debug_event( current
, DbgCreateThreadStateChange
, &req
->entry
);
1481 set_thread_affinity( current
, current
->affinity
);
1483 reply
->pid
= get_process_id( current
->process
);
1484 reply
->tid
= get_thread_id( current
);
1485 reply
->suspend
= (current
->suspend
|| current
->process
->suspend
|| current
->context
!= NULL
);
1488 /* terminate a thread */
1489 DECL_HANDLER(terminate_thread
)
1491 struct thread
*thread
;
1493 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1495 thread
->exit_code
= req
->exit_code
;
1496 if (thread
!= current
) kill_thread( thread
, 1 );
1497 else reply
->self
= 1;
1498 release_object( thread
);
1502 /* open a handle to a thread */
1503 DECL_HANDLER(open_thread
)
1505 struct thread
*thread
= get_thread_from_id( req
->tid
);
1510 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1511 release_object( thread
);
1515 /* fetch information about a thread */
1516 DECL_HANDLER(get_thread_info
)
1518 struct thread
*thread
;
1519 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1521 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1522 thread
= get_thread_from_handle( req
->handle
, access
);
1525 reply
->pid
= get_process_id( thread
->process
);
1526 reply
->tid
= get_thread_id( thread
);
1527 reply
->teb
= thread
->teb
;
1528 reply
->entry_point
= thread
->entry_point
;
1529 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1530 reply
->priority
= thread
->priority
;
1531 reply
->affinity
= thread
->affinity
;
1532 reply
->last
= thread
->process
->running_threads
== 1;
1533 reply
->suspend_count
= thread
->suspend
;
1534 reply
->dbg_hidden
= thread
->dbg_hidden
;
1535 reply
->desc_len
= thread
->desc_len
;
1537 if (thread
->desc
&& get_reply_max_size())
1539 if (thread
->desc_len
<= get_reply_max_size())
1540 set_reply_data( thread
->desc
, thread
->desc_len
);
1542 set_error( STATUS_BUFFER_TOO_SMALL
);
1545 release_object( thread
);
1549 /* fetch information about thread times */
1550 DECL_HANDLER(get_thread_times
)
1552 struct thread
*thread
;
1554 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1556 reply
->creation_time
= thread
->creation_time
;
1557 reply
->exit_time
= thread
->exit_time
;
1558 reply
->unix_pid
= thread
->unix_pid
;
1559 reply
->unix_tid
= thread
->unix_tid
;
1561 release_object( thread
);
1565 /* set information about a thread */
1566 DECL_HANDLER(set_thread_info
)
1568 struct thread
*thread
;
1570 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1572 set_thread_info( thread
, req
);
1573 release_object( thread
);
1577 /* suspend a thread */
1578 DECL_HANDLER(suspend_thread
)
1580 struct thread
*thread
;
1582 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1584 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1585 else reply
->count
= suspend_thread( thread
);
1586 release_object( thread
);
1590 /* resume a thread */
1591 DECL_HANDLER(resume_thread
)
1593 struct thread
*thread
;
1595 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1597 reply
->count
= resume_thread( thread
);
1598 release_object( thread
);
1602 /* select on a handle list */
1603 DECL_HANDLER(select
)
1605 select_op_t select_op
;
1606 data_size_t op_size
;
1607 struct thread_apc
*apc
;
1608 const apc_result_t
*result
= get_req_data();
1610 if (get_req_data_size() < sizeof(*result
) ||
1611 get_req_data_size() - sizeof(*result
) < req
->size
||
1614 set_error( STATUS_INVALID_PARAMETER
);
1618 if (get_req_data_size() - sizeof(*result
) - req
->size
== sizeof(context_t
))
1620 const context_t
*context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1621 if ((current
->context
&& current
->context
->status
!= STATUS_PENDING
) || context
->cpu
!= current
->process
->cpu
)
1623 set_error( STATUS_INVALID_PARAMETER
);
1627 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1628 copy_context( ¤t
->context
->regs
, context
,
1629 context
->flags
& ~(current
->context
->regs
.flags
| get_context_system_regs(current
->process
->cpu
)) );
1630 current
->context
->status
= STATUS_SUCCESS
;
1631 current
->suspend_cookie
= req
->cookie
;
1632 wake_up( ¤t
->context
->obj
, 0 );
1637 set_error( STATUS_INVALID_PARAMETER
);
1641 op_size
= min( req
->size
, sizeof(select_op
) );
1642 memset( &select_op
, 0, sizeof(select_op
) );
1643 memcpy( &select_op
, result
+ 1, op_size
);
1645 /* first store results of previous apc */
1648 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1649 0, &thread_apc_ops
))) return;
1650 apc
->result
= *result
;
1652 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1654 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1655 apc
->caller
->process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1656 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1657 apc
->result
.create_thread
.handle
= handle
;
1658 clear_error(); /* ignore errors from the above calls */
1660 wake_up( &apc
->obj
, 0 );
1661 close_handle( current
->process
, req
->prev_apc
);
1662 release_object( apc
);
1665 select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1667 while (get_error() == STATUS_USER_APC
)
1669 if (!(apc
= thread_dequeue_apc( current
, 0 )))
1671 /* Optimization: ignore APC_NONE calls, they are only used to
1672 * wake up a thread, but since we got here the thread woke up already.
1674 if (apc
->call
.type
!= APC_NONE
&&
1675 (reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1677 reply
->call
= apc
->call
;
1678 release_object( apc
);
1682 wake_up( &apc
->obj
, 0 );
1683 release_object( apc
);
1686 if (get_error() == STATUS_KERNEL_APC
)
1688 apc
= thread_dequeue_apc( current
, 1 );
1689 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1690 reply
->call
= apc
->call
;
1694 wake_up( &apc
->obj
, 0 );
1696 release_object( apc
);
1698 else if (get_error() != STATUS_PENDING
&& get_reply_max_size() == sizeof(context_t
) &&
1699 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1701 if (current
->context
->regs
.flags
)
1703 unsigned int system_flags
= get_context_system_regs(current
->process
->cpu
) &
1704 current
->context
->regs
.flags
;
1705 if (system_flags
) set_thread_context( current
, ¤t
->context
->regs
, system_flags
);
1706 set_reply_data( ¤t
->context
->regs
, sizeof(context_t
) );
1708 release_object( current
->context
);
1709 current
->context
= NULL
;
1713 /* queue an APC for a thread or process */
1714 DECL_HANDLER(queue_apc
)
1716 struct thread
*thread
= NULL
;
1717 struct process
*process
= NULL
;
1718 struct thread_apc
*apc
;
1720 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1722 switch (apc
->call
.type
)
1726 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1728 case APC_VIRTUAL_ALLOC
:
1729 case APC_VIRTUAL_FREE
:
1730 case APC_VIRTUAL_PROTECT
:
1731 case APC_VIRTUAL_FLUSH
:
1732 case APC_VIRTUAL_LOCK
:
1733 case APC_VIRTUAL_UNLOCK
:
1734 case APC_UNMAP_VIEW
:
1735 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1737 case APC_VIRTUAL_QUERY
:
1738 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
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 case APC_BREAK_PROCESS
:
1757 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1759 case APC_DUP_HANDLE
:
1760 process
= get_process_from_handle( req
->handle
, PROCESS_DUP_HANDLE
);
1761 if (process
&& process
!= current
->process
)
1763 /* duplicate the destination process handle into the target process */
1764 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.dup_handle
.dst_process
,
1765 process
, 0, 0, DUPLICATE_SAME_ACCESS
);
1766 if (handle
) apc
->call
.dup_handle
.dst_process
= handle
;
1769 release_object( process
);
1775 set_error( STATUS_INVALID_PARAMETER
);
1781 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_UNSUCCESSFUL
);
1782 release_object( thread
);
1786 reply
->self
= (process
== current
->process
);
1789 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1792 if (queue_apc( process
, NULL
, apc
))
1794 apc
->caller
= (struct thread
*)grab_object( current
);
1795 reply
->handle
= handle
;
1799 close_handle( current
->process
, handle
);
1800 set_error( STATUS_PROCESS_IS_TERMINATING
);
1804 release_object( process
);
1807 release_object( apc
);
1810 /* Get the result of an APC call */
1811 DECL_HANDLER(get_apc_result
)
1813 struct thread_apc
*apc
;
1815 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1816 0, &thread_apc_ops
))) return;
1818 if (apc
->executed
) reply
->result
= apc
->result
;
1819 else set_error( STATUS_PENDING
);
1821 /* close the handle directly to avoid an extra round-trip */
1822 close_handle( current
->process
, req
->handle
);
1823 release_object( apc
);
1826 /* retrieve the current context of a thread */
1827 DECL_HANDLER(get_thread_context
)
1829 struct context
*thread_context
= NULL
;
1830 unsigned int system_flags
;
1831 struct thread
*thread
;
1834 if (get_reply_max_size() < sizeof(context_t
))
1836 set_error( STATUS_INVALID_PARAMETER
);
1840 if ((thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->handle
, 0, &context_ops
)))
1842 close_handle( current
->process
, req
->handle
); /* avoid extra server call */
1843 system_flags
= get_context_system_regs( thread_context
->regs
.cpu
);
1845 else if ((thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
)))
1848 system_flags
= get_context_system_regs( thread
->process
->cpu
);
1849 if (thread
->state
== RUNNING
)
1851 reply
->self
= (thread
== current
);
1852 if (thread
!= current
) stop_thread( thread
);
1853 if (thread
->context
)
1855 /* make sure that system regs are valid in thread context */
1856 if (thread
->unix_tid
!= -1 && (req
->flags
& system_flags
& ~thread
->context
->regs
.flags
))
1857 get_thread_context( thread
, &thread
->context
->regs
, req
->flags
& system_flags
);
1858 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1860 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1862 assert( reply
->self
);
1863 memset( context
, 0, sizeof(context_t
) );
1864 context
->cpu
= thread
->process
->cpu
;
1865 if (req
->flags
& system_flags
)
1867 get_thread_context( thread
, context
, req
->flags
& system_flags
);
1868 context
->flags
|= req
->flags
& system_flags
;
1872 else set_error( STATUS_UNSUCCESSFUL
);
1873 release_object( thread
);
1875 if (get_error() || !thread_context
) return;
1877 set_error( thread_context
->status
);
1878 if (!thread_context
->status
&& (context
= set_reply_data_size( sizeof(context_t
) )))
1880 memset( context
, 0, sizeof(context_t
) );
1881 context
->cpu
= thread_context
->regs
.cpu
;
1882 copy_context( context
, &thread_context
->regs
, req
->flags
);
1883 context
->flags
= req
->flags
;
1885 else if (thread_context
->status
== STATUS_PENDING
)
1887 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1890 release_object( thread_context
);
1893 /* set the current context of a thread */
1894 DECL_HANDLER(set_thread_context
)
1896 struct thread
*thread
;
1897 const context_t
*context
= get_req_data();
1899 if (get_req_data_size() < sizeof(context_t
))
1901 set_error( STATUS_INVALID_PARAMETER
);
1904 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1905 reply
->self
= (thread
== current
);
1907 if (thread
->state
== TERMINATED
) set_error( STATUS_UNSUCCESSFUL
);
1908 else if (context
->cpu
== thread
->process
->cpu
)
1910 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1912 if (thread
!= current
) stop_thread( thread
);
1913 else if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1914 if (thread
->context
&& !get_error())
1916 copy_context( &thread
->context
->regs
, context
, context
->flags
);
1917 thread
->context
->regs
.flags
|= context
->flags
;
1920 else if (context
->cpu
== CPU_x86_64
&& thread
->process
->cpu
== CPU_x86
)
1922 /* convert the WoW64 context */
1923 unsigned int system_flags
= get_context_system_regs( context
->cpu
) & context
->flags
;
1926 set_thread_context( thread
, context
, system_flags
);
1927 if (thread
->context
&& !get_error())
1929 thread
->context
->regs
.debug
.i386_regs
.dr0
= context
->debug
.x86_64_regs
.dr0
;
1930 thread
->context
->regs
.debug
.i386_regs
.dr1
= context
->debug
.x86_64_regs
.dr1
;
1931 thread
->context
->regs
.debug
.i386_regs
.dr2
= context
->debug
.x86_64_regs
.dr2
;
1932 thread
->context
->regs
.debug
.i386_regs
.dr3
= context
->debug
.x86_64_regs
.dr3
;
1933 thread
->context
->regs
.debug
.i386_regs
.dr6
= context
->debug
.x86_64_regs
.dr6
;
1934 thread
->context
->regs
.debug
.i386_regs
.dr7
= context
->debug
.x86_64_regs
.dr7
;
1938 else set_error( STATUS_INVALID_PARAMETER
);
1940 release_object( thread
);
1943 /* fetch a selector entry for a thread */
1944 DECL_HANDLER(get_selector_entry
)
1946 struct thread
*thread
;
1947 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1949 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1950 release_object( thread
);