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
->debug_ctx
= NULL
;
225 thread
->system_regs
= 0;
226 thread
->queue
= NULL
;
229 thread
->req_data
= NULL
;
230 thread
->req_toread
= 0;
231 thread
->reply_data
= NULL
;
232 thread
->reply_towrite
= 0;
233 thread
->request_fd
= NULL
;
234 thread
->reply_fd
= NULL
;
235 thread
->wait_fd
= NULL
;
236 thread
->state
= RUNNING
;
237 thread
->exit_code
= 0;
238 thread
->priority
= 0;
240 thread
->dbg_hidden
= 0;
241 thread
->desktop_users
= 0;
242 thread
->token
= NULL
;
244 thread
->desc_len
= 0;
246 thread
->creation_time
= current_time
;
247 thread
->exit_time
= 0;
249 list_init( &thread
->mutex_list
);
250 list_init( &thread
->system_apc
);
251 list_init( &thread
->user_apc
);
252 list_init( &thread
->kernel_object
);
254 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
255 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
258 /* check if address looks valid for a client-side data structure (TEB etc.) */
259 static inline int is_valid_address( client_ptr_t addr
)
261 return addr
&& !(addr
% sizeof(int));
265 /* dump a context on stdout for debugging purposes */
266 static void dump_context( struct object
*obj
, int verbose
)
268 struct context
*context
= (struct context
*)obj
;
269 assert( obj
->ops
== &context_ops
);
271 fprintf( stderr
, "context flags=%x\n", context
->regs
.flags
);
275 static int context_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
277 struct context
*context
= (struct context
*)obj
;
278 return context
->status
!= STATUS_PENDING
;
282 static struct context
*create_thread_context( struct thread
*thread
)
284 struct context
*context
;
285 if (!(context
= alloc_object( &context_ops
))) return NULL
;
286 context
->status
= STATUS_PENDING
;
287 memset( &context
->regs
, 0, sizeof(context
->regs
) );
288 context
->regs
.cpu
= thread
->process
->cpu
;
293 /* create a new thread */
294 struct thread
*create_thread( int fd
, struct process
*process
, const struct security_descriptor
*sd
)
296 struct thread
*thread
;
301 if (pipe( request_pipe
) == -1)
306 if (send_client_fd( process
, request_pipe
[1], SERVER_PROTOCOL_VERSION
) == -1)
308 close( request_pipe
[0] );
309 close( request_pipe
[1] );
312 close( request_pipe
[1] );
313 fd
= request_pipe
[0];
316 if (process
->is_terminating
)
319 set_error( STATUS_PROCESS_IS_TERMINATING
);
323 if (!(thread
= alloc_object( &thread_ops
)))
329 init_thread_structure( thread
);
331 thread
->process
= (struct process
*)grab_object( process
);
332 thread
->desktop
= process
->desktop
;
333 thread
->affinity
= process
->affinity
;
334 if (!current
) current
= thread
;
336 list_add_tail( &thread_list
, &thread
->entry
);
338 if (sd
&& !set_sd_defaults_from_token( &thread
->obj
, sd
,
339 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
340 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
,
344 release_object( thread
);
347 if (!(thread
->id
= alloc_ptid( thread
)))
350 release_object( thread
);
353 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
, 0 )))
355 release_object( thread
);
359 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
360 add_process_thread( thread
->process
, thread
);
364 /* handle a client event */
365 static void thread_poll_event( struct fd
*fd
, int event
)
367 struct thread
*thread
= get_fd_user( fd
);
368 assert( thread
->obj
.ops
== &thread_ops
);
370 grab_object( thread
);
371 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
372 else if (event
& POLLIN
) read_request( thread
);
373 else if (event
& POLLOUT
) write_reply( thread
);
374 release_object( thread
);
377 static struct list
*thread_get_kernel_obj_list( struct object
*obj
)
379 struct thread
*thread
= (struct thread
*)obj
;
380 return &thread
->kernel_object
;
383 /* cleanup everything that is no longer needed by a dead thread */
384 /* used by destroy_thread and kill_thread */
385 static void cleanup_thread( struct thread
*thread
)
391 thread
->context
->status
= STATUS_ACCESS_DENIED
;
392 wake_up( &thread
->context
->obj
, 0 );
393 release_object( thread
->context
);
394 thread
->context
= NULL
;
396 clear_apc_queue( &thread
->system_apc
);
397 clear_apc_queue( &thread
->user_apc
);
398 free( thread
->req_data
);
399 free( thread
->reply_data
);
400 if (thread
->request_fd
) release_object( thread
->request_fd
);
401 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
402 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
403 cleanup_clipboard_thread(thread
);
404 destroy_thread_windows( thread
);
405 free_msg_queue( thread
);
406 close_thread_desktop( thread
);
407 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
409 if (thread
->inflight
[i
].client
!= -1)
411 close( thread
->inflight
[i
].server
);
412 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
415 free( thread
->desc
);
416 thread
->req_data
= NULL
;
417 thread
->reply_data
= NULL
;
418 thread
->request_fd
= NULL
;
419 thread
->reply_fd
= NULL
;
420 thread
->wait_fd
= NULL
;
423 thread
->desc_len
= 0;
426 /* destroy a thread when its refcount is 0 */
427 static void destroy_thread( struct object
*obj
)
429 struct thread
*thread
= (struct thread
*)obj
;
430 assert( obj
->ops
== &thread_ops
);
432 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
433 list_remove( &thread
->entry
);
434 cleanup_thread( thread
);
435 release_object( thread
->process
);
436 if (thread
->id
) free_ptid( thread
->id
);
437 if (thread
->token
) release_object( thread
->token
);
440 /* dump a thread on stdout for debugging purposes */
441 static void dump_thread( struct object
*obj
, int verbose
)
443 struct thread
*thread
= (struct thread
*)obj
;
444 assert( obj
->ops
== &thread_ops
);
446 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
447 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->state
);
450 static struct object_type
*thread_get_type( struct object
*obj
)
452 static const WCHAR name
[] = {'T','h','r','e','a','d'};
453 static const struct unicode_str str
= { name
, sizeof(name
) };
454 return get_object_type( &str
);
457 static int thread_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
459 struct thread
*mythread
= (struct thread
*)obj
;
460 return (mythread
->state
== TERMINATED
);
463 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
465 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
;
466 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
|
467 THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
;
468 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| THREAD_QUERY_LIMITED_INFORMATION
;
469 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
471 if (access
& THREAD_QUERY_INFORMATION
) access
|= THREAD_QUERY_LIMITED_INFORMATION
;
472 if (access
& THREAD_SET_INFORMATION
) access
|= THREAD_SET_LIMITED_INFORMATION
;
474 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
477 static void dump_thread_apc( struct object
*obj
, int verbose
)
479 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
480 assert( obj
->ops
== &thread_apc_ops
);
482 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
485 static int thread_apc_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
487 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
488 return apc
->executed
;
491 static void thread_apc_destroy( struct object
*obj
)
493 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
494 if (apc
->caller
) release_object( apc
->caller
);
495 if (apc
->owner
) release_object( apc
->owner
);
498 /* queue an async procedure call */
499 static struct thread_apc
*create_apc( struct object
*owner
, const apc_call_t
*call_data
)
501 struct thread_apc
*apc
;
503 if ((apc
= alloc_object( &thread_apc_ops
)))
505 apc
->call
= *call_data
;
509 apc
->result
.type
= APC_NONE
;
510 if (owner
) grab_object( owner
);
515 /* get a thread pointer from a thread id (and increment the refcount) */
516 struct thread
*get_thread_from_id( thread_id_t id
)
518 struct object
*obj
= get_ptid_entry( id
);
520 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
521 set_error( STATUS_INVALID_CID
);
525 /* get a thread from a handle (and increment the refcount) */
526 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
528 return (struct thread
*)get_handle_obj( current
->process
, handle
,
529 access
, &thread_ops
);
532 /* find a thread from a Unix tid */
533 struct thread
*get_thread_from_tid( int tid
)
535 struct thread
*thread
;
537 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
539 if (thread
->unix_tid
== tid
) return thread
;
544 /* find a thread from a Unix pid */
545 struct thread
*get_thread_from_pid( int pid
)
547 struct thread
*thread
;
549 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
551 if (thread
->unix_pid
== pid
) return thread
;
556 int set_thread_affinity( struct thread
*thread
, affinity_t affinity
)
559 #ifdef HAVE_SCHED_SETAFFINITY
560 if (thread
->unix_tid
!= -1)
567 for (i
= 0, mask
= 1; mask
; i
++, mask
<<= 1)
568 if (affinity
& mask
) CPU_SET( i
, &set
);
570 ret
= sched_setaffinity( thread
->unix_tid
, sizeof(set
), &set
);
573 if (!ret
) thread
->affinity
= affinity
;
577 affinity_t
get_thread_affinity( struct thread
*thread
)
580 #ifdef HAVE_SCHED_SETAFFINITY
581 if (thread
->unix_tid
!= -1)
586 if (!sched_getaffinity( thread
->unix_tid
, sizeof(set
), &set
))
587 for (i
= 0; i
< 8 * sizeof(mask
); i
++)
588 if (CPU_ISSET( i
, &set
)) mask
|= (affinity_t
)1 << i
;
591 if (!mask
) mask
= ~(affinity_t
)0;
595 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
596 #define THREAD_PRIORITY_REALTIME_LOWEST -7
598 /* set all information about a thread */
599 static void set_thread_info( struct thread
*thread
,
600 const struct set_thread_info_request
*req
)
602 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
604 int max
= THREAD_PRIORITY_HIGHEST
;
605 int min
= THREAD_PRIORITY_LOWEST
;
606 if (thread
->process
->priority
== PROCESS_PRIOCLASS_REALTIME
)
608 max
= THREAD_PRIORITY_REALTIME_HIGHEST
;
609 min
= THREAD_PRIORITY_REALTIME_LOWEST
;
611 if ((req
->priority
>= min
&& req
->priority
<= max
) ||
612 req
->priority
== THREAD_PRIORITY_IDLE
||
613 req
->priority
== THREAD_PRIORITY_TIME_CRITICAL
)
614 thread
->priority
= req
->priority
;
616 set_error( STATUS_INVALID_PARAMETER
);
618 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
620 if ((req
->affinity
& thread
->process
->affinity
) != req
->affinity
)
621 set_error( STATUS_INVALID_PARAMETER
);
622 else if (thread
->state
== TERMINATED
)
623 set_error( STATUS_THREAD_IS_TERMINATING
);
624 else if (set_thread_affinity( thread
, req
->affinity
))
627 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
628 security_set_thread_token( thread
, req
->token
);
629 if (req
->mask
& SET_THREAD_INFO_ENTRYPOINT
)
630 thread
->entry_point
= req
->entry_point
;
631 if (req
->mask
& SET_THREAD_INFO_DBG_HIDDEN
)
632 thread
->dbg_hidden
= 1;
633 if (req
->mask
& SET_THREAD_INFO_DESCRIPTION
)
636 data_size_t desc_len
= get_req_data_size();
640 if ((desc
= mem_alloc( desc_len
)))
642 memcpy( desc
, get_req_data(), desc_len
);
643 free( thread
->desc
);
645 thread
->desc_len
= desc_len
;
650 free( thread
->desc
);
652 thread
->desc_len
= 0;
657 /* stop a thread (at the Unix level) */
658 void stop_thread( struct thread
*thread
)
660 if (thread
->context
) return; /* already suspended, no need for a signal */
661 if (!(thread
->context
= create_thread_context( thread
))) return;
662 /* can't stop a thread while initialisation is in progress */
663 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
666 /* suspend a thread */
667 int suspend_thread( struct thread
*thread
)
669 int old_count
= thread
->suspend
;
670 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
672 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
674 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
678 /* resume a thread */
679 int resume_thread( struct thread
*thread
)
681 int old_count
= thread
->suspend
;
682 if (thread
->suspend
> 0)
684 if (!(--thread
->suspend
)) resume_delayed_debug_events( thread
);
685 if (!(thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
690 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
691 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
695 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
699 /* remove a thread from an object wait queue */
700 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
702 list_remove( &entry
->entry
);
703 release_object( obj
);
706 struct thread
*get_wait_queue_thread( struct wait_queue_entry
*entry
)
708 return entry
->wait
->thread
;
711 enum select_op
get_wait_queue_select_op( struct wait_queue_entry
*entry
)
713 return entry
->wait
->select
;
716 client_ptr_t
get_wait_queue_key( struct wait_queue_entry
*entry
)
718 return entry
->wait
->key
;
721 void make_wait_abandoned( struct wait_queue_entry
*entry
)
723 entry
->wait
->abandoned
= 1;
727 static unsigned int end_wait( struct thread
*thread
, unsigned int status
)
729 struct thread_wait
*wait
= thread
->wait
;
730 struct wait_queue_entry
*entry
;
734 thread
->wait
= wait
->next
;
736 if (status
< wait
->count
) /* wait satisfied, tell it to the objects */
738 if (wait
->select
== SELECT_WAIT_ALL
)
740 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
741 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
745 entry
= wait
->queues
+ status
;
746 entry
->obj
->ops
->satisfied( entry
->obj
, entry
);
748 if (wait
->abandoned
) status
+= STATUS_ABANDONED_WAIT_0
;
750 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
751 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
752 if (wait
->user
) remove_timeout_user( wait
->user
);
757 /* build the thread wait structure */
758 static int wait_on( const select_op_t
*select_op
, unsigned int count
, struct object
*objects
[],
759 int flags
, abstime_t when
)
761 struct thread_wait
*wait
;
762 struct wait_queue_entry
*entry
;
765 if (!(wait
= mem_alloc( FIELD_OFFSET(struct thread_wait
, queues
[count
]) ))) return 0;
766 wait
->next
= current
->wait
;
767 wait
->thread
= current
;
770 wait
->select
= select_op
->op
;
775 current
->wait
= wait
;
777 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
779 struct object
*obj
= objects
[i
];
781 if (!obj
->ops
->add_queue( obj
, entry
))
784 end_wait( current
, get_error() );
791 static int wait_on_handles( const select_op_t
*select_op
, unsigned int count
, const obj_handle_t
*handles
,
792 int flags
, abstime_t when
)
794 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
798 assert( count
<= MAXIMUM_WAIT_OBJECTS
);
800 for (i
= 0; i
< count
; i
++)
801 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
804 if (i
== count
) ret
= wait_on( select_op
, count
, objects
, flags
, when
);
806 while (i
> 0) release_object( objects
[--i
] );
810 /* check if the thread waiting condition is satisfied */
811 static int check_wait( struct thread
*thread
)
814 struct thread_wait
*wait
= thread
->wait
;
815 struct wait_queue_entry
*entry
;
819 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty( &thread
->system_apc
))
820 return STATUS_KERNEL_APC
;
822 /* Suspended threads may not acquire locks, but they can run system APCs */
823 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
825 if (wait
->select
== SELECT_WAIT_ALL
)
828 /* Note: we must check them all anyway, as some objects may
829 * want to do something when signaled, even if others are not */
830 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
831 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, entry
);
832 if (!not_ok
) return STATUS_WAIT_0
;
836 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
837 if (entry
->obj
->ops
->signaled( entry
->obj
, entry
)) return i
;
840 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
841 if (wait
->when
>= 0 && wait
->when
<= current_time
) return STATUS_TIMEOUT
;
842 if (wait
->when
< 0 && -wait
->when
<= monotonic_time
) return STATUS_TIMEOUT
;
846 /* send the wakeup signal to a thread */
847 static int send_thread_wakeup( struct thread
*thread
, client_ptr_t cookie
, int signaled
)
849 struct wake_up_reply reply
;
852 /* check if we're waking current suspend wait */
853 if (thread
->context
&& thread
->suspend_cookie
== cookie
854 && signaled
!= STATUS_KERNEL_APC
&& signaled
!= STATUS_USER_APC
)
856 if (!thread
->context
->regs
.flags
)
858 release_object( thread
->context
);
859 thread
->context
= NULL
;
861 else signaled
= STATUS_KERNEL_APC
; /* signal a fake APC so that client calls select to get a new context */
864 memset( &reply
, 0, sizeof(reply
) );
865 reply
.cookie
= cookie
;
866 reply
.signaled
= signaled
;
867 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
870 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
871 else if (errno
== EPIPE
)
872 kill_thread( thread
, 0 ); /* normal death */
874 fatal_protocol_error( thread
, "write: %s\n", strerror( errno
));
878 /* attempt to wake up a thread */
879 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
880 int wake_thread( struct thread
*thread
)
885 for (count
= 0; thread
->wait
; count
++)
887 if ((signaled
= check_wait( thread
)) == -1) break;
889 cookie
= thread
->wait
->cookie
;
890 signaled
= end_wait( thread
, signaled
);
891 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
892 if (cookie
&& send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
894 if (!count
) count
= -1;
901 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
902 int wake_thread_queue_entry( struct wait_queue_entry
*entry
)
904 struct thread_wait
*wait
= entry
->wait
;
905 struct thread
*thread
= wait
->thread
;
909 if (thread
->wait
!= wait
) return 0; /* not the current wait */
910 if (thread
->process
->suspend
+ thread
->suspend
> 0) return 0; /* cannot acquire locks */
912 assert( wait
->select
!= SELECT_WAIT_ALL
);
914 cookie
= wait
->cookie
;
915 signaled
= end_wait( thread
, entry
- wait
->queues
);
916 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d\n", thread
->id
, signaled
);
918 if (!cookie
|| send_thread_wakeup( thread
, cookie
, signaled
) != -1)
919 wake_thread( thread
); /* check other waits too */
924 /* thread wait timeout */
925 static void thread_timeout( void *ptr
)
927 struct thread_wait
*wait
= ptr
;
928 struct thread
*thread
= wait
->thread
;
929 client_ptr_t cookie
= wait
->cookie
;
932 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
933 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
935 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=TIMEOUT\n", thread
->id
);
936 end_wait( thread
, STATUS_TIMEOUT
);
939 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
940 /* check if other objects have become signaled in the meantime */
941 wake_thread( thread
);
944 /* try signaling an event flag, a semaphore or a mutex */
945 static int signal_object( obj_handle_t handle
)
950 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
953 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
954 release_object( obj
);
959 /* select on a list of handles */
960 static void select_on( const select_op_t
*select_op
, data_size_t op_size
, client_ptr_t cookie
,
961 int flags
, abstime_t when
)
965 struct object
*object
;
967 switch (select_op
->op
)
970 if (!wait_on( select_op
, 0, NULL
, flags
, when
)) return;
974 case SELECT_WAIT_ALL
:
975 count
= (op_size
- offsetof( select_op_t
, wait
.handles
)) / sizeof(select_op
->wait
.handles
[0]);
976 if (op_size
< offsetof( select_op_t
, wait
.handles
) || count
> MAXIMUM_WAIT_OBJECTS
)
978 set_error( STATUS_INVALID_PARAMETER
);
981 if (!wait_on_handles( select_op
, count
, select_op
->wait
.handles
, flags
, when
))
985 case SELECT_SIGNAL_AND_WAIT
:
986 if (!wait_on_handles( select_op
, 1, &select_op
->signal_and_wait
.wait
, flags
, when
))
988 if (select_op
->signal_and_wait
.signal
)
990 if (!signal_object( select_op
->signal_and_wait
.signal
))
992 end_wait( current
, get_error() );
995 /* check if we woke ourselves up */
996 if (!current
->wait
) return;
1000 case SELECT_KEYED_EVENT_WAIT
:
1001 case SELECT_KEYED_EVENT_RELEASE
:
1002 object
= (struct object
*)get_keyed_event_obj( current
->process
, select_op
->keyed_event
.handle
,
1003 select_op
->op
== SELECT_KEYED_EVENT_WAIT
? KEYEDEVENT_WAIT
: KEYEDEVENT_WAKE
);
1004 if (!object
) return;
1005 ret
= wait_on( select_op
, 1, &object
, flags
, when
);
1006 release_object( object
);
1008 current
->wait
->key
= select_op
->keyed_event
.key
;
1012 set_error( STATUS_INVALID_PARAMETER
);
1016 if ((ret
= check_wait( current
)) != -1)
1018 /* condition is already satisfied */
1019 set_error( end_wait( current
, ret
));
1023 /* now we need to wait */
1024 if (current
->wait
->when
!= TIMEOUT_INFINITE
)
1026 if (!(current
->wait
->user
= add_timeout_user( abstime_to_timeout(current
->wait
->when
),
1027 thread_timeout
, current
->wait
)))
1029 end_wait( current
, get_error() );
1033 current
->wait
->cookie
= cookie
;
1034 set_error( STATUS_PENDING
);
1038 /* attempt to wake threads sleeping on the object wait queue */
1039 void wake_up( struct object
*obj
, int max
)
1044 LIST_FOR_EACH( ptr
, &obj
->wait_queue
)
1046 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
1047 if (!(ret
= wake_thread( get_wait_queue_thread( entry
)))) continue;
1048 if (ret
> 0 && max
&& !--max
) break;
1049 /* restart at the head of the list since a wake up can change the object wait queue */
1050 ptr
= &obj
->wait_queue
;
1054 /* return the apc queue to use for a given apc type */
1055 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
1062 return &thread
->user_apc
;
1064 return &thread
->system_apc
;
1068 /* check if thread is currently waiting for a (system) apc */
1069 static inline int is_in_apc_wait( struct thread
*thread
)
1071 return (thread
->process
->suspend
|| thread
->suspend
||
1072 (thread
->wait
&& (thread
->wait
->flags
& SELECT_INTERRUPTIBLE
)));
1075 /* queue an existing APC to a given thread */
1076 static int queue_apc( struct process
*process
, struct thread
*thread
, struct thread_apc
*apc
)
1080 if (thread
&& thread
->state
== TERMINATED
&& process
)
1083 if (!thread
) /* find a suitable thread inside the process */
1085 struct thread
*candidate
;
1087 /* first try to find a waiting thread */
1088 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1090 if (candidate
->state
== TERMINATED
) continue;
1091 if (is_in_apc_wait( candidate
))
1099 /* then use the first one that accepts a signal */
1100 LIST_FOR_EACH_ENTRY( candidate
, &process
->thread_list
, struct thread
, proc_entry
)
1102 if (send_thread_signal( candidate
, SIGUSR1
))
1109 if (!thread
) return 0; /* nothing found */
1110 queue
= get_apc_queue( thread
, apc
->call
.type
);
1114 if (thread
->state
== TERMINATED
) return 0;
1115 queue
= get_apc_queue( thread
, apc
->call
.type
);
1116 /* send signal for system APCs if needed */
1117 if (queue
== &thread
->system_apc
&& list_empty( queue
) && !is_in_apc_wait( thread
))
1119 if (!send_thread_signal( thread
, SIGUSR1
)) return 0;
1121 /* cancel a possible previous APC with the same owner */
1122 if (apc
->owner
) thread_cancel_apc( thread
, apc
->owner
, apc
->call
.type
);
1126 list_add_tail( queue
, &apc
->entry
);
1127 if (!list_prev( queue
, &apc
->entry
)) /* first one */
1128 wake_thread( thread
);
1133 /* queue an async procedure call */
1134 int thread_queue_apc( struct process
*process
, struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
1136 struct thread_apc
*apc
;
1139 if ((apc
= create_apc( owner
, call_data
)))
1141 ret
= queue_apc( process
, thread
, apc
);
1142 release_object( apc
);
1147 /* cancel the async procedure call owned by a specific object */
1148 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
1150 struct thread_apc
*apc
;
1151 struct list
*queue
= get_apc_queue( thread
, type
);
1153 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
1155 if (apc
->owner
!= owner
) continue;
1156 list_remove( &apc
->entry
);
1158 wake_up( &apc
->obj
, 0 );
1159 release_object( apc
);
1164 /* remove the head apc from the queue; the returned object must be released by the caller */
1165 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system
)
1167 struct thread_apc
*apc
= NULL
;
1168 struct list
*ptr
= list_head( system
? &thread
->system_apc
: &thread
->user_apc
);
1172 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1178 /* clear an APC queue, cancelling all the APCs on it */
1179 static void clear_apc_queue( struct list
*queue
)
1183 while ((ptr
= list_head( queue
)))
1185 struct thread_apc
*apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
1186 list_remove( &apc
->entry
);
1188 wake_up( &apc
->obj
, 0 );
1189 release_object( apc
);
1193 /* add an fd to the inflight list */
1194 /* return list index, or -1 on error */
1195 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
1199 if (server
== -1) return -1;
1206 /* first check if we already have an entry for this fd */
1207 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1208 if (thread
->inflight
[i
].client
== client
)
1210 close( thread
->inflight
[i
].server
);
1211 thread
->inflight
[i
].server
= server
;
1215 /* now find a free spot to store it */
1216 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1217 if (thread
->inflight
[i
].client
== -1)
1219 thread
->inflight
[i
].client
= client
;
1220 thread
->inflight
[i
].server
= server
;
1228 /* get an inflight fd and purge it from the list */
1229 /* the fd must be closed when no longer used */
1230 int thread_get_inflight_fd( struct thread
*thread
, int client
)
1234 if (client
== -1) return -1;
1238 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
1240 if (thread
->inflight
[i
].client
== client
)
1242 ret
= thread
->inflight
[i
].server
;
1243 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
1247 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
1251 /* kill a thread on the spot */
1252 void kill_thread( struct thread
*thread
, int violent_death
)
1254 if (thread
->state
== TERMINATED
) return; /* already killed */
1255 thread
->state
= TERMINATED
;
1256 thread
->exit_time
= current_time
;
1257 if (current
== thread
) current
= NULL
;
1259 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
1260 thread
->id
, thread
->exit_code
);
1263 while (thread
->wait
) end_wait( thread
, STATUS_THREAD_IS_TERMINATING
);
1264 send_thread_wakeup( thread
, 0, thread
->exit_code
);
1265 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1268 kill_console_processes( thread
, 0 );
1269 debug_exit_thread( thread
);
1270 abandon_mutexes( thread
);
1271 wake_up( &thread
->obj
, 0 );
1272 if (violent_death
) send_thread_signal( thread
, SIGQUIT
);
1273 cleanup_thread( thread
);
1274 remove_process_thread( thread
->process
, thread
);
1275 release_object( thread
);
1278 /* copy parts of a context structure */
1279 static void copy_context( context_t
*to
, const context_t
*from
, unsigned int flags
)
1281 assert( to
->cpu
== from
->cpu
);
1282 if (flags
& SERVER_CTX_CONTROL
) to
->ctl
= from
->ctl
;
1283 if (flags
& SERVER_CTX_INTEGER
) to
->integer
= from
->integer
;
1284 if (flags
& SERVER_CTX_SEGMENTS
) to
->seg
= from
->seg
;
1285 if (flags
& SERVER_CTX_FLOATING_POINT
) to
->fp
= from
->fp
;
1286 if (flags
& SERVER_CTX_DEBUG_REGISTERS
) to
->debug
= from
->debug
;
1287 if (flags
& SERVER_CTX_EXTENDED_REGISTERS
) to
->ext
= from
->ext
;
1290 /* return the context flags that correspond to system regs */
1291 /* (system regs are the ones we can't access on the client side) */
1292 static unsigned int get_context_system_regs( enum cpu_type cpu
)
1296 case CPU_x86
: return SERVER_CTX_DEBUG_REGISTERS
;
1297 case CPU_x86_64
: return SERVER_CTX_DEBUG_REGISTERS
;
1298 case CPU_POWERPC
: return 0;
1299 case CPU_ARM
: return SERVER_CTX_DEBUG_REGISTERS
;
1300 case CPU_ARM64
: return SERVER_CTX_DEBUG_REGISTERS
;
1305 /* gets the current impersonation token */
1306 struct token
*thread_get_impersonation_token( struct thread
*thread
)
1309 return thread
->token
;
1311 return thread
->process
->token
;
1314 /* check if a cpu type can be supported on this server */
1315 int is_cpu_supported( enum cpu_type cpu
)
1317 unsigned int prefix_cpu_mask
= get_prefix_cpu_mask();
1319 if (supported_cpus
& prefix_cpu_mask
& CPU_FLAG(cpu
)) return 1;
1320 if (!(supported_cpus
& prefix_cpu_mask
))
1321 set_error( STATUS_NOT_SUPPORTED
);
1322 else if (supported_cpus
& CPU_FLAG(cpu
))
1323 set_error( STATUS_INVALID_IMAGE_WIN_64
); /* server supports it but not the prefix */
1325 set_error( STATUS_INVALID_IMAGE_FORMAT
);
1329 /* return the cpu mask for supported cpus */
1330 unsigned int get_supported_cpu_mask(void)
1332 return supported_cpus
& get_prefix_cpu_mask();
1335 /* create a new thread */
1336 DECL_HANDLER(new_thread
)
1338 struct thread
*thread
;
1339 struct process
*process
;
1340 struct unicode_str name
;
1341 const struct security_descriptor
*sd
;
1342 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1343 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
1345 if (!(process
= get_process_from_handle( req
->process
, PROCESS_CREATE_THREAD
)))
1347 if (request_fd
!= -1) close( request_fd
);
1351 if (process
!= current
->process
)
1353 if (request_fd
!= -1) /* can't create a request fd in a different process */
1355 close( request_fd
);
1356 set_error( STATUS_INVALID_PARAMETER
);
1359 if (process
->running_threads
) /* only the initial thread can be created in another process */
1361 set_error( STATUS_ACCESS_DENIED
);
1365 else if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1367 if (request_fd
!= -1) close( request_fd
);
1368 set_error( STATUS_INVALID_HANDLE
);
1372 if ((thread
= create_thread( request_fd
, process
, sd
)))
1374 thread
->system_regs
= current
->system_regs
;
1375 if (req
->suspend
) thread
->suspend
++;
1376 reply
->tid
= get_thread_id( thread
);
1377 if ((reply
->handle
= alloc_handle_no_access_check( current
->process
, thread
,
1378 req
->access
, objattr
->attributes
)))
1380 /* thread object will be released when the thread gets killed */
1383 kill_thread( thread
, 1 );
1386 release_object( process
);
1389 /* initialize a new thread */
1390 DECL_HANDLER(init_thread
)
1392 struct process
*process
= current
->process
;
1393 int wait_fd
, reply_fd
;
1395 if ((reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
)) == -1)
1397 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1400 if ((wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
)) == -1)
1402 set_error( STATUS_TOO_MANY_OPENED_FILES
);
1406 if (current
->reply_fd
) /* already initialised */
1408 set_error( STATUS_INVALID_PARAMETER
);
1412 if (fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
1414 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
, 0 );
1415 current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
, 0 );
1416 if (!current
->reply_fd
|| !current
->wait_fd
) return;
1418 if (!is_valid_address(req
->teb
))
1420 set_error( STATUS_INVALID_PARAMETER
);
1424 current
->unix_pid
= req
->unix_pid
;
1425 current
->unix_tid
= req
->unix_tid
;
1426 current
->teb
= req
->teb
;
1427 current
->entry_point
= process
->peb
? req
->entry
: 0;
1429 if (!process
->peb
) /* first thread, initialize the process too */
1431 if (!is_cpu_supported( req
->cpu
)) return;
1432 process
->unix_pid
= current
->unix_pid
;
1433 process
->peb
= req
->entry
;
1434 process
->cpu
= req
->cpu
;
1435 reply
->info_size
= init_process( current
);
1436 if (!process
->parent_id
)
1437 process
->affinity
= current
->affinity
= get_thread_affinity( current
);
1439 set_thread_affinity( current
, current
->affinity
);
1443 if (req
->cpu
!= process
->cpu
)
1445 set_error( STATUS_INVALID_PARAMETER
);
1448 if (process
->unix_pid
!= current
->unix_pid
)
1449 process
->unix_pid
= -1; /* can happen with linuxthreads */
1450 init_thread_context( current
);
1451 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, &req
->entry
);
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
->version
= SERVER_PROTOCOL_VERSION
;
1459 reply
->server_start
= server_start_time
;
1460 reply
->all_cpus
= supported_cpus
& get_prefix_cpu_mask();
1461 reply
->suspend
= (current
->suspend
|| process
->suspend
|| current
->context
!= NULL
);
1465 if (reply_fd
!= -1) close( reply_fd
);
1466 if (wait_fd
!= -1) close( wait_fd
);
1469 /* terminate a thread */
1470 DECL_HANDLER(terminate_thread
)
1472 struct thread
*thread
;
1474 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
1476 thread
->exit_code
= req
->exit_code
;
1477 if (thread
!= current
) kill_thread( thread
, 1 );
1478 else reply
->self
= 1;
1479 release_object( thread
);
1483 /* open a handle to a thread */
1484 DECL_HANDLER(open_thread
)
1486 struct thread
*thread
= get_thread_from_id( req
->tid
);
1491 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
1492 release_object( thread
);
1496 /* fetch information about a thread */
1497 DECL_HANDLER(get_thread_info
)
1499 struct thread
*thread
;
1500 unsigned int access
= req
->access
& (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
);
1502 if (!access
) access
= THREAD_QUERY_LIMITED_INFORMATION
;
1503 thread
= get_thread_from_handle( req
->handle
, access
);
1506 reply
->pid
= get_process_id( thread
->process
);
1507 reply
->tid
= get_thread_id( thread
);
1508 reply
->teb
= thread
->teb
;
1509 reply
->entry_point
= thread
->entry_point
;
1510 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
1511 reply
->priority
= thread
->priority
;
1512 reply
->affinity
= thread
->affinity
;
1513 reply
->last
= thread
->process
->running_threads
== 1;
1514 reply
->suspend_count
= thread
->suspend
;
1515 reply
->dbg_hidden
= thread
->dbg_hidden
;
1516 reply
->desc_len
= thread
->desc_len
;
1518 if (thread
->desc
&& get_reply_max_size())
1520 if (thread
->desc_len
<= get_reply_max_size())
1521 set_reply_data( thread
->desc
, thread
->desc_len
);
1523 set_error( STATUS_BUFFER_TOO_SMALL
);
1526 release_object( thread
);
1530 /* fetch information about thread times */
1531 DECL_HANDLER(get_thread_times
)
1533 struct thread
*thread
;
1535 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_LIMITED_INFORMATION
)))
1537 reply
->creation_time
= thread
->creation_time
;
1538 reply
->exit_time
= thread
->exit_time
;
1539 reply
->unix_pid
= thread
->unix_pid
;
1540 reply
->unix_tid
= thread
->unix_tid
;
1542 release_object( thread
);
1546 /* set information about a thread */
1547 DECL_HANDLER(set_thread_info
)
1549 struct thread
*thread
;
1551 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1553 set_thread_info( thread
, req
);
1554 release_object( thread
);
1558 /* suspend a thread */
1559 DECL_HANDLER(suspend_thread
)
1561 struct thread
*thread
;
1563 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1565 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1566 else reply
->count
= suspend_thread( thread
);
1567 release_object( thread
);
1571 /* resume a thread */
1572 DECL_HANDLER(resume_thread
)
1574 struct thread
*thread
;
1576 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1578 reply
->count
= resume_thread( thread
);
1579 release_object( thread
);
1583 /* select on a handle list */
1584 DECL_HANDLER(select
)
1586 select_op_t select_op
;
1587 data_size_t op_size
;
1588 struct thread_apc
*apc
;
1589 const apc_result_t
*result
= get_req_data();
1591 if (get_req_data_size() < sizeof(*result
) ||
1592 get_req_data_size() - sizeof(*result
) < req
->size
||
1595 set_error( STATUS_INVALID_PARAMETER
);
1599 if (get_req_data_size() - sizeof(*result
) - req
->size
== sizeof(context_t
))
1601 const context_t
*context
= (const context_t
*)((const char *)(result
+ 1) + req
->size
);
1602 if ((current
->context
&& current
->context
->status
!= STATUS_PENDING
) || context
->cpu
!= current
->process
->cpu
)
1604 set_error( STATUS_INVALID_PARAMETER
);
1608 if (!current
->context
&& !(current
->context
= create_thread_context( current
))) return;
1609 copy_context( ¤t
->context
->regs
, context
,
1610 context
->flags
& ~(current
->context
->regs
.flags
| get_context_system_regs(current
->process
->cpu
)) );
1611 current
->context
->status
= STATUS_SUCCESS
;
1612 current
->suspend_cookie
= req
->cookie
;
1613 wake_up( ¤t
->context
->obj
, 0 );
1618 set_error( STATUS_INVALID_PARAMETER
);
1622 op_size
= min( req
->size
, sizeof(select_op
) );
1623 memset( &select_op
, 0, sizeof(select_op
) );
1624 memcpy( &select_op
, result
+ 1, op_size
);
1626 /* first store results of previous apc */
1629 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev_apc
,
1630 0, &thread_apc_ops
))) return;
1631 apc
->result
= *result
;
1633 if (apc
->result
.type
== APC_CREATE_THREAD
) /* transfer the handle to the caller process */
1635 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->result
.create_thread
.handle
,
1636 apc
->caller
->process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1637 close_handle( current
->process
, apc
->result
.create_thread
.handle
);
1638 apc
->result
.create_thread
.handle
= handle
;
1639 clear_error(); /* ignore errors from the above calls */
1641 else if (apc
->result
.type
== APC_ASYNC_IO
)
1644 async_set_result( apc
->owner
, apc
->result
.async_io
.status
, apc
->result
.async_io
.total
);
1646 wake_up( &apc
->obj
, 0 );
1647 close_handle( current
->process
, req
->prev_apc
);
1648 release_object( apc
);
1651 select_on( &select_op
, op_size
, req
->cookie
, req
->flags
, req
->timeout
);
1653 while (get_error() == STATUS_USER_APC
)
1655 if (!(apc
= thread_dequeue_apc( current
, 0 )))
1657 /* Optimization: ignore APC_NONE calls, they are only used to
1658 * wake up a thread, but since we got here the thread woke up already.
1660 if (apc
->call
.type
!= APC_NONE
&&
1661 (reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1663 reply
->call
= apc
->call
;
1664 release_object( apc
);
1668 wake_up( &apc
->obj
, 0 );
1669 release_object( apc
);
1672 if (get_error() == STATUS_KERNEL_APC
)
1674 apc
= thread_dequeue_apc( current
, 1 );
1675 if ((reply
->apc_handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1676 reply
->call
= apc
->call
;
1680 wake_up( &apc
->obj
, 0 );
1682 release_object( apc
);
1684 else if (get_error() != STATUS_PENDING
&& get_reply_max_size() == sizeof(context_t
) &&
1685 current
->context
&& current
->suspend_cookie
== req
->cookie
)
1687 if (current
->context
->regs
.flags
)
1689 unsigned int system_flags
= get_context_system_regs(current
->process
->cpu
) &
1690 current
->context
->regs
.flags
;
1691 if (system_flags
) set_thread_context( current
, ¤t
->context
->regs
, system_flags
);
1692 set_reply_data( ¤t
->context
->regs
, sizeof(context_t
) );
1694 release_object( current
->context
);
1695 current
->context
= NULL
;
1699 /* queue an APC for a thread or process */
1700 DECL_HANDLER(queue_apc
)
1702 struct thread
*thread
= NULL
;
1703 struct process
*process
= NULL
;
1704 struct thread_apc
*apc
;
1706 if (!(apc
= create_apc( NULL
, &req
->call
))) return;
1708 switch (apc
->call
.type
)
1712 thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
);
1714 case APC_VIRTUAL_ALLOC
:
1715 case APC_VIRTUAL_FREE
:
1716 case APC_VIRTUAL_PROTECT
:
1717 case APC_VIRTUAL_FLUSH
:
1718 case APC_VIRTUAL_LOCK
:
1719 case APC_VIRTUAL_UNLOCK
:
1720 case APC_UNMAP_VIEW
:
1721 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1723 case APC_VIRTUAL_QUERY
:
1724 process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
);
1727 process
= get_process_from_handle( req
->handle
, PROCESS_VM_OPERATION
);
1728 if (process
&& process
!= current
->process
)
1730 /* duplicate the handle into the target process */
1731 obj_handle_t handle
= duplicate_handle( current
->process
, apc
->call
.map_view
.handle
,
1732 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
1733 if (handle
) apc
->call
.map_view
.handle
= handle
;
1736 release_object( process
);
1741 case APC_CREATE_THREAD
:
1742 case APC_BREAK_PROCESS
:
1743 process
= get_process_from_handle( req
->handle
, PROCESS_CREATE_THREAD
);
1746 set_error( STATUS_INVALID_PARAMETER
);
1752 if (!queue_apc( NULL
, thread
, apc
)) set_error( STATUS_THREAD_IS_TERMINATING
);
1753 release_object( thread
);
1757 reply
->self
= (process
== current
->process
);
1760 obj_handle_t handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 );
1763 if (queue_apc( process
, NULL
, apc
))
1765 apc
->caller
= (struct thread
*)grab_object( current
);
1766 reply
->handle
= handle
;
1770 close_handle( current
->process
, handle
);
1771 set_error( STATUS_PROCESS_IS_TERMINATING
);
1775 release_object( process
);
1778 release_object( apc
);
1781 /* Get the result of an APC call */
1782 DECL_HANDLER(get_apc_result
)
1784 struct thread_apc
*apc
;
1786 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->handle
,
1787 0, &thread_apc_ops
))) return;
1789 if (apc
->executed
) reply
->result
= apc
->result
;
1790 else set_error( STATUS_PENDING
);
1792 /* close the handle directly to avoid an extra round-trip */
1793 close_handle( current
->process
, req
->handle
);
1794 release_object( apc
);
1797 /* retrieve the current context of a thread */
1798 DECL_HANDLER(get_thread_context
)
1800 struct context
*thread_context
= NULL
;
1801 unsigned int system_flags
;
1802 struct thread
*thread
;
1805 if (get_reply_max_size() < sizeof(context_t
))
1807 set_error( STATUS_INVALID_PARAMETER
);
1811 if ((thread_context
= (struct context
*)get_handle_obj( current
->process
, req
->handle
, 0, &context_ops
)))
1813 close_handle( current
->process
, req
->handle
); /* avoid extra server call */
1814 system_flags
= get_context_system_regs( thread_context
->regs
.cpu
);
1816 else if ((thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
)))
1819 system_flags
= get_context_system_regs( thread
->process
->cpu
);
1820 if (thread
->state
== RUNNING
)
1822 reply
->self
= (thread
== current
);
1823 if (thread
!= current
) stop_thread( thread
);
1824 if (thread
->context
)
1826 /* make sure that system regs are valid in thread context */
1827 if (thread
->unix_tid
!= -1 && (req
->flags
& system_flags
& ~thread
->context
->regs
.flags
))
1828 get_thread_context( thread
, &thread
->context
->regs
, req
->flags
& system_flags
);
1829 if (!get_error()) thread_context
= (struct context
*)grab_object( thread
->context
);
1831 else if (!get_error() && (context
= set_reply_data_size( sizeof(context_t
) )))
1833 assert( reply
->self
);
1834 memset( context
, 0, sizeof(context_t
) );
1835 context
->cpu
= thread
->process
->cpu
;
1836 if (req
->flags
& system_flags
)
1838 get_thread_context( thread
, context
, req
->flags
& system_flags
);
1839 context
->flags
|= req
->flags
& system_flags
;
1843 else set_error( STATUS_UNSUCCESSFUL
);
1844 release_object( thread
);
1846 if (get_error() || !thread_context
) return;
1848 set_error( thread_context
->status
);
1849 if (!thread_context
->status
&& (context
= set_reply_data_size( sizeof(context_t
) )))
1851 memset( context
, 0, sizeof(context_t
) );
1852 context
->cpu
= thread_context
->regs
.cpu
;
1853 copy_context( context
, &thread_context
->regs
, req
->flags
);
1854 context
->flags
= req
->flags
;
1856 else if (thread_context
->status
== STATUS_PENDING
)
1858 reply
->handle
= alloc_handle( current
->process
, thread_context
, SYNCHRONIZE
, 0 );
1861 release_object( thread_context
);
1864 /* set the current context of a thread */
1865 DECL_HANDLER(set_thread_context
)
1867 struct thread
*thread
;
1868 const context_t
*context
= get_req_data();
1870 if (get_req_data_size() < sizeof(context_t
))
1872 set_error( STATUS_INVALID_PARAMETER
);
1875 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1876 reply
->self
= (thread
== current
);
1878 if (thread
->state
== TERMINATED
) set_error( STATUS_UNSUCCESSFUL
);
1879 else if (context
->cpu
!= thread
->process
->cpu
) set_error( STATUS_INVALID_PARAMETER
);
1882 unsigned int system_flags
= get_context_system_regs(context
->cpu
) & context
->flags
;
1884 if (thread
!= current
) stop_thread( thread
);
1885 else if (system_flags
) set_thread_context( thread
, context
, system_flags
);
1886 if (thread
->context
&& !get_error())
1888 copy_context( &thread
->context
->regs
, context
, context
->flags
);
1889 thread
->context
->regs
.flags
|= context
->flags
;
1893 release_object( thread
);
1896 /* fetch a selector entry for a thread */
1897 DECL_HANDLER(get_selector_entry
)
1899 struct thread
*thread
;
1900 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1902 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1903 release_object( thread
);