OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * NaCl Server Runtime user thread state. | 8 * NaCl Server Runtime user thread state. |
9 */ | 9 */ |
10 | 10 |
11 #include <string.h> | 11 #include <string.h> |
12 | 12 |
13 #include "native_client/src/shared/platform/aligned_malloc.h" | 13 #include "native_client/src/shared/platform/aligned_malloc.h" |
14 #include "native_client/src/shared/platform/nacl_check.h" | 14 #include "native_client/src/shared/platform/nacl_check.h" |
15 #include "native_client/src/shared/platform/nacl_exit.h" | 15 #include "native_client/src/shared/platform/nacl_exit.h" |
16 #include "native_client/src/shared/platform/nacl_sync_checked.h" | 16 #include "native_client/src/shared/platform/nacl_sync_checked.h" |
17 | 17 |
18 #include "native_client/src/trusted/service_runtime/arch/sel_ldr_arch.h" | 18 #include "native_client/src/trusted/service_runtime/arch/sel_ldr_arch.h" |
19 #include "native_client/src/trusted/service_runtime/nacl_desc_effector_ldr.h" | 19 #include "native_client/src/trusted/service_runtime/nacl_desc_effector_ldr.h" |
20 #include "native_client/src/trusted/service_runtime/nacl_globals.h" | 20 #include "native_client/src/trusted/service_runtime/nacl_globals.h" |
21 #include "native_client/src/trusted/service_runtime/nacl_tls.h" | 21 #include "native_client/src/trusted/service_runtime/nacl_tls.h" |
22 #include "native_client/src/trusted/service_runtime/nacl_switch_to_app.h" | 22 #include "native_client/src/trusted/service_runtime/nacl_switch_to_app.h" |
23 #include "native_client/src/trusted/service_runtime/nacl_stack_safety.h" | 23 #include "native_client/src/trusted/service_runtime/nacl_stack_safety.h" |
24 #include "native_client/src/trusted/service_runtime/nacl_syscall_common.h" | 24 #include "native_client/src/trusted/service_runtime/nacl_syscall_common.h" |
| 25 #include "native_client/src/trusted/service_runtime/osx/mach_thread_map.h" |
25 | 26 |
26 | 27 |
27 void WINAPI NaClAppThreadLauncher(void *state) { | 28 void WINAPI NaClAppThreadLauncher(void *state) { |
28 struct NaClAppThread *natp = (struct NaClAppThread *) state; | 29 struct NaClAppThread *natp = (struct NaClAppThread *) state; |
29 uint32_t thread_idx; | 30 uint32_t thread_idx; |
30 NaClLog(4, "NaClAppThreadLauncher: entered\n"); | 31 NaClLog(4, "NaClAppThreadLauncher: entered\n"); |
31 | 32 |
32 NaClSignalStackRegister(natp->signal_stack); | 33 NaClSignalStackRegister(natp->signal_stack); |
33 | 34 |
34 NaClLog(4, " natp = 0x%016"NACL_PRIxPTR"\n", (uintptr_t) natp); | 35 NaClLog(4, " natp = 0x%016"NACL_PRIxPTR"\n", (uintptr_t) natp); |
35 NaClLog(4, " prog_ctr = 0x%016"NACL_PRIxNACL_REG"\n", natp->user.prog_ctr); | 36 NaClLog(4, " prog_ctr = 0x%016"NACL_PRIxNACL_REG"\n", natp->user.prog_ctr); |
36 NaClLog(4, "stack_ptr = 0x%016"NACL_PRIxPTR"\n", | 37 NaClLog(4, "stack_ptr = 0x%016"NACL_PRIxPTR"\n", |
37 NaClGetThreadCtxSp(&natp->user)); | 38 NaClGetThreadCtxSp(&natp->user)); |
38 | 39 |
39 thread_idx = NaClGetThreadIdx(natp); | 40 thread_idx = NaClGetThreadIdx(natp); |
40 CHECK(0 < thread_idx); | 41 CHECK(0 < thread_idx); |
41 CHECK(thread_idx < NACL_THREAD_MAX); | 42 CHECK(thread_idx < NACL_THREAD_MAX); |
42 NaClTlsSetCurrentThread(natp); | 43 NaClTlsSetCurrentThread(natp); |
43 nacl_user[thread_idx] = &natp->user; | 44 nacl_user[thread_idx] = &natp->user; |
44 #if NACL_WINDOWS | 45 #if NACL_WINDOWS |
45 nacl_thread_ids[thread_idx] = GetCurrentThreadId(); | 46 nacl_thread_ids[thread_idx] = GetCurrentThreadId(); |
| 47 #elif NACL_OSX |
| 48 NaClSetCurrentMachThreadForThreadIndex(thread_idx); |
46 #endif | 49 #endif |
47 | 50 |
48 /* | 51 /* |
49 * We have to hold the threads_mu lock until after thread_num field | 52 * We have to hold the threads_mu lock until after thread_num field |
50 * in this thread has been initialized. All other threads can only | 53 * in this thread has been initialized. All other threads can only |
51 * find and examine this natp through the threads table, so the fact | 54 * find and examine this natp through the threads table, so the fact |
52 * that natp is not consistent (no thread_num) will not be visible. | 55 * that natp is not consistent (no thread_num) will not be visible. |
53 */ | 56 */ |
54 NaClXMutexLock(&natp->nap->threads_mu); | 57 NaClXMutexLock(&natp->nap->threads_mu); |
55 natp->thread_num = NaClAddThreadMu(natp->nap, natp); | 58 natp->thread_num = NaClAddThreadMu(natp->nap, natp); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 */ | 118 */ |
116 thread_idx = NaClGetThreadIdx(natp); | 119 thread_idx = NaClGetThreadIdx(natp); |
117 /* | 120 /* |
118 * On x86-64 and ARM, clearing nacl_user entry ensures that we will | 121 * On x86-64 and ARM, clearing nacl_user entry ensures that we will |
119 * fault if another syscall is made with this thread_idx. In | 122 * fault if another syscall is made with this thread_idx. In |
120 * particular, thread_idx 0 is never used. | 123 * particular, thread_idx 0 is never used. |
121 */ | 124 */ |
122 nacl_user[thread_idx] = NULL; | 125 nacl_user[thread_idx] = NULL; |
123 #if NACL_WINDOWS | 126 #if NACL_WINDOWS |
124 nacl_thread_ids[thread_idx] = 0; | 127 nacl_thread_ids[thread_idx] = 0; |
| 128 #elif NACL_OSX |
| 129 NaClClearMachThreadForThreadIndex(thread_idx); |
125 #endif | 130 #endif |
126 NaClTlsSetCurrentThread(NULL); | 131 NaClTlsSetCurrentThread(NULL); |
127 | 132 |
128 NaClLog(3, " removing thread from thread table\n"); | 133 NaClLog(3, " removing thread from thread table\n"); |
129 /* Deallocate the ID natp->thread_num. */ | 134 /* Deallocate the ID natp->thread_num. */ |
130 NaClRemoveThreadMu(nap, natp->thread_num); | 135 NaClRemoveThreadMu(nap, natp->thread_num); |
131 NaClLog(3, " unlocking thread\n"); | 136 NaClLog(3, " unlocking thread\n"); |
132 NaClXMutexUnlock(&natp->mu); | 137 NaClXMutexUnlock(&natp->mu); |
133 NaClLog(3, " unlocking thread table\n"); | 138 NaClLog(3, " unlocking thread table\n"); |
134 NaClXMutexUnlock(&nap->threads_mu); | 139 NaClXMutexUnlock(&nap->threads_mu); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 NaClThreadDtor(&natp->host_thread); | 266 NaClThreadDtor(&natp->host_thread); |
262 } | 267 } |
263 free(natp->suspended_registers); | 268 free(natp->suspended_registers); |
264 NaClMutexDtor(&natp->suspend_mu); | 269 NaClMutexDtor(&natp->suspend_mu); |
265 NaClSignalStackFree(natp->signal_stack); | 270 NaClSignalStackFree(natp->signal_stack); |
266 natp->signal_stack = NULL; | 271 natp->signal_stack = NULL; |
267 NaClTlsFree(natp); | 272 NaClTlsFree(natp); |
268 NaClMutexDtor(&natp->mu); | 273 NaClMutexDtor(&natp->mu); |
269 NaClAlignedFree(natp); | 274 NaClAlignedFree(natp); |
270 } | 275 } |
OLD | NEW |