ServerThreadContext.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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) :
  9. m_autoProcessHandle((HANDLE)data->processHandle),
  10. m_threadContextData(*data),
  11. m_refCount(0),
  12. m_numericPropertyBV(nullptr),
  13. m_preReservedSectionAllocator((HANDLE)data->processHandle),
  14. m_sectionAllocator((HANDLE)data->processHandle),
  15. m_thunkPageAllocators(nullptr, /* allocXData */ false, &m_sectionAllocator, nullptr, (HANDLE)data->processHandle),
  16. m_codePageAllocators(nullptr, ALLOC_XDATA, &m_sectionAllocator, &m_preReservedSectionAllocator, (HANDLE)data->processHandle),
  17. m_codeGenAlloc(nullptr, nullptr, &m_codePageAllocators, (HANDLE)data->processHandle),
  18. m_pageAlloc(nullptr, Js::Configuration::Global.flags, PageAllocatorType_BGJIT,
  19. AutoSystemInfo::Data.IsLowMemoryProcess() ?
  20. PageAllocator::DefaultLowMaxFreePageCount :
  21. PageAllocator::DefaultMaxFreePageCount
  22. ),
  23. m_jitCRTBaseAddress((intptr_t)GetModuleHandle(UCrtC99MathApis::LibraryName))
  24. {
  25. m_pid = GetProcessId((HANDLE)data->processHandle);
  26. #if !_M_X64_OR_ARM64 && _CONTROL_FLOW_GUARD
  27. m_codeGenAlloc.canCreatePreReservedSegment = data->allowPrereserveAlloc != FALSE;
  28. #endif
  29. m_numericPropertyBV = HeapNew(BVSparse<HeapAllocator>, &HeapAllocator::Instance);
  30. }
  31. ServerThreadContext::~ServerThreadContext()
  32. {
  33. if (this->m_numericPropertyBV != nullptr)
  34. {
  35. HeapDelete(m_numericPropertyBV);
  36. this->m_numericPropertyBV = nullptr;
  37. }
  38. }
  39. PreReservedSectionAllocWrapper *
  40. ServerThreadContext::GetPreReservedSectionAllocator()
  41. {
  42. return &m_preReservedSectionAllocator;
  43. }
  44. intptr_t
  45. ServerThreadContext::GetBailOutRegisterSaveSpaceAddr() const
  46. {
  47. return static_cast<intptr_t>(m_threadContextData.bailOutRegisterSaveSpaceAddr);
  48. }
  49. ptrdiff_t
  50. ServerThreadContext::GetChakraBaseAddressDifference() const
  51. {
  52. return GetRuntimeChakraBaseAddress() - (intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr();
  53. }
  54. ptrdiff_t
  55. ServerThreadContext::GetCRTBaseAddressDifference() const
  56. {
  57. return GetRuntimeCRTBaseAddress() - m_jitCRTBaseAddress;
  58. }
  59. intptr_t
  60. ServerThreadContext::GetDisableImplicitFlagsAddr() const
  61. {
  62. return static_cast<intptr_t>(m_threadContextData.disableImplicitFlagsAddr);
  63. }
  64. intptr_t
  65. ServerThreadContext::GetImplicitCallFlagsAddr() const
  66. {
  67. return static_cast<intptr_t>(m_threadContextData.implicitCallFlagsAddr);
  68. }
  69. #if defined(ENABLE_SIMDJS) && (defined(_M_IX86) || defined(_M_X64))
  70. intptr_t
  71. ServerThreadContext::GetSimdTempAreaAddr(uint8 tempIndex) const
  72. {
  73. Assert(tempIndex < SIMD_TEMP_SIZE);
  74. return m_threadContextData.simdTempAreaBaseAddr + tempIndex * sizeof(_x86_SIMDValue);
  75. }
  76. #endif
  77. intptr_t
  78. ServerThreadContext::GetThreadStackLimitAddr() const
  79. {
  80. return static_cast<intptr_t>(m_threadContextData.threadStackLimitAddr);
  81. }
  82. size_t
  83. ServerThreadContext::GetScriptStackLimit() const
  84. {
  85. return static_cast<size_t>(m_threadContextData.scriptStackLimit);
  86. }
  87. bool
  88. ServerThreadContext::IsThreadBound() const
  89. {
  90. return m_threadContextData.isThreadBound != FALSE;
  91. }
  92. HANDLE
  93. ServerThreadContext::GetProcessHandle() const
  94. {
  95. return reinterpret_cast<HANDLE>(m_threadContextData.processHandle);
  96. }
  97. CustomHeap::OOPCodePageAllocators *
  98. ServerThreadContext::GetThunkPageAllocators()
  99. {
  100. return &m_thunkPageAllocators;
  101. }
  102. CustomHeap::OOPCodePageAllocators *
  103. ServerThreadContext::GetCodePageAllocators()
  104. {
  105. return &m_codePageAllocators;
  106. }
  107. SectionAllocWrapper *
  108. ServerThreadContext::GetSectionAllocator()
  109. {
  110. return &m_sectionAllocator;
  111. }
  112. OOPCodeGenAllocators *
  113. ServerThreadContext::GetCodeGenAllocators()
  114. {
  115. return &m_codeGenAlloc;
  116. }
  117. intptr_t
  118. ServerThreadContext::GetRuntimeChakraBaseAddress() const
  119. {
  120. return static_cast<intptr_t>(m_threadContextData.chakraBaseAddress);
  121. }
  122. intptr_t
  123. ServerThreadContext::GetRuntimeCRTBaseAddress() const
  124. {
  125. return static_cast<intptr_t>(m_threadContextData.crtBaseAddress);
  126. }
  127. PageAllocator *
  128. ServerThreadContext::GetForegroundPageAllocator()
  129. {
  130. return &m_pageAlloc;
  131. }
  132. bool
  133. ServerThreadContext::IsNumericProperty(Js::PropertyId propertyId)
  134. {
  135. if (propertyId >= 0 && Js::IsInternalPropertyId(propertyId))
  136. {
  137. return Js::InternalPropertyRecords::GetInternalPropertyName(propertyId)->IsNumeric();
  138. }
  139. bool found = false;
  140. {
  141. AutoCriticalSection lock(&m_cs);
  142. found = m_numericPropertyBV->Test(propertyId) != FALSE;
  143. }
  144. return found;
  145. }
  146. void
  147. ServerThreadContext::UpdateNumericPropertyBV(BVSparseNode * newProps)
  148. {
  149. AutoCriticalSection lock(&m_cs);
  150. m_numericPropertyBV->CopyFromNode(newProps);
  151. }
  152. void ServerThreadContext::AddRef()
  153. {
  154. InterlockedExchangeAdd(&m_refCount, (uint)1);
  155. }
  156. void ServerThreadContext::Release()
  157. {
  158. InterlockedExchangeSubtract(&m_refCount, (uint)1);
  159. if (m_isClosed && m_refCount == 0)
  160. {
  161. HeapDelete(this);
  162. }
  163. }
  164. void ServerThreadContext::Close()
  165. {
  166. this->m_isClosed = true;
  167. #ifdef STACK_BACK_TRACE
  168. ServerContextManager::RecordCloseContext(this);
  169. #endif
  170. }
  171. #endif