thread.c 57.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Server-side thread management
 *
 * Copyright (C) 1998 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include "config.h"
22
#include "wine/port.h"
23

Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <assert.h>
25
#include <errno.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <fcntl.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
27
#include <signal.h>
28
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
29 30 31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
32
#include <sys/types.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
33
#include <unistd.h>
34
#include <time.h>
35 36 37
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
38 39 40
#ifdef HAVE_SCHED_H
#include <sched.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
41

42 43
#include "ntstatus.h"
#define WIN32_NO_STATUS
44
#include "windef.h"
45
#include "winternl.h"
46

47
#include "file.h"
48 49 50
#include "handle.h"
#include "process.h"
#include "thread.h"
51
#include "request.h"
52
#include "user.h"
53
#include "security.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
54 55


56 57 58 59 60 61
#ifdef __i386__
static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
#elif defined(__x86_64__)
static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
#elif defined(__powerpc__)
static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
62 63
#elif defined(__arm__)
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);
66 67 68 69
#else
#error Unsupported CPU
#endif

Alexandre Julliard's avatar
Alexandre Julliard committed
70 71 72 73
/* thread queues */

struct thread_wait
{
74 75
    struct thread_wait     *next;       /* next wait structure for this thread */
    struct thread          *thread;     /* owner thread */
Alexandre Julliard's avatar
Alexandre Julliard committed
76 77
    int                     count;      /* count of objects */
    int                     flags;
78
    int                     abandoned;
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 */
82
    timeout_t               timeout;
83
    struct timeout_user    *user;
Alexandre Julliard's avatar
Alexandre Julliard committed
84 85 86
    struct wait_queue_entry queues[1];
};

87 88 89 90
/* asynchronous procedure calls */

struct thread_apc
{
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 97
    apc_call_t          call;     /* call arguments */
    apc_result_t        result;   /* call results once executed */
98 99
};

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 );
104 105 106 107 108

static const struct object_ops thread_apc_ops =
{
    sizeof(struct thread_apc),  /* size */
    dump_thread_apc,            /* dump */
109
    no_get_type,                /* get_type */
110 111 112 113 114 115 116
    add_queue,                  /* add_queue */
    remove_queue,               /* remove_queue */
    thread_apc_signaled,        /* signaled */
    no_satisfied,               /* satisfied */
    no_signal,                  /* signal */
    no_get_fd,                  /* get_fd */
    no_map_access,              /* map_access */
117 118
    default_get_sd,             /* get_sd */
    default_set_sd,             /* set_sd */
119
    no_lookup_name,             /* lookup_name */
120 121
    no_link_name,               /* link_name */
    NULL,                       /* unlink_name */
122
    no_open_file,               /* open_file */
123
    no_kernel_obj_list,         /* get_kernel_obj_list */
124
    no_close_handle,            /* close_handle */
125
    thread_apc_destroy          /* destroy */
126 127
};

Alexandre Julliard's avatar
Alexandre Julliard committed
128

Alexandre Julliard's avatar
Alexandre Julliard committed
129 130
/* thread operations */

Alexandre Julliard's avatar
Alexandre Julliard committed
131
static void dump_thread( struct object *obj, int verbose );
132
static struct object_type *thread_get_type( struct object *obj );
133
static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
134
static unsigned int thread_map_access( struct object *obj, unsigned int access );
135
static void thread_poll_event( struct fd *fd, int event );
136
static struct list *thread_get_kernel_obj_list( struct object *obj );
Alexandre Julliard's avatar
Alexandre Julliard committed
137 138 139 140
static void destroy_thread( struct object *obj );

static const struct object_ops thread_ops =
{
141 142
    sizeof(struct thread),      /* size */
    dump_thread,                /* dump */
143
    thread_get_type,            /* get_type */
144 145 146 147
    add_queue,                  /* add_queue */
    remove_queue,               /* remove_queue */
    thread_signaled,            /* signaled */
    no_satisfied,               /* satisfied */
148
    no_signal,                  /* signal */
149
    no_get_fd,                  /* get_fd */
150
    thread_map_access,          /* map_access */
151 152
    default_get_sd,             /* get_sd */
    default_set_sd,             /* set_sd */
153
    no_lookup_name,             /* lookup_name */
154 155
    no_link_name,               /* link_name */
    NULL,                       /* unlink_name */
156
    no_open_file,               /* open_file */
157
    thread_get_kernel_obj_list, /* get_kernel_obj_list */
158
    no_close_handle,            /* close_handle */
159 160 161 162 163
    destroy_thread              /* destroy */
};

static const struct fd_ops thread_fd_ops =
{
164 165
    NULL,                       /* get_poll_events */
    thread_poll_event,          /* poll_event */
166 167
    NULL,                       /* flush */
    NULL,                       /* get_fd_type */
168
    NULL,                       /* ioctl */
169
    NULL,                       /* queue_async */
170
    NULL                        /* reselect_async */
Alexandre Julliard's avatar
Alexandre Julliard committed
171 172
};

173
static struct list thread_list = LIST_INIT(thread_list);
Alexandre Julliard's avatar
Alexandre Julliard committed
174

175
/* initialize the structure for a newly allocated thread */
176
static inline void init_thread_structure( struct thread *thread )
177 178 179
{
    int i;

180
    thread->unix_pid        = -1;  /* not known yet */
181
    thread->unix_tid        = -1;  /* not known yet */
182
    thread->context         = NULL;
183
    thread->suspend_context = NULL;
184
    thread->teb             = 0;
185
    thread->entry_point     = 0;
186 187
    thread->debug_ctx       = NULL;
    thread->debug_event     = NULL;
188
    thread->debug_break     = 0;
189
    thread->system_regs     = 0;
190 191 192
    thread->queue           = NULL;
    thread->wait            = NULL;
    thread->error           = 0;
193 194 195 196
    thread->req_data        = NULL;
    thread->req_toread      = 0;
    thread->reply_data      = NULL;
    thread->reply_towrite   = 0;
197 198 199
    thread->request_fd      = NULL;
    thread->reply_fd        = NULL;
    thread->wait_fd         = NULL;
200 201
    thread->state           = RUNNING;
    thread->exit_code       = 0;
202
    thread->priority        = 0;
203
    thread->suspend         = 0;
204
    thread->desktop_users   = 0;
205
    thread->token           = NULL;
206

207
    thread->creation_time = current_time;
208
    thread->exit_time     = 0;
209

210
    list_init( &thread->mutex_list );
211 212
    list_init( &thread->system_apc );
    list_init( &thread->user_apc );
213
    list_init( &thread->kernel_object );
214

215 216 217 218
    for (i = 0; i < MAX_INFLIGHT_FDS; i++)
        thread->inflight[i].server = thread->inflight[i].client = -1;
}

219
/* check if address looks valid for a client-side data structure (TEB etc.) */
220
static inline int is_valid_address( client_ptr_t addr )
221
{
222
    return addr && !(addr % sizeof(int));
223 224
}

225
/* create a new thread */
226
struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
227 228
{
    struct thread *thread;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    int request_pipe[2];

    if (fd == -1)
    {
        if (pipe( request_pipe ) == -1)
        {
            file_set_error();
            return NULL;
        }
        if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
        {
            close( request_pipe[0] );
            close( request_pipe[1] );
            return NULL;
        }
        close( request_pipe[1] );
        fd = request_pipe[0];
    }
247

248 249
    if (process->is_terminating)
    {
250
        close( fd );
251 252 253 254
        set_error( STATUS_PROCESS_IS_TERMINATING );
        return NULL;
    }

255 256 257 258 259
    if (!(thread = alloc_object( &thread_ops )))
    {
        close( fd );
        return NULL;
    }
260

261
    init_thread_structure( thread );
262

263
    thread->process = (struct process *)grab_object( process );
264
    thread->desktop = process->desktop;
265
    thread->affinity = process->affinity;
266 267
    if (!current) current = thread;

268
    list_add_head( &thread_list, &thread->entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
269

270 271 272 273 274 275 276 277 278
    if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
                                           OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
                                           DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
                                           process->token ))
    {
        close( fd );
        release_object( thread );
        return NULL;
    }
279 280
    if (!(thread->id = alloc_ptid( thread )))
    {
281
        close( fd );
282 283 284
        release_object( thread );
        return NULL;
    }
285
    if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
286 287 288 289
    {
        release_object( thread );
        return NULL;
    }
290

291
    set_fd_events( thread->request_fd, POLLIN );  /* start listening to events */
292
    add_process_thread( thread->process, thread );
293
    return thread;
Alexandre Julliard's avatar
Alexandre Julliard committed
294 295
}

296
/* handle a client event */
297
static void thread_poll_event( struct fd *fd, int event )
298
{
299 300
    struct thread *thread = get_fd_user( fd );
    assert( thread->obj.ops == &thread_ops );
301

302
    grab_object( thread );
303
    if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
304
    else if (event & POLLIN) read_request( thread );
305
    else if (event & POLLOUT) write_reply( thread );
306
    release_object( thread );
307 308
}

309 310 311 312 313 314
static struct list *thread_get_kernel_obj_list( struct object *obj )
{
    struct thread *thread = (struct thread *)obj;
    return &thread->kernel_object;
}

315 316 317 318 319 320
/* cleanup everything that is no longer needed by a dead thread */
/* used by destroy_thread and kill_thread */
static void cleanup_thread( struct thread *thread )
{
    int i;

321 322
    clear_apc_queue( &thread->system_apc );
    clear_apc_queue( &thread->user_apc );
323 324
    free( thread->req_data );
    free( thread->reply_data );
325 326 327
    if (thread->request_fd) release_object( thread->request_fd );
    if (thread->reply_fd) release_object( thread->reply_fd );
    if (thread->wait_fd) release_object( thread->wait_fd );
328
    free( thread->suspend_context );
329
    cleanup_clipboard_thread(thread);
330
    destroy_thread_windows( thread );
331
    free_msg_queue( thread );
332
    close_thread_desktop( thread );
333 334 335 336 337 338 339 340
    for (i = 0; i < MAX_INFLIGHT_FDS; i++)
    {
        if (thread->inflight[i].client != -1)
        {
            close( thread->inflight[i].server );
            thread->inflight[i].client = thread->inflight[i].server = -1;
        }
    }
341 342
    thread->req_data = NULL;
    thread->reply_data = NULL;
343 344 345
    thread->request_fd = NULL;
    thread->reply_fd = NULL;
    thread->wait_fd = NULL;
346 347
    thread->context = NULL;
    thread->suspend_context = NULL;
348
    thread->desktop = 0;
349 350
}

Alexandre Julliard's avatar
Alexandre Julliard committed
351 352 353 354 355 356
/* destroy a thread when its refcount is 0 */
static void destroy_thread( struct object *obj )
{
    struct thread *thread = (struct thread *)obj;
    assert( obj->ops == &thread_ops );

357
    assert( !thread->debug_ctx );  /* cannot still be debugging something */
358
    list_remove( &thread->entry );
359
    cleanup_thread( thread );
360
    release_object( thread->process );
361
    if (thread->id) free_ptid( thread->id );
362
    if (thread->token) release_object( thread->token );
Alexandre Julliard's avatar
Alexandre Julliard committed
363 364
}

Alexandre Julliard's avatar
Alexandre Julliard committed
365 366 367 368 369 370
/* dump a thread on stdout for debugging purposes */
static void dump_thread( struct object *obj, int verbose )
{
    struct thread *thread = (struct thread *)obj;
    assert( obj->ops == &thread_ops );

371 372
    fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
             thread->id, thread->unix_pid, thread->unix_tid, thread->state );
Alexandre Julliard's avatar
Alexandre Julliard committed
373 374
}

375 376 377 378 379 380 381
static struct object_type *thread_get_type( struct object *obj )
{
    static const WCHAR name[] = {'T','h','r','e','a','d'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

382
static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard's avatar
Alexandre Julliard committed
383 384 385 386 387
{
    struct thread *mythread = (struct thread *)obj;
    return (mythread->state == TERMINATED);
}

388 389
static unsigned int thread_map_access( struct object *obj, unsigned int access )
{
390
    if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
391
    if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
392
                                            THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
393
    if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
394
    if (access & GENERIC_ALL)     access |= THREAD_ALL_ACCESS;
395 396 397 398

    if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
    if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;

399 400 401
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

402 403 404 405 406
static void dump_thread_apc( struct object *obj, int verbose )
{
    struct thread_apc *apc = (struct thread_apc *)obj;
    assert( obj->ops == &thread_apc_ops );

407
    fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
408 409
}

410
static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
411
{
412 413
    struct thread_apc *apc = (struct thread_apc *)obj;
    return apc->executed;
414 415
}

416 417 418 419
static void thread_apc_destroy( struct object *obj )
{
    struct thread_apc *apc = (struct thread_apc *)obj;
    if (apc->caller) release_object( apc->caller );
420
    if (apc->owner) release_object( apc->owner );
421 422
}

423 424 425 426 427 428 429 430
/* queue an async procedure call */
static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
{
    struct thread_apc *apc;

    if ((apc = alloc_object( &thread_apc_ops )))
    {
        apc->call        = *call_data;
431
        apc->caller      = NULL;
432 433 434
        apc->owner       = owner;
        apc->executed    = 0;
        apc->result.type = APC_NONE;
435
        if (owner) grab_object( owner );
436 437 438 439
    }
    return apc;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
440
/* get a thread pointer from a thread id (and increment the refcount) */
441
struct thread *get_thread_from_id( thread_id_t id )
Alexandre Julliard's avatar
Alexandre Julliard committed
442
{
443 444 445
    struct object *obj = get_ptid_entry( id );

    if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
446
    set_error( STATUS_INVALID_CID );
447
    return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
448 449
}

Alexandre Julliard's avatar
Alexandre Julliard committed
450
/* get a thread from a handle (and increment the refcount) */
451
struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
Alexandre Julliard's avatar
Alexandre Julliard committed
452
{
Alexandre Julliard's avatar
Alexandre Julliard committed
453 454
    return (struct thread *)get_handle_obj( current->process, handle,
                                            access, &thread_ops );
Alexandre Julliard's avatar
Alexandre Julliard committed
455 456
}

457 458
/* find a thread from a Unix tid */
struct thread *get_thread_from_tid( int tid )
459
{
460
    struct thread *thread;
461

462 463
    LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
    {
464
        if (thread->unix_tid == tid) return thread;
465
    }
466 467 468 469 470 471 472 473
    return NULL;
}

/* find a thread from a Unix pid */
struct thread *get_thread_from_pid( int pid )
{
    struct thread *thread;

474 475 476 477
    LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
    {
        if (thread->unix_pid == pid) return thread;
    }
478
    return NULL;
479 480
}

481
int set_thread_affinity( struct thread *thread, affinity_t affinity )
482
{
483
    int ret = 0;
484
#ifdef HAVE_SCHED_SETAFFINITY
485
    if (thread->unix_tid != -1)
486 487 488 489 490 491 492 493 494
    {
        cpu_set_t set;
        int i;
        affinity_t mask;

        CPU_ZERO( &set );
        for (i = 0, mask = 1; mask; i++, mask <<= 1)
            if (affinity & mask) CPU_SET( i, &set );

495
        ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
496 497
    }
#endif
498 499
    if (!ret) thread->affinity = affinity;
    return ret;
500 501
}

502 503 504 505 506 507 508 509 510 511 512
affinity_t get_thread_affinity( struct thread *thread )
{
    affinity_t mask = 0;
#ifdef HAVE_SCHED_SETAFFINITY
    if (thread->unix_tid != -1)
    {
        cpu_set_t set;
        unsigned int i;

        if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
            for (i = 0; i < 8 * sizeof(mask); i++)
513
                if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
514 515
    }
#endif
516
    if (!mask) mask = ~(affinity_t)0;
517 518 519
    return mask;
}

520 521 522
#define THREAD_PRIORITY_REALTIME_HIGHEST 6
#define THREAD_PRIORITY_REALTIME_LOWEST -7

523
/* set all information about a thread */
524
static void set_thread_info( struct thread *thread,
525
                             const struct set_thread_info_request *req )
526 527
{
    if (req->mask & SET_THREAD_INFO_PRIORITY)
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    {
        int max = THREAD_PRIORITY_HIGHEST;
        int min = THREAD_PRIORITY_LOWEST;
        if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
        {
            max = THREAD_PRIORITY_REALTIME_HIGHEST;
            min = THREAD_PRIORITY_REALTIME_LOWEST;
        }
        if ((req->priority >= min && req->priority <= max) ||
            req->priority == THREAD_PRIORITY_IDLE ||
            req->priority == THREAD_PRIORITY_TIME_CRITICAL)
            thread->priority = req->priority;
        else
            set_error( STATUS_INVALID_PARAMETER );
    }
543
    if (req->mask & SET_THREAD_INFO_AFFINITY)
544 545 546 547
    {
        if ((req->affinity & thread->process->affinity) != req->affinity)
            set_error( STATUS_INVALID_PARAMETER );
        else if (thread->state == TERMINATED)
548
            set_error( STATUS_THREAD_IS_TERMINATING );
549 550 551
        else if (set_thread_affinity( thread, req->affinity ))
            file_set_error();
    }
552 553
    if (req->mask & SET_THREAD_INFO_TOKEN)
        security_set_thread_token( thread, req->token );
554 555
    if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
        thread->entry_point = req->entry_point;
556 557
}

558 559 560
/* stop a thread (at the Unix level) */
void stop_thread( struct thread *thread )
{
561
    if (thread->context) return;  /* already inside a debug event, no need for a signal */
562 563 564 565
    /* can't stop a thread while initialisation is in progress */
    if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
}

566 567 568 569 570 571
/* stop a thread if it's supposed to be suspended */
void stop_thread_if_suspended( struct thread *thread )
{
    if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
}

572
/* suspend a thread */
573
int suspend_thread( struct thread *thread )
574 575
{
    int old_count = thread->suspend;
576
    if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
577
    {
578
        if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
579
    }
580
    else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
581 582 583 584
    return old_count;
}

/* resume a thread */
585
int resume_thread( struct thread *thread )
586 587 588 589
{
    int old_count = thread->suspend;
    if (thread->suspend > 0)
    {
590
        if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
591 592
    }
    return old_count;
Alexandre Julliard's avatar
Alexandre Julliard committed
593 594 595
}

/* add a thread to an object wait queue; return 1 if OK, 0 on error */
596
int add_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard's avatar
Alexandre Julliard committed
597
{
598
    grab_object( obj );
599 600
    entry->obj = obj;
    list_add_tail( &obj->wait_queue, &entry->entry );
601
    return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
602 603 604
}

/* remove a thread from an object wait queue */
605
void remove_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliard's avatar
Alexandre Julliard committed
606
{
607
    list_remove( &entry->entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
608 609 610
    release_object( obj );
}

611 612 613 614 615
struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
{
    return entry->wait->thread;
}

616 617 618 619 620 621 622 623 624 625
enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
{
    return entry->wait->select;
}

client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
{
    return entry->wait->key;
}

626 627 628 629 630
void make_wait_abandoned( struct wait_queue_entry *entry )
{
    entry->wait->abandoned = 1;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
631
/* finish waiting */
632
static unsigned int end_wait( struct thread *thread, unsigned int status )
Alexandre Julliard's avatar
Alexandre Julliard committed
633 634 635 636 637 638
{
    struct thread_wait *wait = thread->wait;
    struct wait_queue_entry *entry;
    int i;

    assert( wait );
639
    thread->wait = wait->next;
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

    if (status < wait->count)  /* wait satisfied, tell it to the objects */
    {
        if (wait->select == SELECT_WAIT_ALL)
        {
            for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
                entry->obj->ops->satisfied( entry->obj, entry );
        }
        else
        {
            entry = wait->queues + status;
            entry->obj->ops->satisfied( entry->obj, entry );
        }
        if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
    }
655 656
    for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
        entry->obj->ops->remove_queue( entry->obj, entry );
657
    if (wait->user) remove_timeout_user( wait->user );
Alexandre Julliard's avatar
Alexandre Julliard committed
658
    free( wait );
659
    return status;
Alexandre Julliard's avatar
Alexandre Julliard committed
660 661 662
}

/* build the thread wait structure */
663 664
static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
                    int flags, timeout_t timeout )
Alexandre Julliard's avatar
Alexandre Julliard committed
665 666 667
{
    struct thread_wait *wait;
    struct wait_queue_entry *entry;
668
    unsigned int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
669

670
    if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
671 672
    wait->next    = current->wait;
    wait->thread  = current;
Alexandre Julliard's avatar
Alexandre Julliard committed
673 674
    wait->count   = count;
    wait->flags   = flags;
675
    wait->select  = select_op->op;
676
    wait->cookie  = 0;
677
    wait->user    = NULL;
678
    wait->timeout = timeout;
679
    wait->abandoned = 0;
680
    current->wait = wait;
Alexandre Julliard's avatar
Alexandre Julliard committed
681 682 683

    for (i = 0, entry = wait->queues; i < count; i++, entry++)
    {
684
        struct object *obj = objects[i];
685
        entry->wait = wait;
686 687
        if (!obj->ops->add_queue( obj, entry ))
        {
688
            wait->count = i;
689
            end_wait( current, get_error() );
690 691
            return 0;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
692 693 694 695
    }
    return 1;
}

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
                            int flags, timeout_t timeout )
{
    struct object *objects[MAXIMUM_WAIT_OBJECTS];
    unsigned int i;
    int ret = 0;

    assert( count <= MAXIMUM_WAIT_OBJECTS );

    for (i = 0; i < count; i++)
        if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
            break;

    if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );

    while (i > 0) release_object( objects[--i] );
    return ret;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
715
/* check if the thread waiting condition is satisfied */
716
static int check_wait( struct thread *thread )
Alexandre Julliard's avatar
Alexandre Julliard committed
717
{
718
    int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
719
    struct thread_wait *wait = thread->wait;
720
    struct wait_queue_entry *entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
721

722 723 724 725 726
    assert( wait );

    if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
        return STATUS_USER_APC;

727
    /* Suspended threads may not acquire locks, but they can run system APCs */
728
    if (thread->process->suspend + thread->suspend > 0) return -1;
729

730
    if (wait->select == SELECT_WAIT_ALL)
Alexandre Julliard's avatar
Alexandre Julliard committed
731
    {
732 733 734
        int not_ok = 0;
        /* Note: we must check them all anyway, as some objects may
         * want to do something when signaled, even if others are not */
Alexandre Julliard's avatar
Alexandre Julliard committed
735
        for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
736
            not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
737
        if (!not_ok) return STATUS_WAIT_0;
Alexandre Julliard's avatar
Alexandre Julliard committed
738 739 740 741
    }
    else
    {
        for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
742
            if (entry->obj->ops->signaled( entry->obj, entry )) return i;
Alexandre Julliard's avatar
Alexandre Julliard committed
743
    }
744

745
    if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
746
    if (wait->timeout <= current_time) return STATUS_TIMEOUT;
747 748 749
    return -1;
}

750
/* send the wakeup signal to a thread */
751
static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
752 753 754 755
{
    struct wake_up_reply reply;
    int ret;

756
    memset( &reply, 0, sizeof(reply) );
757 758
    reply.cookie   = cookie;
    reply.signaled = signaled;
759 760
    if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
        return 0;
761 762 763 764 765
    if (ret >= 0)
        fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
    else if (errno == EPIPE)
        kill_thread( thread, 0 );  /* normal death */
    else
766
        fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
767 768 769
    return -1;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
770
/* attempt to wake up a thread */
771
/* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
772
int wake_thread( struct thread *thread )
Alexandre Julliard's avatar
Alexandre Julliard committed
773
{
774
    int signaled, count;
775
    client_ptr_t cookie;
776

777 778 779 780 781
    for (count = 0; thread->wait; count++)
    {
        if ((signaled = check_wait( thread )) == -1) break;

        cookie = thread->wait->cookie;
782
        signaled = end_wait( thread, signaled );
783
        if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
784
        if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
785 786 787 788
        {
            if (!count) count = -1;
            break;
        }
789 790
    }
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
791 792
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806
/* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
int wake_thread_queue_entry( struct wait_queue_entry *entry )
{
    struct thread_wait *wait = entry->wait;
    struct thread *thread = wait->thread;
    int signaled;
    client_ptr_t cookie;

    if (thread->wait != wait) return 0;  /* not the current wait */
    if (thread->process->suspend + thread->suspend > 0) return 0;  /* cannot acquire locks */

    assert( wait->select != SELECT_WAIT_ALL );

    cookie = wait->cookie;
807
    signaled = end_wait( thread, entry - wait->queues );
808 809
    if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );

810
    if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
811 812 813 814 815
        wake_thread( thread );  /* check other waits too */

    return 1;
}

816 817 818
/* thread wait timeout */
static void thread_timeout( void *ptr )
{
819 820
    struct thread_wait *wait = ptr;
    struct thread *thread = wait->thread;
821
    client_ptr_t cookie = wait->cookie;
822

823 824
    wait->user = NULL;
    if (thread->wait != wait) return; /* not the top-level wait, ignore it */
825
    if (thread->suspend + thread->process->suspend > 0) return;  /* suspended, ignore it */
826

827
    if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
828
    end_wait( thread, STATUS_TIMEOUT );
829 830

    assert( cookie );
831
    if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
832 833
    /* check if other objects have become signaled in the meantime */
    wake_thread( thread );
834 835
}

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
/* try signaling an event flag, a semaphore or a mutex */
static int signal_object( obj_handle_t handle )
{
    struct object *obj;
    int ret = 0;

    obj = get_handle_obj( current->process, handle, 0, NULL );
    if (obj)
    {
        ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
        release_object( obj );
    }
    return ret;
}

851
/* select on a list of handles */
852
static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
853
                            int flags, timeout_t timeout )
Alexandre Julliard's avatar
Alexandre Julliard committed
854
{
855
    int ret;
856
    unsigned int count;
857
    struct object *object;
858

859 860
    if (timeout <= 0) timeout = current_time - timeout;

861
    switch (select_op->op)
862
    {
863
    case SELECT_NONE:
864
        if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
865 866 867
        break;

    case SELECT_WAIT:
868
    case SELECT_WAIT_ALL:
869 870 871 872 873 874
        count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
        if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
        {
            set_error( STATUS_INVALID_PARAMETER );
            return 0;
        }
875 876 877 878 879 880 881 882 883 884 885
        if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
            return timeout;
        break;

    case SELECT_SIGNAL_AND_WAIT:
        if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
            return timeout;
        if (select_op->signal_and_wait.signal)
        {
            if (!signal_object( select_op->signal_and_wait.signal ))
            {
886
                end_wait( current, get_error() );
887 888 889 890 891
                return timeout;
            }
            /* check if we woke ourselves up */
            if (!current->wait) return timeout;
        }
892 893
        break;

894 895 896 897 898 899 900 901 902 903 904
    case SELECT_KEYED_EVENT_WAIT:
    case SELECT_KEYED_EVENT_RELEASE:
        object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
                         select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
        if (!object) return timeout;
        ret = wait_on( select_op, 1, &object, flags, timeout );
        release_object( object );
        if (!ret) return timeout;
        current->wait->key = select_op->keyed_event.key;
        break;

905
    default:
906
        set_error( STATUS_INVALID_PARAMETER );
907
        return 0;
908
    }
909

910 911 912
    if ((ret = check_wait( current )) != -1)
    {
        /* condition is already satisfied */
913
        set_error( end_wait( current, ret ));
914
        return timeout;
915 916 917
    }

    /* now we need to wait */
918
    if (current->wait->timeout != TIMEOUT_INFINITE)
919
    {
920
        if (!(current->wait->user = add_timeout_user( current->wait->timeout,
921
                                                      thread_timeout, current->wait )))
922
        {
923
            end_wait( current, get_error() );
924
            return timeout;
925 926
        }
    }
927
    current->wait->cookie = cookie;
928
    set_error( STATUS_PENDING );
929
    return timeout;
Alexandre Julliard's avatar
Alexandre Julliard committed
930 931 932 933 934
}

/* attempt to wake threads sleeping on the object wait queue */
void wake_up( struct object *obj, int max )
{
935
    struct list *ptr;
936
    int ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
937

938
    LIST_FOR_EACH( ptr, &obj->wait_queue )
Alexandre Julliard's avatar
Alexandre Julliard committed
939
    {
940
        struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
941 942
        if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
        if (ret > 0 && max && !--max) break;
943 944
        /* restart at the head of the list since a wake up can change the object wait queue */
        ptr = &obj->wait_queue;
Alexandre Julliard's avatar
Alexandre Julliard committed
945 946 947
    }
}

948 949 950 951 952 953 954 955 956 957 958 959 960 961
/* return the apc queue to use for a given apc type */
static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
{
    switch(type)
    {
    case APC_NONE:
    case APC_USER:
    case APC_TIMER:
        return &thread->user_apc;
    default:
        return &thread->system_apc;
    }
}

962 963 964 965 966 967 968
/* check if thread is currently waiting for a (system) apc */
static inline int is_in_apc_wait( struct thread *thread )
{
    return (thread->process->suspend || thread->suspend ||
            (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
}

969 970
/* queue an existing APC to a given thread */
static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
971
{
972 973
    struct list *queue;

974 975 976
    if (thread && thread->state == TERMINATED && process)
        thread = NULL;

977 978 979
    if (!thread)  /* find a suitable thread inside the process */
    {
        struct thread *candidate;
980

981 982 983 984
        /* first try to find a waiting thread */
        LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
        {
            if (candidate->state == TERMINATED) continue;
985
            if (is_in_apc_wait( candidate ))
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
            {
                thread = candidate;
                break;
            }
        }
        if (!thread)
        {
            /* then use the first one that accepts a signal */
            LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
            {
                if (send_thread_signal( candidate, SIGUSR1 ))
                {
                    thread = candidate;
                    break;
                }
            }
        }
        if (!thread) return 0;  /* nothing found */
1004
        queue = get_apc_queue( thread, apc->call.type );
1005 1006 1007 1008
    }
    else
    {
        if (thread->state == TERMINATED) return 0;
1009 1010 1011 1012 1013 1014
        queue = get_apc_queue( thread, apc->call.type );
        /* send signal for system APCs if needed */
        if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
        {
            if (!send_thread_signal( thread, SIGUSR1 )) return 0;
        }
1015 1016 1017
        /* cancel a possible previous APC with the same owner */
        if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
    }
1018

1019
    grab_object( apc );
1020 1021
    list_add_tail( queue, &apc->entry );
    if (!list_prev( queue, &apc->entry ))  /* first one */
1022
        wake_thread( thread );
1023

1024 1025 1026
    return 1;
}

1027
/* queue an async procedure call */
1028
int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1029 1030 1031 1032 1033 1034
{
    struct thread_apc *apc;
    int ret = 0;

    if ((apc = create_apc( owner, call_data )))
    {
1035
        ret = queue_apc( process, thread, apc );
1036 1037 1038 1039 1040
        release_object( apc );
    }
    return ret;
}

1041
/* cancel the async procedure call owned by a specific object */
1042
void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1043 1044
{
    struct thread_apc *apc;
1045 1046
    struct list *queue = get_apc_queue( thread, type );

1047
    LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1048 1049
    {
        if (apc->owner != owner) continue;
1050
        list_remove( &apc->entry );
1051 1052
        apc->executed = 1;
        wake_up( &apc->obj, 0 );
1053
        release_object( apc );
1054 1055 1056 1057
        return;
    }
}

1058
/* remove the head apc from the queue; the returned object must be released by the caller */
1059
static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
1060
{
1061 1062
    struct thread_apc *apc = NULL;
    struct list *ptr = list_head( &thread->system_apc );
1063

1064 1065
    if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
    if (ptr)
1066
    {
1067 1068
        apc = LIST_ENTRY( ptr, struct thread_apc, entry );
        list_remove( ptr );
1069 1070 1071 1072
    }
    return apc;
}

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
/* clear an APC queue, cancelling all the APCs on it */
static void clear_apc_queue( struct list *queue )
{
    struct list *ptr;

    while ((ptr = list_head( queue )))
    {
        struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
        list_remove( &apc->entry );
        apc->executed = 1;
        wake_up( &apc->obj, 0 );
        release_object( apc );
    }
}

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
/* add an fd to the inflight list */
/* return list index, or -1 on error */
int thread_add_inflight_fd( struct thread *thread, int client, int server )
{
    int i;

    if (server == -1) return -1;
    if (client == -1)
    {
        close( server );
        return -1;
    }

    /* first check if we already have an entry for this fd */
    for (i = 0; i < MAX_INFLIGHT_FDS; i++)
        if (thread->inflight[i].client == client)
        {
            close( thread->inflight[i].server );
            thread->inflight[i].server = server;
            return i;
        }

    /* now find a free spot to store it */
    for (i = 0; i < MAX_INFLIGHT_FDS; i++)
        if (thread->inflight[i].client == -1)
        {
            thread->inflight[i].client = client;
            thread->inflight[i].server = server;
            return i;
        }
1118 1119

    close( server );
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    return -1;
}

/* get an inflight fd and purge it from the list */
/* the fd must be closed when no longer used */
int thread_get_inflight_fd( struct thread *thread, int client )
{
    int i, ret;

    if (client == -1) return -1;

    do
    {
        for (i = 0; i < MAX_INFLIGHT_FDS; i++)
        {
            if (thread->inflight[i].client == client)
            {
                ret = thread->inflight[i].server;
                thread->inflight[i].server = thread->inflight[i].client = -1;
                return ret;
            }
        }
    } while (!receive_fd( thread->process ));  /* in case it is still in the socket buffer */
    return -1;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1146
/* kill a thread on the spot */
1147
void kill_thread( struct thread *thread, int violent_death )
Alexandre Julliard's avatar
Alexandre Julliard committed
1148
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1149
    if (thread->state == TERMINATED) return;  /* already killed */
Alexandre Julliard's avatar
Alexandre Julliard committed
1150
    thread->state = TERMINATED;
1151
    thread->exit_time = current_time;
1152
    if (current == thread) current = NULL;
1153
    if (debug_level)
1154 1155
        fprintf( stderr,"%04x: *killed* exit_code=%d\n",
                 thread->id, thread->exit_code );
1156 1157
    if (thread->wait)
    {
1158
        while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1159
        send_thread_wakeup( thread, 0, thread->exit_code );
1160
        /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1161 1162
        violent_death = 0;
    }
1163
    kill_console_processes( thread, 0 );
1164
    debug_exit_thread( thread );
Alexandre Julliard's avatar
Alexandre Julliard committed
1165
    abandon_mutexes( thread );
Alexandre Julliard's avatar
Alexandre Julliard committed
1166
    wake_up( &thread->obj, 0 );
1167
    if (violent_death) send_thread_signal( thread, SIGQUIT );
1168
    cleanup_thread( thread );
1169
    remove_process_thread( thread->process, thread );
Alexandre Julliard's avatar
Alexandre Julliard committed
1170
    release_object( thread );
Alexandre Julliard's avatar
Alexandre Julliard committed
1171
}
1172

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
/* copy parts of a context structure */
static void copy_context( context_t *to, const context_t *from, unsigned int flags )
{
    assert( to->cpu == from->cpu );
    to->flags |= flags;
    if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
    if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
    if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
    if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
    if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
    if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
}

/* return the context flags that correspond to system regs */
/* (system regs are the ones we can't access on the client side) */
static unsigned int get_context_system_regs( enum cpu_type cpu )
{
    switch (cpu)
    {
    case CPU_x86:     return SERVER_CTX_DEBUG_REGISTERS;
    case CPU_x86_64:  return SERVER_CTX_DEBUG_REGISTERS;
    case CPU_POWERPC: return 0;
1195
    case CPU_ARM:     return SERVER_CTX_DEBUG_REGISTERS;
1196
    case CPU_ARM64:   return SERVER_CTX_DEBUG_REGISTERS;
1197 1198 1199 1200
    }
    return 0;
}

1201 1202 1203
/* trigger a breakpoint event in a given thread */
void break_thread( struct thread *thread )
{
1204
    debug_event_t data;
1205 1206 1207

    assert( thread->context );

1208 1209 1210 1211
    memset( &data, 0, sizeof(data) );
    data.exception.first     = 1;
    data.exception.exc_code  = STATUS_BREAKPOINT;
    data.exception.flags     = EXCEPTION_CONTINUABLE;
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    switch (thread->context->cpu)
    {
    case CPU_x86:
        data.exception.address = thread->context->ctl.i386_regs.eip;
        break;
    case CPU_x86_64:
        data.exception.address = thread->context->ctl.x86_64_regs.rip;
        break;
    case CPU_POWERPC:
        data.exception.address = thread->context->ctl.powerpc_regs.iar;
        break;
1223 1224 1225
    case CPU_ARM:
        data.exception.address = thread->context->ctl.arm_regs.pc;
        break;
1226 1227 1228
    case CPU_ARM64:
        data.exception.address = thread->context->ctl.arm64_regs.pc;
        break;
1229
    }
1230 1231 1232 1233
    generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
    thread->debug_break = 0;
}

1234 1235 1236 1237 1238 1239 1240
/* take a snapshot of currently running threads */
struct thread_snapshot *thread_snap( int *count )
{
    struct thread_snapshot *snapshot, *ptr;
    struct thread *thread;
    int total = 0;

1241
    LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1242 1243 1244
        if (thread->state != TERMINATED) total++;
    if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
    ptr = snapshot;
1245
    LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    {
        if (thread->state == TERMINATED) continue;
        ptr->thread   = thread;
        ptr->count    = thread->obj.refcount;
        ptr->priority = thread->priority;
        grab_object( thread );
        ptr++;
    }
    *count = total;
    return snapshot;
}

1258 1259 1260 1261 1262 1263 1264 1265 1266
/* gets the current impersonation token */
struct token *thread_get_impersonation_token( struct thread *thread )
{
    if (thread->token)
        return thread->token;
    else
        return thread->process->token;
}

1267 1268 1269 1270 1271
/* check if a cpu type can be supported on this server */
int is_cpu_supported( enum cpu_type cpu )
{
    unsigned int prefix_cpu_mask = get_prefix_cpu_mask();

1272
    if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1273 1274 1275 1276 1277 1278 1279 1280 1281
    if (!(supported_cpus & prefix_cpu_mask))
        set_error( STATUS_NOT_SUPPORTED );
    else if (supported_cpus & CPU_FLAG(cpu))
        set_error( STATUS_INVALID_IMAGE_WIN_64 );  /* server supports it but not the prefix */
    else
        set_error( STATUS_INVALID_IMAGE_FORMAT );
    return 0;
}

1282 1283 1284 1285 1286 1287
/* return the cpu mask for supported cpus */
unsigned int get_supported_cpu_mask(void)
{
    return supported_cpus & get_prefix_cpu_mask();
}

1288 1289 1290
/* create a new thread */
DECL_HANDLER(new_thread)
{
1291
    struct thread *thread;
1292
    struct process *process;
1293 1294 1295
    struct unicode_str name;
    const struct security_descriptor *sd;
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1296
    int request_fd = thread_get_inflight_fd( current, req->request_fd );
1297

1298
    if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1299
    {
1300
        if (request_fd != -1) close( request_fd );
1301 1302 1303
        return;
    }

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
    if (process != current->process)
    {
        if (request_fd != -1)  /* can't create a request fd in a different process */
        {
            close( request_fd );
            set_error( STATUS_INVALID_PARAMETER );
            goto done;
        }
        if (process->running_threads)  /* only the initial thread can be created in another process */
        {
            set_error( STATUS_ACCESS_DENIED );
            goto done;
        }
    }
    else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
    {
        if (request_fd != -1) close( request_fd );
        set_error( STATUS_INVALID_HANDLE );
        goto done;
    }

    if ((thread = create_thread( request_fd, process, sd )))
1326
    {
1327
        thread->system_regs = current->system_regs;
1328
        if (req->suspend) thread->suspend++;
1329
        reply->tid = get_thread_id( thread );
1330 1331
        if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
                                                           req->access, objattr->attributes )))
1332
        {
1333
            /* thread object will be released when the thread gets killed */
1334
            goto done;
1335
        }
1336
        kill_thread( thread, 1 );
1337
    }
1338 1339
done:
    release_object( process );
1340 1341 1342 1343 1344
}

/* initialize a new thread */
DECL_HANDLER(init_thread)
{
1345
    struct process *process = current->process;
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
    int wait_fd, reply_fd;

    if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
    {
        set_error( STATUS_TOO_MANY_OPENED_FILES );
        return;
    }
    if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
    {
        set_error( STATUS_TOO_MANY_OPENED_FILES );
        goto error;
    }
1358

1359
    if (current->reply_fd)  /* already initialised */
1360
    {
1361
        set_error( STATUS_INVALID_PARAMETER );
1362 1363
        goto error;
    }
1364

1365
    if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1366

1367
    current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj, 0 );
1368 1369
    current->wait_fd  = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj, 0 );
    if (!current->reply_fd || !current->wait_fd) return;
1370

1371
    if (!is_valid_address(req->teb))
1372 1373 1374 1375 1376
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

1377
    current->unix_pid = req->unix_pid;
1378
    current->unix_tid = req->unix_tid;
1379
    current->teb      = req->teb;
1380
    current->entry_point = process->peb ? req->entry : 0;
1381

1382 1383
    if (!process->peb)  /* first thread, initialize the process too */
    {
1384
        if (!is_cpu_supported( req->cpu )) return;
1385
        process->unix_pid = current->unix_pid;
1386
        process->peb      = req->entry;
1387
        process->cpu      = req->cpu;
1388
        reply->info_size  = init_process( current );
1389
        if (!process->parent_id)
1390 1391 1392
            process->affinity = current->affinity = get_thread_affinity( current );
        else
            set_thread_affinity( current, current->affinity );
1393 1394 1395
    }
    else
    {
1396 1397 1398 1399 1400
        if (req->cpu != process->cpu)
        {
            set_error( STATUS_INVALID_PARAMETER );
            return;
        }
1401 1402
        if (process->unix_pid != current->unix_pid)
            process->unix_pid = -1;  /* can happen with linuxthreads */
1403
        init_thread_context( current );
1404
        generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
1405
        set_thread_affinity( current, current->affinity );
1406 1407
    }
    debug_level = max( debug_level, req->debug_level );
1408

1409
    reply->pid     = get_process_id( process );
1410 1411
    reply->tid     = get_thread_id( current );
    reply->version = SERVER_PROTOCOL_VERSION;
1412
    reply->server_start = server_start_time;
1413
    reply->all_cpus     = supported_cpus & get_prefix_cpu_mask();
1414
    reply->suspend      = (current->suspend || process->suspend);
1415 1416 1417 1418 1419 1420 1421
    return;

 error:
    if (reply_fd != -1) close( reply_fd );
    if (wait_fd != -1) close( wait_fd );
}

1422 1423 1424 1425 1426
/* terminate a thread */
DECL_HANDLER(terminate_thread)
{
    struct thread *thread;

1427 1428
    reply->self = 0;
    reply->last = 0;
1429 1430
    if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
    {
1431 1432 1433 1434
        thread->exit_code = req->exit_code;
        if (thread != current) kill_thread( thread, 1 );
        else
        {
1435 1436
            reply->self = 1;
            reply->last = (thread->process->running_threads == 1);
1437
        }
1438 1439 1440 1441
        release_object( thread );
    }
}

1442 1443 1444 1445 1446 1447 1448 1449
/* open a handle to a thread */
DECL_HANDLER(open_thread)
{
    struct thread *thread = get_thread_from_id( req->tid );

    reply->handle = 0;
    if (thread)
    {
1450
        reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1451 1452 1453 1454
        release_object( thread );
    }
}

1455 1456 1457 1458
/* fetch information about a thread */
DECL_HANDLER(get_thread_info)
{
    struct thread *thread;
1459
    obj_handle_t handle = req->handle;
1460

1461
    if (!handle) thread = get_thread_from_id( req->tid_in );
1462
    else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
1463 1464

    if (thread)
1465
    {
1466
        reply->pid            = get_process_id( thread->process );
1467 1468
        reply->tid            = get_thread_id( thread );
        reply->teb            = thread->teb;
1469
        reply->entry_point    = thread->entry_point;
1470
        reply->exit_code      = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1471
        reply->priority       = thread->priority;
1472
        reply->affinity       = thread->affinity;
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
        reply->last           = thread->process->running_threads == 1;

        release_object( thread );
    }
}

/* fetch information about thread times */
DECL_HANDLER(get_thread_times)
{
    struct thread *thread;

    if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
    {
1486 1487
        reply->creation_time  = thread->creation_time;
        reply->exit_time      = thread->exit_time;
1488

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
        release_object( thread );
    }
}

/* set information about a thread */
DECL_HANDLER(set_thread_info)
{
    struct thread *thread;

    if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
    {
        set_thread_info( thread, req );
        release_object( thread );
    }
}

/* suspend a thread */
DECL_HANDLER(suspend_thread)
{
    struct thread *thread;
1509

1510 1511
    if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
    {
1512
        if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1513
        else reply->count = suspend_thread( thread );
1514 1515 1516 1517 1518 1519 1520 1521
        release_object( thread );
    }
}

/* resume a thread */
DECL_HANDLER(resume_thread)
{
    struct thread *thread;
1522

1523 1524
    if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
    {
1525
        reply->count = resume_thread( thread );
1526 1527 1528 1529 1530 1531 1532
        release_object( thread );
    }
}

/* select on a handle list */
DECL_HANDLER(select)
{
1533 1534
    select_op_t select_op;
    data_size_t op_size;
1535 1536 1537 1538 1539 1540 1541 1542
    struct thread_apc *apc;
    const apc_result_t *result = get_req_data();

    if (get_req_data_size() < sizeof(*result))
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
1543 1544 1545 1546 1547 1548
    if (!req->cookie)
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

1549 1550 1551
    op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
    memset( &select_op, 0, sizeof(select_op) );
    memcpy( &select_op, result + 1, op_size );
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569

    /* first store results of previous apc */
    if (req->prev_apc)
    {
        if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
                                                         0, &thread_apc_ops ))) return;
        apc->result = *result;
        apc->executed = 1;
        if (apc->result.type == APC_CREATE_THREAD)  /* transfer the handle to the caller process */
        {
            obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
                                                    apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
            close_handle( current->process, apc->result.create_thread.handle );
            apc->result.create_thread.handle = handle;
            clear_error();  /* ignore errors from the above calls */
        }
        else if (apc->result.type == APC_ASYNC_IO)
        {
1570
            if (apc->owner)
1571
                async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
1572 1573 1574 1575 1576 1577
        }
        wake_up( &apc->obj, 0 );
        close_handle( current->process, req->prev_apc );
        release_object( apc );
    }

1578
    reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588

    if (get_error() == STATUS_USER_APC)
    {
        for (;;)
        {
            if (!(apc = thread_dequeue_apc( current, !(req->flags & SELECT_ALERTABLE) )))
                break;
            /* Optimization: ignore APC_NONE calls, they are only used to
             * wake up a thread, but since we got here the thread woke up already.
             */
1589 1590
            if (apc->call.type != APC_NONE &&
                (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1591
            {
1592
                reply->call = apc->call;
1593 1594 1595 1596 1597 1598 1599 1600
                release_object( apc );
                break;
            }
            apc->executed = 1;
            wake_up( &apc->obj, 0 );
            release_object( apc );
        }
    }
1601 1602
}

1603
/* queue an APC for a thread or process */
1604 1605
DECL_HANDLER(queue_apc)
{
1606 1607
    struct thread *thread = NULL;
    struct process *process = NULL;
1608 1609 1610 1611 1612
    struct thread_apc *apc;

    if (!(apc = create_apc( NULL, &req->call ))) return;

    switch (apc->call.type)
1613
    {
1614 1615
    case APC_NONE:
    case APC_USER:
1616
        thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1617 1618 1619
        break;
    case APC_VIRTUAL_ALLOC:
    case APC_VIRTUAL_FREE:
1620
    case APC_VIRTUAL_PROTECT:
1621
    case APC_VIRTUAL_FLUSH:
1622 1623
    case APC_VIRTUAL_LOCK:
    case APC_VIRTUAL_UNLOCK:
1624
    case APC_UNMAP_VIEW:
1625
        process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1626 1627
        break;
    case APC_VIRTUAL_QUERY:
1628
        process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1629
        break;
1630
    case APC_MAP_VIEW:
1631
        process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1632
        if (process && process != current->process)
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
        {
            /* duplicate the handle into the target process */
            obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
                                                    process, 0, 0, DUP_HANDLE_SAME_ACCESS );
            if (handle) apc->call.map_view.handle = handle;
            else
            {
                release_object( process );
                process = NULL;
            }
        }
        break;
1645
    case APC_CREATE_THREAD:
1646
        process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1647 1648 1649 1650
        break;
    default:
        set_error( STATUS_INVALID_PARAMETER );
        break;
1651
    }
1652 1653 1654 1655 1656 1657 1658 1659

    if (thread)
    {
        if (!queue_apc( NULL, thread, apc )) set_error( STATUS_THREAD_IS_TERMINATING );
        release_object( thread );
    }
    else if (process)
    {
1660 1661
        reply->self = (process == current->process);
        if (!reply->self)
1662
        {
1663 1664
            obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
            if (handle)
1665
            {
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
                if (queue_apc( process, NULL, apc ))
                {
                    apc->caller = (struct thread *)grab_object( current );
                    reply->handle = handle;
                }
                else
                {
                    close_handle( current->process, handle );
                    set_error( STATUS_PROCESS_IS_TERMINATING );
                }
1676 1677 1678 1679 1680
            }
        }
        release_object( process );
    }

1681
    release_object( apc );
1682
}
1683

1684 1685 1686 1687 1688 1689 1690
/* Get the result of an APC call */
DECL_HANDLER(get_apc_result)
{
    struct thread_apc *apc;

    if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
                                                     0, &thread_apc_ops ))) return;
1691 1692 1693 1694 1695 1696

    if (apc->executed) reply->result = apc->result;
    else set_error( STATUS_PENDING );

    /* close the handle directly to avoid an extra round-trip */
    close_handle( current->process, req->handle );
1697 1698 1699
    release_object( apc );
}

1700 1701 1702 1703
/* retrieve the current context of a thread */
DECL_HANDLER(get_thread_context)
{
    struct thread *thread;
1704
    context_t *context;
1705

1706
    if (get_reply_max_size() < sizeof(context_t))
1707 1708 1709 1710 1711
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
    if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1712
    reply->self = (thread == current);
1713

1714
    if (thread != current && !thread->context)
1715 1716
    {
        /* thread is not suspended, retry (if it's still running) */
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
        if (thread->state == RUNNING)
        {
            set_error( STATUS_PENDING );
            if (req->suspend)
            {
                release_object( thread );
                /* make sure we have suspend access */
                if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
                suspend_thread( thread );
            }
        }
1728
        else set_error( STATUS_UNSUCCESSFUL );
1729
    }
1730
    else if ((context = set_reply_data_size( sizeof(context_t) )))
1731
    {
1732
        unsigned int flags = get_context_system_regs( thread->process->cpu );
1733

1734 1735
        memset( context, 0, sizeof(context_t) );
        context->cpu = thread->process->cpu;
1736
        if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
1737
        if (req->flags & flags) get_thread_context( thread, context, req->flags & flags );
1738 1739 1740 1741 1742 1743 1744 1745
    }
    release_object( thread );
}

/* set the current context of a thread */
DECL_HANDLER(set_thread_context)
{
    struct thread *thread;
1746
    const context_t *context = get_req_data();
1747

1748
    if (get_req_data_size() < sizeof(context_t))
1749 1750 1751 1752
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
1753
    if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1754
    reply->self = (thread == current);
1755

1756
    if (thread != current && !thread->context)
1757 1758
    {
        /* thread is not suspended, retry (if it's still running) */
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
        if (thread->state == RUNNING)
        {
            set_error( STATUS_PENDING );
            if (req->suspend)
            {
                release_object( thread );
                /* make sure we have suspend access */
                if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
                suspend_thread( thread );
            }
        }
1770
        else set_error( STATUS_UNSUCCESSFUL );
1771
    }
1772
    else if (context->cpu == thread->process->cpu)
1773
    {
1774 1775
        unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
        unsigned int client_flags = context->flags & ~system_flags;
1776

1777 1778
        if (system_flags) set_thread_context( thread, context, system_flags );
        if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
1779
    }
1780 1781
    else set_error( STATUS_INVALID_PARAMETER );

1782
    release_object( thread );
1783 1784
}

1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
/* retrieve the suspended context of a thread */
DECL_HANDLER(get_suspend_context)
{
    if (get_reply_max_size() < sizeof(context_t))
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

    if (current->suspend_context)
    {
1796 1797 1798 1799
        if (current->suspend_context->flags)
            set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
        else
            free( current->suspend_context );
1800 1801 1802 1803 1804
        if (current->context == current->suspend_context)
        {
            current->context = NULL;
            stop_thread_if_suspended( current );
        }
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
        current->suspend_context = NULL;
    }
    else set_error( STATUS_INVALID_PARAMETER );  /* not suspended, shouldn't happen */
}

/* store the suspended context of a thread */
DECL_HANDLER(set_suspend_context)
{
    const context_t *context = get_req_data();

    if (get_req_data_size() < sizeof(context_t))
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

    if (current->context || context->cpu != current->process->cpu)
    {
        /* nested suspend or exception, shouldn't happen */
        set_error( STATUS_INVALID_PARAMETER );
    }
    else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
    {
        memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
1829
        current->suspend_context->flags = 0;  /* to keep track of what is modified */
1830 1831 1832 1833 1834
        current->context = current->suspend_context;
        if (current->debug_break) break_thread( current );
    }
}

1835 1836 1837 1838 1839 1840
/* fetch a selector entry for a thread */
DECL_HANDLER(get_selector_entry)
{
    struct thread *thread;
    if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
    {
1841
        get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1842 1843 1844
        release_object( thread );
    }
}