aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/runtime/linux_thread.c (13014B)


      1 #define _GNU_SOURCE
      2 #include "linux_thread.h"
      3 #include "guest_heap.h"
      4 #include "guest_crt.h"
      5 #include "browser_profile.h"
      6 #include <errno.h>
      7 #include <pthread.h>
      8 #include <time.h>
      9 #include <unistd.h>
     10 
     11 void recomp_eh_thread_exit(void);
     12 
     13 /* A live thread and its open handle each own a reference. Closing a handle
     14  * never terminates the thread; waits keep their own reference while blocked. */
     15 struct guest_thread {
     16     void (*host_entry)(void);
     17     uint32_t handle, id, entry, argument, allocation, stack_size, exit_code;
     18     uint32_t timer_delay;
     19     unsigned references;
     20     int complete, suspended, automatic, cancelled;
     21     pthread_cond_t changed;
     22     struct guest_thread *next;
     23 };
     24 static struct guest_thread *threads;
     25 static uint32_t next_handle = 0x06000000u;
     26 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     27 static _Thread_local struct guest_thread *current_thread;
     28 
     29 static void check_pthread(int error)
     30 {
     31     if (error) { fprintf(stderr, "Thread pthread: %s\n", strerror(error)); abort(); }
     32 }
     33 
     34 static struct guest_thread *find_thread_locked(uint32_t handle)
     35 {
     36     for (struct guest_thread *thread = threads; thread; thread = thread->next)
     37         if (thread->handle == handle) return thread;
     38     MEM32(g_fs_base + 0x34) = 6;
     39     return NULL;
     40 }
     41 
     42 static void release_thread_locked(struct guest_thread *thread)
     43 {
     44     if (--thread->references) return;
     45     check_pthread(pthread_cond_destroy(&thread->changed));
     46     free(thread);
     47 }
     48 
     49 static void close_thread_locked(struct guest_thread *thread)
     50 {
     51     struct guest_thread **link = &threads;
     52     while (*link && *link != thread) link = &(*link)->next;
     53     if (*link) {
     54         *link = thread->next;
     55         release_thread_locked(thread);
     56     }
     57 }
     58 
     59 static void finish_thread(void *argument)
     60 {
     61     struct guest_thread *thread = argument;
     62     recomp_eh_thread_exit();
     63     MEM32(g_fs_base) = UINT32_MAX;
     64     g_esp = g_fs_base - 0x100;
     65     linux_thread_notify(3); /* DLL_THREAD_DETACH */
     66 #ifdef __EMSCRIPTEN__
     67     browser_profile_thread_finish();
     68 #endif
     69     guest_free(thread->allocation);
     70     check_pthread(pthread_mutex_lock(&mutex));
     71     thread->complete = 1;
     72     check_pthread(pthread_cond_broadcast(&thread->changed));
     73     if (thread->automatic || thread->timer_delay) close_thread_locked(thread);
     74     release_thread_locked(thread);
     75     check_pthread(pthread_mutex_unlock(&mutex));
     76 }
     77 
     78 static void *run_thread(void *argument)
     79 {
     80     struct guest_thread *thread = argument;
     81     current_thread = thread;
     82     g_fs_base = thread->allocation + thread->stack_size;
     83     g_esp = g_fs_base - 0x100;
     84     memset((void *)ADDR(thread->allocation), 0, thread->stack_size + 0x10000);
     85     MEM32(g_fs_base) = UINT32_MAX;
     86     MEM32(g_fs_base + 4) = g_fs_base;
     87     MEM32(g_fs_base + 8) = thread->allocation;
     88     MEM32(g_fs_base + 0x18) = g_fs_base;
     89     MEM32(g_fs_base + 0x20) = getpid();
     90     MEM32(g_fs_base + 0x24) = gettid();
     91 
     92     /* Publish the real host thread ID before returning from thread creation. */
     93     check_pthread(pthread_mutex_lock(&mutex));
     94     thread->id = gettid();
     95     check_pthread(pthread_cond_broadcast(&thread->changed));
     96     while (thread->suspended) check_pthread(pthread_cond_wait(&thread->changed, &mutex));
     97     check_pthread(pthread_mutex_unlock(&mutex));
     98 
     99     pthread_cleanup_push(finish_thread, thread);
    100 #ifdef __EMSCRIPTEN__
    101     browser_profile_thread_initialize(&g_cur_func, thread->entry);
    102 #endif
    103     linux_thread_notify(2); /* DLL_THREAD_ATTACH */
    104     if (thread->host_entry) {
    105         thread->host_entry();
    106         goto finished;
    107     }
    108     if (thread->timer_delay) {
    109         /* A cancelled one-shot timer wakes without entering its guest callback. */
    110         struct timespec deadline;
    111         if (clock_gettime(CLOCK_MONOTONIC, &deadline)) abort();
    112         deadline.tv_sec += thread->timer_delay / 1000;
    113         deadline.tv_nsec += (thread->timer_delay % 1000) * 1000000;
    114         if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; }
    115         check_pthread(pthread_mutex_lock(&mutex));
    116         int error = 0;
    117         while (!thread->cancelled && error != ETIMEDOUT) {
    118             error = pthread_cond_timedwait(&thread->changed, &mutex, &deadline);
    119             if (error != ETIMEDOUT) check_pthread(error);
    120         }
    121         int cancelled = thread->cancelled;
    122         check_pthread(pthread_mutex_unlock(&mutex));
    123         if (cancelled) goto finished;
    124     }
    125     uint32_t stack = g_esp;
    126     if (thread->timer_delay) {
    127         PUSH32(g_esp, 0); /* dw2 */
    128         PUSH32(g_esp, 0); /* dw1 */
    129         PUSH32(g_esp, thread->argument);
    130         PUSH32(g_esp, 0); /* uMsg */
    131         PUSH32(g_esp, thread->handle);
    132     } else {
    133         PUSH32(g_esp, thread->argument);
    134     }
    135     PUSH32(g_esp, RECOMP_RETADDR);
    136     recomp_func_t entry = recomp_lookup(thread->entry);
    137     if (!entry) {
    138         fprintf(stderr, "Missing guest thread entry %08x\n", thread->entry);
    139         abort();
    140     }
    141     entry();
    142     if (thread->automatic) g_esp += 4; /* cdecl caller removes the argument. */
    143     if (g_esp != stack) {
    144         fprintf(stderr, "Guest thread %08x returned with unbalanced stack\n", thread->entry);
    145         abort();
    146     }
    147     thread->exit_code = thread->automatic || thread->timer_delay ? 0 : g_eax;
    148 finished:
    149     pthread_cleanup_pop(1);
    150     return NULL;
    151 }
    152 
    153 static void create_thread(uint32_t stack_size, uint32_t entry, uint32_t argument,
    154                           uint32_t flags, uint32_t id, int automatic, uint32_t timer_delay,
    155                           void (*host_entry)(void))
    156 {
    157     g_eax = automatic ? UINT32_MAX : 0;
    158     if (!entry && !host_entry) { MEM32(guest_crt_errno_address()) = 22; return; }
    159     if (!stack_size) stack_size = 0x100000;
    160     uint64_t rounded = ((uint64_t)stack_size + 0xffffu) & ~0xffffull;
    161     if (rounded > UINT32_MAX - 0x10000u) { MEM32(guest_crt_errno_address()) = 12; return; }
    162 
    163     struct guest_thread *thread = calloc(1, sizeof(*thread));
    164     if (!thread) { MEM32(guest_crt_errno_address()) = 12; return; }
    165     thread->allocation = guest_malloc(rounded + 0x10000);
    166     if (!thread->allocation) { free(thread); MEM32(guest_crt_errno_address()) = 12; return; }
    167     thread->entry = entry;
    168     thread->host_entry = host_entry;
    169     thread->argument = argument;
    170     thread->timer_delay = timer_delay;
    171     thread->stack_size = rounded;
    172     /* Creation retains a reference even if an automatic thread exits immediately. */
    173     thread->references = 3;
    174     thread->suspended = flags == 4;
    175     thread->automatic = automatic;
    176     pthread_condattr_t attributes;
    177     check_pthread(pthread_condattr_init(&attributes));
    178     check_pthread(pthread_condattr_setclock(&attributes, CLOCK_MONOTONIC));
    179     check_pthread(pthread_cond_init(&thread->changed, &attributes));
    180     check_pthread(pthread_condattr_destroy(&attributes));
    181 
    182     check_pthread(pthread_mutex_lock(&mutex));
    183     if (next_handle >= 0x07000000u) abort();
    184     thread->handle = next_handle;
    185     next_handle += 4;
    186     pthread_attr_t host_attributes;
    187     check_pthread(pthread_attr_init(&host_attributes));
    188     check_pthread(pthread_attr_setdetachstate(&host_attributes, PTHREAD_CREATE_DETACHED));
    189     pthread_t host;
    190     int error = pthread_create(&host, &host_attributes, run_thread, thread);
    191     check_pthread(pthread_attr_destroy(&host_attributes));
    192     if (error) {
    193         check_pthread(pthread_cond_destroy(&thread->changed));
    194         guest_free(thread->allocation);
    195         free(thread);
    196         MEM32(guest_crt_errno_address()) = error == EAGAIN ? 11 : 12;
    197     } else {
    198         thread->next = threads;
    199         threads = thread;
    200         while (!thread->id) check_pthread(pthread_cond_wait(&thread->changed, &mutex));
    201         if (id) MEM32(id) = thread->id;
    202         g_eax = thread->handle;
    203         release_thread_locked(thread);
    204     }
    205     check_pthread(pthread_mutex_unlock(&mutex));
    206 }
    207 
    208 /* Runtime workers calling guest code need the same stack, TLS and DLL setup. */
    209 void linux_thread_start_worker(void (*entry)(void))
    210 {
    211     uint32_t saved = g_eax;
    212     create_thread(0, 0, 0, 0, 0, 1, 0, entry);
    213     if (g_eax == UINT32_MAX) abort();
    214     g_eax = saved;
    215 }
    216 
    217 static void begin_thread(void)
    218 {
    219     uint32_t stack_size = MEM32(g_esp + 8), entry = MEM32(g_esp + 12);
    220     uint32_t argument = MEM32(g_esp + 16), flags = MEM32(g_esp + 20);
    221     uint32_t id = MEM32(g_esp + 24);
    222     if (MEM32(g_esp + 4) || (flags & ~4u)) {
    223         fprintf(stderr, "Thread security attributes/creation flags %08x are not implemented\n", flags);
    224         abort();
    225     }
    226     g_esp += 4; /* _beginthreadex is cdecl; its entry function is stdcall. */
    227     create_thread(stack_size, entry, argument, flags, id, 0, 0, NULL);
    228 }
    229 
    230 static void begin_automatic_thread(void)
    231 {
    232     uint32_t entry = MEM32(g_esp + 4), stack_size = MEM32(g_esp + 8), argument = MEM32(g_esp + 12);
    233     g_esp += 4;
    234     create_thread(stack_size, entry, argument, 0, 0, 1, 0, NULL);
    235 }
    236 
    237 static void set_timer(void)
    238 {
    239     uint32_t delay = MEM32(g_esp + 4), entry = MEM32(g_esp + 12);
    240     uint32_t argument = MEM32(g_esp + 16), flags = MEM32(g_esp + 20);
    241     g_esp += 24;
    242     g_eax = 0;
    243     if (flags) {
    244         fprintf(stderr, "Unsupported multimedia timer flags %08x\n", flags);
    245         exit(3);
    246     }
    247     if (!delay || !entry) return;
    248     create_thread(0, entry, argument, 0, 0, 0, delay, NULL);
    249 }
    250 
    251 static void kill_timer(void)
    252 {
    253     uint32_t handle = MEM32(g_esp + 4);
    254     g_esp += 8;
    255     g_eax = 11; /* MMSYSERR_INVALPARAM */
    256     check_pthread(pthread_mutex_lock(&mutex));
    257     for (struct guest_thread *thread = threads; thread; thread = thread->next) {
    258         if (thread->handle != handle || !thread->timer_delay || thread->cancelled) continue;
    259         thread->cancelled = 1;
    260         check_pthread(pthread_cond_broadcast(&thread->changed));
    261         g_eax = 0;
    262         break;
    263     }
    264     check_pthread(pthread_mutex_unlock(&mutex));
    265 }
    266 
    267 static void end_thread(void)
    268 {
    269     if (!current_thread) {
    270         fprintf(stderr, "_endthreadex outside a created guest thread\n");
    271         abort();
    272     }
    273     current_thread->exit_code = MEM32(g_esp + 4);
    274     pthread_exit(NULL);
    275 }
    276 
    277 static void end_automatic_thread(void)
    278 {
    279     if (!current_thread) {
    280         fprintf(stderr, "_endthread outside a created guest thread\n");
    281         abort();
    282     }
    283     current_thread->exit_code = 0;
    284     current_thread->automatic = 1;
    285     pthread_exit(NULL);
    286 }
    287 
    288 static void close_thread(void)
    289 {
    290     check_pthread(pthread_mutex_lock(&mutex));
    291     struct guest_thread *thread = find_thread_locked(MEM32(g_esp + 4));
    292     g_eax = thread != NULL;
    293     if (thread) close_thread_locked(thread);
    294     check_pthread(pthread_mutex_unlock(&mutex));
    295     g_esp += 8;
    296 }
    297 
    298 static void wait_thread(void)
    299 {
    300     uint32_t milliseconds = MEM32(g_esp + 8);
    301     check_pthread(pthread_mutex_lock(&mutex));
    302     struct guest_thread *thread = find_thread_locked(MEM32(g_esp + 4));
    303     g_eax = UINT32_MAX;
    304     if (thread) {
    305         ++thread->references;
    306         struct timespec deadline;
    307         if (clock_gettime(CLOCK_MONOTONIC, &deadline)) abort();
    308         deadline.tv_sec += milliseconds / 1000;
    309         deadline.tv_nsec += (milliseconds % 1000) * 1000000;
    310         if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; }
    311         int error = 0;
    312         while (!thread->complete && !error) {
    313             error = milliseconds == UINT32_MAX ? pthread_cond_wait(&thread->changed, &mutex) :
    314                 pthread_cond_timedwait(&thread->changed, &mutex, &deadline);
    315             if (error != ETIMEDOUT) check_pthread(error);
    316         }
    317         g_eax = thread->complete ? 0 : 258;
    318         release_thread_locked(thread);
    319     }
    320     check_pthread(pthread_mutex_unlock(&mutex));
    321     g_esp += 12;
    322 }
    323 
    324 static void get_exit_code(void)
    325 {
    326     check_pthread(pthread_mutex_lock(&mutex));
    327     struct guest_thread *thread = find_thread_locked(MEM32(g_esp + 4));
    328     g_eax = thread != NULL;
    329     if (thread) MEM32(MEM32(g_esp + 8)) = thread->complete ? thread->exit_code : 259;
    330     check_pthread(pthread_mutex_unlock(&mutex));
    331     g_esp += 12;
    332 }
    333 
    334 static void resume_thread(void)
    335 {
    336     check_pthread(pthread_mutex_lock(&mutex));
    337     struct guest_thread *thread = find_thread_locked(MEM32(g_esp + 4));
    338     g_eax = UINT32_MAX;
    339     if (thread) {
    340         g_eax = thread->suspended;
    341         thread->suspended = 0;
    342         check_pthread(pthread_cond_broadcast(&thread->changed));
    343     }
    344     check_pthread(pthread_mutex_unlock(&mutex));
    345     g_esp += 8;
    346 }
    347 
    348 recomp_func_t linux_thread_lookup(const char *name)
    349 {
    350     if (!strcmp(name, "timeSetEvent")) return set_timer;
    351     if (!strcmp(name, "timeKillEvent")) return kill_timer;
    352     if (!strcmp(name, "_beginthreadex")) return begin_thread;
    353     if (!strcmp(name, "_endthreadex")) return end_thread;
    354     if (!strcmp(name, "_beginthread")) return begin_automatic_thread;
    355     if (!strcmp(name, "_endthread")) return end_automatic_thread;
    356     if (!strcmp(name, "CloseHandle")) return close_thread;
    357     if (!strcmp(name, "WaitForSingleObject")) return wait_thread;
    358     if (!strcmp(name, "GetExitCodeThread")) return get_exit_code;
    359     if (!strcmp(name, "ResumeThread")) return resume_thread;
    360     return NULL;
    361 }