Chromium Code Reviews| 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 #include "native_client/src/include/nacl_platform.h" | 7 #include "native_client/src/include/nacl_platform.h" |
| 8 #include "native_client/src/include/nacl_macros.h" | 8 #include "native_client/src/include/nacl_macros.h" |
| 9 #include "native_client/src/include/portability.h" | 9 #include "native_client/src/include/portability.h" |
| 10 #include "native_client/src/shared/platform/nacl_check.h" | 10 #include "native_client/src/shared/platform/nacl_check.h" |
| 11 #include "native_client/src/shared/platform/nacl_sync_checked.h" | 11 #include "native_client/src/shared/platform/nacl_sync_checked.h" |
| 12 #include "native_client/src/trusted/service_runtime/arch/arm/sel_ldr_arm.h" | 12 #include "native_client/src/trusted/service_runtime/arch/mips/sel_ldr_mips.h" |
| 13 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h" | 13 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h" |
| 14 #include "native_client/src/trusted/service_runtime/nacl_globals.h" | 14 #include "native_client/src/trusted/service_runtime/nacl_globals.h" |
| 15 #include "native_client/src/trusted/service_runtime/nacl_tls.h" | 15 #include "native_client/src/trusted/service_runtime/nacl_tls.h" |
| 16 #include "native_client/src/trusted/service_runtime/sel_ldr.h" | 16 #include "native_client/src/trusted/service_runtime/sel_ldr.h" |
| 17 #include "native_client/src/trusted/service_runtime/sel_memory.h" | 17 #include "native_client/src/trusted/service_runtime/sel_memory.h" |
| 18 | 18 |
| 19 static struct NaClMutex gNaClTlsMu; | 19 static struct NaClMutex gNaClTlsMu; |
| 20 static int gNaClThreadIdxInUse[NACL_THREAD_MAX]; /* bool */ | 20 static int gNaClThreadIdxInUse[NACL_THREAD_MAX]; /* bool */ |
| 21 | 21 |
| 22 /* | 22 /* |
| 23 * This holds the index of the current thread. | 23 * This holds the index of the current thread. |
| 24 * This is also used directly in nacl_syscall.S (NaClSyscallSeg). | 24 * This is also used directly in nacl_syscall.S (NaClSyscallSeg). |
| 25 */ | 25 */ |
| 26 __thread uint32_t gNaClThreadIdx = NACL_TLS_INDEX_INVALID; | 26 __thread uint32_t gNaClThreadIdx = NACL_TLS_INDEX_INVALID; |
| 27 | 27 |
| 28 uint32_t NaClTlsGetIdx(void) { | 28 uint32_t NaClTlsGetIdx(void) { |
| 29 return gNaClThreadIdx; | 29 return gNaClThreadIdx; |
| 30 } | 30 } |
| 31 | 31 |
| 32 void NaClTlsSetIdx(uint32_t tls_idx) { | 32 void NaClTlsSetIdx(uint32_t tls_idx) { |
| 33 gNaClThreadIdx = tls_idx; | 33 gNaClThreadIdx = tls_idx; |
| 34 } | 34 } |
| 35 | 35 |
| 36 uint32_t NaClGetThreadIdx(struct NaClAppThread *natp) { | 36 uint32_t NaClGetThreadIdx(struct NaClAppThread *natp) { |
| 37 return natp->user.tls_idx; | 37 return natp->user.tls_idx; |
| 38 } | 38 } |
| 39 | 39 |
|
Mark Seaborn
2012/09/08 02:43:14
Since this file shows up as a comparison against t
petarj
2012/09/11 16:58:13
Sure, I have just done that. Note that this file i
| |
| 40 | |
| 41 int NaClTlsInit() { | 40 int NaClTlsInit() { |
| 42 size_t i; | 41 size_t i; |
| 43 | 42 |
| 44 NaClLog(2, "NaClTlsInit\n"); | 43 NaClLog(2, "NaClTlsInit\n"); |
| 45 | 44 |
| 46 for (i = 0; i < NACL_ARRAY_SIZE(gNaClThreadIdxInUse); i++) { | 45 for (i = 0; i < NACL_ARRAY_SIZE(gNaClThreadIdxInUse); i++) { |
| 47 gNaClThreadIdxInUse[i] = 0; | 46 gNaClThreadIdxInUse[i] = 0; |
| 48 } | 47 } |
| 49 if (!NaClMutexCtor(&gNaClTlsMu)) { | 48 if (!NaClMutexCtor(&gNaClTlsMu)) { |
| 50 NaClLog(LOG_WARNING, | 49 NaClLog(LOG_WARNING, |
| 51 "NaClTlsInit: gNaClTlsMu initialization failed\n"); | 50 "NaClTlsInit: gNaClTlsMu initialization failed\n"); |
| 52 return 0; | 51 return 0; |
| 53 } | 52 } |
| 54 | 53 |
| 55 return 1; | 54 return 1; |
| 56 } | 55 } |
| 57 | 56 |
| 58 | |
| 59 void NaClTlsFini() { | 57 void NaClTlsFini() { |
| 60 NaClLog(2, "NaClTlsFini\n"); | 58 NaClLog(2, "NaClTlsFini\n"); |
| 61 NaClMutexDtor(&gNaClTlsMu); | 59 NaClMutexDtor(&gNaClTlsMu); |
| 62 } | 60 } |
| 63 | 61 |
| 64 static int NaClThreadIdxAllocate() { | 62 static int NaClThreadIdxAllocate() { |
| 65 int i; | 63 int i; |
| 66 | 64 |
| 67 NaClXMutexLock(&gNaClTlsMu); | 65 NaClXMutexLock(&gNaClTlsMu); |
| 68 for (i = 0; i < NACL_THREAD_MAX; i++) { | 66 for (i = 0; i < NACL_THREAD_MAX; i++) { |
| 69 if (!gNaClThreadIdxInUse[i]) { | 67 if (!gNaClThreadIdxInUse[i]) { |
| 70 gNaClThreadIdxInUse[i] = 1; | 68 gNaClThreadIdxInUse[i] = 1; |
| 71 break; | 69 break; |
| 72 } | 70 } |
| 73 } | 71 } |
| 74 NaClXMutexUnlock(&gNaClTlsMu); | 72 NaClXMutexUnlock(&gNaClTlsMu); |
| 75 | 73 |
| 76 if (NACL_THREAD_MAX != i) { | 74 if (NACL_THREAD_MAX != i) { |
| 77 return i; | 75 return i; |
| 78 } | 76 } |
| 79 | 77 |
| 80 NaClLog(LOG_ERROR, "NaClThreadIdxAllocate: no more slots for a thread\n"); | 78 NaClLog(LOG_ERROR, "NaClThreadIdxAllocate: no more slots for a thread\n"); |
| 81 return -1; | 79 return -1; |
| 82 } | 80 } |
| 83 | 81 |
| 84 | |
| 85 /* | 82 /* |
| 86 * Allocation does not mean we can set gNaClThreadIdx, since we are not | 83 * Allocation does not mean we can set gNaClThreadIdx, since we are not |
| 87 * that thread. Setting it must wait until the thread actually launches. | 84 * that thread. Setting it must wait until the thread actually launches. |
| 88 */ | 85 */ |
| 89 uint32_t NaClTlsAllocate(struct NaClAppThread *natp) { | 86 uint32_t NaClTlsAllocate(struct NaClAppThread *natp) { |
| 90 int idx = NaClThreadIdxAllocate(); | 87 int idx = NaClThreadIdxAllocate(); |
| 91 | 88 |
| 92 NaClLog(2, "NaClTlsAllocate: $tp %x idx %d\n", natp->tls_values.tls1, idx); | 89 NaClLog(2, "NaClTlsAllocate: $tp %x idx %d\n", natp->tls_values.tls1, idx); |
| 93 if (-1 == idx) { | 90 if (-1 == idx) { |
| 94 NaClLog(LOG_FATAL, | 91 NaClLog(LOG_FATAL, |
| 95 "NaClTlsAllocate: thread limit reached\n"); | 92 "NaClTlsAllocate: thread limit reached\n"); |
| 96 return NACL_TLS_INDEX_INVALID; | 93 return NACL_TLS_INDEX_INVALID; |
| 97 } | 94 } |
| 98 | 95 |
| 99 natp->user.r9 = natp->tls_values.tls1; | 96 natp->user.t8 = natp->tls_values.tls1; |
| 100 | 97 |
| 101 /* | 98 /* |
| 102 * Bias by 1: successful return value is never 0. | 99 * Bias by 1: successful return value is never 0. |
| 103 */ | 100 */ |
| 104 return idx + 1; | 101 return idx + 1; |
| 105 } | 102 } |
| 106 | 103 |
| 107 | 104 |
| 108 void NaClTlsFree(struct NaClAppThread *natp) { | 105 void NaClTlsFree(struct NaClAppThread *natp) { |
| 109 uint32_t idx = NaClGetThreadIdx(natp); | 106 uint32_t idx = NaClGetThreadIdx(natp); |
| 110 NaClLog(2, | 107 NaClLog(2, |
| 111 "NaClTlsFree: old idx %d $tp %x\n", | 108 "NaClTlsFree: old idx %d $tp %x\n", |
| 112 idx, natp->user.r9); | 109 idx, natp->user.t8); |
| 113 | 110 |
| 114 NaClXMutexLock(&gNaClTlsMu); | 111 NaClXMutexLock(&gNaClTlsMu); |
| 115 gNaClThreadIdxInUse[idx - 1] = 0; | 112 gNaClThreadIdxInUse[idx - 1] = 0; |
| 116 NaClXMutexUnlock(&gNaClTlsMu); | 113 NaClXMutexUnlock(&gNaClTlsMu); |
| 117 | 114 |
| 118 natp->user.r9 = 0; | 115 natp->user.t8 = 0; |
| 119 } | 116 } |
| 120 | 117 |
| 121 | |
| 122 void NaClTlsChange(struct NaClAppThread *natp) { | 118 void NaClTlsChange(struct NaClAppThread *natp) { |
| 123 NaClLog(2, "NaClTlsChange: $tp %x\n", natp->tls_values.tls1); | 119 NaClLog(2, "NaClTlsChange: $tp %x\n", natp->tls_values.tls1); |
| 124 | 120 |
| 125 natp->user.r9 = natp->tls_values.tls1; | 121 natp->user.t8 = (uint32_t) natp->tls_values.tls1; |
|
Mark Seaborn
2012/09/08 02:43:14
Isn't the cast unnecessary? This is already uint3
petarj
2012/09/11 16:58:13
Not needed (anymore). Done.
| |
| 126 } | 122 } |
| OLD | NEW |