ServerThreadContext.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. #if ENABLE_OOP_NATIVE_CODEGEN
  7. #include "JITServer/JITServer.h"
  8. ServerThreadContext::ServerThreadContext(ThreadContextDataIDL* data, ProcessContext* processContext) :
  9. m_threadContextData(*data),
  10. m_refCount(0),
  11. m_numericPropertyBV(nullptr),
  12. m_preReservedSectionAllocator(processContext->processHandle),
  13. m_sectionAllocator(processContext->processHandle),
  14. m_thunkPageAllocators(nullptr, /* allocXData */ false, &m_sectionAllocator, nullptr, processContext->processHandle),
  15. m_codePageAllocators(nullptr, ALLOC_XDATA, &m_sectionAllocator, &m_preReservedSectionAllocator, processContext->processHandle),
  16. #if defined(_CONTROL_FLOW_GUARD) && !defined(_M_ARM)
  17. m_jitThunkEmitter(this, &m_sectionAllocator, processContext->processHandle),
  18. #endif
  19. m_codeGenAlloc(nullptr, nullptr, this, &m_codePageAllocators, processContext->processHandle),
  20. m_pageAlloc(nullptr, Js::Configuration::Global.flags, PageAllocatorType_BGJIT,
  21. AutoSystemInfo::Data.IsLowMemoryProcess() ?
  22. PageAllocator::DefaultLowMaxFreePageCount :
  23. PageAllocator::DefaultMaxFreePageCount
  24. ),
  25. processContext(processContext)
  26. {
  27. m_pid = GetProcessId(processContext->processHandle);
  28. #if !TARGET_64 && _CONTROL_FLOW_GUARD
  29. m_codeGenAlloc.canCreatePreReservedSegment = data->allowPrereserveAlloc != FALSE;
  30. #endif
  31. m_numericPropertyBV = HeapNew(BVSparse<HeapAllocator>, &HeapAllocator::Instance);
  32. }
  33. ServerThreadContext::~ServerThreadContext()
  34. {
  35. processContext->Release();
  36. if (this->m_numericPropertyBV != nullptr)
  37. {
  38. HeapDelete(m_numericPropertyBV);
  39. this->m_numericPropertyBV = nullptr;
  40. }
  41. }
  42. PreReservedSectionAllocWrapper *
  43. ServerThreadContext::GetPreReservedSectionAllocator()
  44. {
  45. return &m_preReservedSectionAllocator;
  46. }
  47. intptr_t
  48. ServerThreadContext::GetBailOutRegisterSaveSpaceAddr() const
  49. {
  50. return static_cast<intptr_t>(m_threadContextData.bailOutRegisterSaveSpaceAddr);
  51. }
  52. ptrdiff_t
  53. ServerThreadContext::GetChakraBaseAddressDifference() const
  54. {
  55. return GetRuntimeChakraBaseAddress() - (intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr();
  56. }
  57. ptrdiff_t
  58. ServerThreadContext::GetCRTBaseAddressDifference() const
  59. {
  60. return GetRuntimeCRTBaseAddress() - GetJITCRTBaseAddress();
  61. }
  62. intptr_t
  63. ServerThreadContext::GetDisableImplicitFlagsAddr() const
  64. {
  65. return static_cast<intptr_t>(m_threadContextData.disableImplicitFlagsAddr);
  66. }
  67. intptr_t
  68. ServerThreadContext::GetImplicitCallFlagsAddr() const
  69. {
  70. return static_cast<intptr_t>(m_threadContextData.implicitCallFlagsAddr);
  71. }
  72. #if defined(ENABLE_SIMDJS) && (defined(_M_IX86) || defined(_M_X64))
  73. intptr_t
  74. ServerThreadContext::GetSimdTempAreaAddr(uint8 tempIndex) const
  75. {
  76. Assert(tempIndex < SIMD_TEMP_SIZE);
  77. return m_threadContextData.simdTempAreaBaseAddr + tempIndex * sizeof(_x86_SIMDValue);
  78. }
  79. #endif
  80. intptr_t
  81. ServerThreadContext::GetThreadStackLimitAddr() const
  82. {
  83. return static_cast<intptr_t>(m_threadContextData.threadStackLimitAddr);
  84. }
  85. size_t
  86. ServerThreadContext::GetScriptStackLimit() const
  87. {
  88. return static_cast<size_t>(m_threadContextData.scriptStackLimit);
  89. }
  90. bool
  91. ServerThreadContext::IsThreadBound() const
  92. {
  93. return m_threadContextData.isThreadBound != FALSE;
  94. }
  95. HANDLE
  96. ServerThreadContext::GetProcessHandle() const
  97. {
  98. return this->processContext->processHandle;
  99. }
  100. CustomHeap::OOPCodePageAllocators *
  101. ServerThreadContext::GetThunkPageAllocators()
  102. {
  103. return &m_thunkPageAllocators;
  104. }
  105. CustomHeap::OOPCodePageAllocators *
  106. ServerThreadContext::GetCodePageAllocators()
  107. {
  108. return &m_codePageAllocators;
  109. }
  110. SectionAllocWrapper *
  111. ServerThreadContext::GetSectionAllocator()
  112. {
  113. return &m_sectionAllocator;
  114. }
  115. OOPCodeGenAllocators *
  116. ServerThreadContext::GetCodeGenAllocators()
  117. {
  118. return &m_codeGenAlloc;
  119. }
  120. #if defined(_CONTROL_FLOW_GUARD) && !defined(_M_ARM)
  121. OOPJITThunkEmitter *
  122. ServerThreadContext::GetJITThunkEmitter()
  123. {
  124. return &m_jitThunkEmitter;
  125. }
  126. #endif
  127. intptr_t
  128. ServerThreadContext::GetRuntimeChakraBaseAddress() const
  129. {
  130. return this->processContext->chakraBaseAddress;
  131. }
  132. intptr_t
  133. ServerThreadContext::GetRuntimeCRTBaseAddress() const
  134. {
  135. return this->processContext->crtBaseAddress;
  136. }
  137. /* static */
  138. intptr_t
  139. ServerThreadContext::GetJITCRTBaseAddress()
  140. {
  141. return (intptr_t)AutoSystemInfo::Data.GetCRTHandle();
  142. }
  143. PageAllocator *
  144. ServerThreadContext::GetForegroundPageAllocator()
  145. {
  146. return &m_pageAlloc;
  147. }
  148. bool
  149. ServerThreadContext::IsNumericProperty(Js::PropertyId propertyId)
  150. {
  151. if (propertyId >= 0 && Js::IsInternalPropertyId(propertyId))
  152. {
  153. return Js::InternalPropertyRecords::GetInternalPropertyName(propertyId)->IsNumeric();
  154. }
  155. bool found = false;
  156. {
  157. AutoCriticalSection lock(&m_cs);
  158. found = m_numericPropertyBV->Test(propertyId) != FALSE;
  159. }
  160. return found;
  161. }
  162. void
  163. ServerThreadContext::UpdateNumericPropertyBV(BVSparseNode * newProps)
  164. {
  165. AutoCriticalSection lock(&m_cs);
  166. m_numericPropertyBV->CopyFromNode(newProps);
  167. }
  168. void ServerThreadContext::AddRef()
  169. {
  170. InterlockedExchangeAdd(&m_refCount, (uint)1);
  171. }
  172. void ServerThreadContext::Release()
  173. {
  174. InterlockedExchangeSubtract(&m_refCount, (uint)1);
  175. if (m_isClosed && m_refCount == 0)
  176. {
  177. HeapDelete(this);
  178. }
  179. }
  180. void ServerThreadContext::Close()
  181. {
  182. this->m_isClosed = true;
  183. #ifdef STACK_BACK_TRACE
  184. ServerContextManager::RecordCloseContext(this);
  185. #endif
  186. }
  187. ProcessContext::ProcessContext(HANDLE processHandle, intptr_t chakraBaseAddress, intptr_t crtBaseAddress) :
  188. processHandle(processHandle),
  189. chakraBaseAddress(chakraBaseAddress),
  190. crtBaseAddress(crtBaseAddress),
  191. refCount(0)
  192. {
  193. }
  194. ProcessContext::~ProcessContext()
  195. {
  196. CloseHandle(processHandle);
  197. }
  198. void ProcessContext::AddRef()
  199. {
  200. InterlockedExchangeAdd(&this->refCount, 1);
  201. }
  202. void ProcessContext::Release()
  203. {
  204. InterlockedExchangeSubtract(&this->refCount, 1);
  205. }
  206. bool ProcessContext::HasRef()
  207. {
  208. return this->refCount != 0;
  209. }
  210. #endif