CommonPal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #pragma once
  6. #ifdef _WIN32
  7. #pragma warning(push)
  8. #pragma warning(disable: 4995) /* 'function': name was marked as #pragma deprecated */
  9. // === Windows Header Files ===
  10. #define INC_OLE2 /* for windows.h */
  11. #define CONST_VTABLE /* for objbase.h */
  12. #include <windows.h>
  13. /* Don't want GetObject and GetClassName to be defined to GetObjectW and GetClassNameW */
  14. #undef GetObject
  15. #undef GetClassName
  16. #undef Yield /* winbase.h defines this but we want to use it for Js::OpCode::Yield; it is Win16 legacy, no harm undef'ing it */
  17. #pragma warning(pop)
  18. typedef wchar_t wchar16;
  19. // xplat-todo: get a better name for this macro
  20. #define CH_WSTR(s) L##s
  21. #define get_cpuid __cpuid
  22. #else // !_WIN32
  23. #include "pal.h"
  24. #include "inc/rt/palrt.h"
  25. #include "inc/rt/no_sal2.h"
  26. #include "inc/rt/oaidl.h"
  27. #include <emmintrin.h>
  28. typedef char16_t wchar16;
  29. #define CH_WSTR(s) u##s
  30. // xplat-todo: verify below is correct
  31. #include <cpuid.h>
  32. inline int get_cpuid(int cpuInfo[4], int function_id)
  33. {
  34. return __get_cpuid(
  35. static_cast<unsigned int>(function_id),
  36. reinterpret_cast<unsigned int*>(&cpuInfo[0]),
  37. reinterpret_cast<unsigned int*>(&cpuInfo[1]),
  38. reinterpret_cast<unsigned int*>(&cpuInfo[2]),
  39. reinterpret_cast<unsigned int*>(&cpuInfo[3]));
  40. }
  41. #define _BitScanForward BitScanForward
  42. #define _BitScanForward64 BitScanForward64
  43. #define _BitScanReverse BitScanReverse
  44. #define _BitScanReverse64 BitScanReverse64
  45. #define _bittest BitTest
  46. #define _bittestandset BitTestAndSet
  47. #define _interlockedbittestandset InterlockedBitTestAndSet
  48. #ifdef PAL_STDCPP_COMPAT
  49. // SAL.h doesn't define these if PAL_STDCPP_COMPAT is defined
  50. // Apparently, some C++ headers will conflict with this-
  51. // not sure which ones but stubbing them out for now in linux-
  52. // we can revisit if we do hit a conflict
  53. #define __in _SAL1_Source_(__in, (), _In_)
  54. #define __out _SAL1_Source_(__out, (), _Out_)
  55. #define fclose PAL_fclose
  56. #define fflush PAL_fflush
  57. #define fwprintf PAL_fwprintf
  58. #define wcschr PAL_wcschr
  59. #define wcscmp PAL_wcscmp
  60. #define wcslen PAL_wcslen
  61. #define wcsncmp PAL_wcsncmp
  62. #define wcsrchr PAL_wcsrchr
  63. #define wcsstr PAL_wcsstr
  64. #define wprintf PAL_wprintf
  65. #define stdout PAL_stdout
  66. #endif // PAL_STDCPP_COMPAT
  67. #define FILE PAL_FILE
  68. // These are not available in pal
  69. #define fwprintf_s fwprintf
  70. #define wmemcmp wcsncmp
  71. // sprintf_s overloaded in safecrt.h. Not sure why palrt.h redefines sprintf_s.
  72. #undef sprintf_s
  73. // PAL LoadLibraryExW not supported
  74. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0
  75. // winerror.h
  76. #define FACILITY_JSCRIPT 2306
  77. #define JSCRIPT_E_CANTEXECUTE _HRESULT_TYPEDEF_(0x89020001L)
  78. #define DISP_E_UNKNOWNINTERFACE _HRESULT_TYPEDEF_(0x80020001L)
  79. #define DISP_E_MEMBERNOTFOUND _HRESULT_TYPEDEF_(0x80020003L)
  80. #define DISP_E_UNKNOWNNAME _HRESULT_TYPEDEF_(0x80020006L)
  81. #define DISP_E_NONAMEDARGS _HRESULT_TYPEDEF_(0x80020007L)
  82. #define DISP_E_EXCEPTION _HRESULT_TYPEDEF_(0x80020009L)
  83. #define DISP_E_BADINDEX _HRESULT_TYPEDEF_(0x8002000BL)
  84. #define DISP_E_UNKNOWNLCID _HRESULT_TYPEDEF_(0x8002000CL)
  85. #define DISP_E_ARRAYISLOCKED _HRESULT_TYPEDEF_(0x8002000DL)
  86. #define DISP_E_BADPARAMCOUNT _HRESULT_TYPEDEF_(0x8002000EL)
  87. #define DISP_E_PARAMNOTOPTIONAL _HRESULT_TYPEDEF_(0x8002000FL)
  88. #define DISP_E_NOTACOLLECTION _HRESULT_TYPEDEF_(0x80020011L)
  89. #define TYPE_E_DLLFUNCTIONNOTFOUND _HRESULT_TYPEDEF_(0x8002802FL)
  90. #define TYPE_E_TYPEMISMATCH _HRESULT_TYPEDEF_(0x80028CA0L)
  91. #define TYPE_E_OUTOFBOUNDS _HRESULT_TYPEDEF_(0x80028CA1L)
  92. #define TYPE_E_IOERROR _HRESULT_TYPEDEF_(0x80028CA2L)
  93. #define TYPE_E_CANTCREATETMPFILE _HRESULT_TYPEDEF_(0x80028CA3L)
  94. #define TYPE_E_CANTLOADLIBRARY _HRESULT_TYPEDEF_(0x80029C4AL)
  95. #define STG_E_TOOMANYOPENFILES _HRESULT_TYPEDEF_(0x80030004L)
  96. #define STG_E_ACCESSDENIED _HRESULT_TYPEDEF_(0x80030005L)
  97. #define STG_E_INSUFFICIENTMEMORY _HRESULT_TYPEDEF_(0x80030008L)
  98. #define STG_E_NOMOREFILES _HRESULT_TYPEDEF_(0x80030012L)
  99. #define STG_E_DISKISWRITEPROTECTED _HRESULT_TYPEDEF_(0x80030013L)
  100. #define STG_E_READFAULT _HRESULT_TYPEDEF_(0x8003001EL)
  101. #define STG_E_SHAREVIOLATION _HRESULT_TYPEDEF_(0x80030020L)
  102. #define STG_E_LOCKVIOLATION _HRESULT_TYPEDEF_(0x80030021L)
  103. #define STG_E_MEDIUMFULL _HRESULT_TYPEDEF_(0x80030070L)
  104. #define STG_E_INVALIDNAME _HRESULT_TYPEDEF_(0x800300FCL)
  105. #define STG_E_INUSE _HRESULT_TYPEDEF_(0x80030100L)
  106. #define STG_E_NOTCURRENT _HRESULT_TYPEDEF_(0x80030101L)
  107. #define STG_E_CANTSAVE _HRESULT_TYPEDEF_(0x80030103L)
  108. #define REGDB_E_CLASSNOTREG _HRESULT_TYPEDEF_(0x80040154L)
  109. #define MK_E_UNAVAILABLE _HRESULT_TYPEDEF_(0x800401E3L)
  110. #define MK_E_INVALIDEXTENSION _HRESULT_TYPEDEF_(0x800401E6L)
  111. #define MK_E_CANTOPENFILE _HRESULT_TYPEDEF_(0x800401EAL)
  112. #define CO_E_APPNOTFOUND _HRESULT_TYPEDEF_(0x800401F5L)
  113. #define CO_E_APPDIDNTREG _HRESULT_TYPEDEF_(0x800401FEL)
  114. #define GetScode(hr) ((SCODE) (hr))
  115. // activscp.h
  116. #define SCRIPT_E_RECORDED 0x86664004L
  117. // _countof
  118. #if defined _M_X64 || defined _M_ARM || defined _M_ARM64
  119. #define _UNALIGNED __unaligned
  120. #else
  121. #define _UNALIGNED
  122. #endif
  123. #ifdef __cplusplus
  124. extern "C++"
  125. {
  126. template <typename _CountofType, size_t _SizeOfArray>
  127. char(*__countof_helper(_UNALIGNED _CountofType(&_Array)[_SizeOfArray]))[_SizeOfArray];
  128. #define __crt_countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
  129. }
  130. #else
  131. #define __crt_countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
  132. #endif
  133. #ifndef _countof
  134. #define _countof __crt_countof
  135. #endif
  136. // _countof
  137. #define ARRAYSIZE(A) _countof(A)
  138. //
  139. // Singly linked list structure. Can be used as either a list head, or
  140. // as link words.
  141. //
  142. typedef struct _SINGLE_LIST_ENTRY {
  143. struct _SINGLE_LIST_ENTRY *Next;
  144. } SINGLE_LIST_ENTRY, *PSINGLE_LIST_ENTRY;
  145. #if defined(_WIN64)
  146. //
  147. // The type SINGLE_LIST_ENTRY is not suitable for use with SLISTs. For
  148. // WIN64, an entry on an SLIST is required to be 16-byte aligned, while a
  149. // SINGLE_LIST_ENTRY structure has only 8 byte alignment.
  150. //
  151. // Therefore, all SLIST code should use the SLIST_ENTRY type instead of the
  152. // SINGLE_LIST_ENTRY type.
  153. //
  154. #pragma warning(push)
  155. #pragma warning(disable:4324) // structure padded due to align()
  156. typedef struct DECLSPEC_ALIGN(16) _SLIST_ENTRY {
  157. struct _SLIST_ENTRY *Next;
  158. } SLIST_ENTRY, *PSLIST_ENTRY;
  159. #pragma warning(pop)
  160. #else
  161. typedef struct _SINGLE_LIST_ENTRY SLIST_ENTRY, *PSLIST_ENTRY;
  162. #endif // _WIN64
  163. #if defined(_AMD64_)
  164. typedef union DECLSPEC_ALIGN(16) _SLIST_HEADER {
  165. struct { // original struct
  166. ULONGLONG Alignment;
  167. ULONGLONG Region;
  168. } DUMMYSTRUCTNAME;
  169. struct { // x64 16-byte header
  170. ULONGLONG Depth : 16;
  171. ULONGLONG Sequence : 48;
  172. ULONGLONG Reserved : 4;
  173. ULONGLONG NextEntry : 60; // last 4 bits are always 0's
  174. } HeaderX64;
  175. } SLIST_HEADER, *PSLIST_HEADER;
  176. #elif defined(_X86_)
  177. typedef union _SLIST_HEADER {
  178. ULONGLONG Alignment;
  179. struct {
  180. SLIST_ENTRY Next;
  181. WORD Depth;
  182. WORD CpuId;
  183. } DUMMYSTRUCTNAME;
  184. } SLIST_HEADER, *PSLIST_HEADER;
  185. #elif defined(_ARM_)
  186. typedef union _SLIST_HEADER {
  187. ULONGLONG Alignment;
  188. struct {
  189. SLIST_ENTRY Next;
  190. WORD Depth;
  191. WORD Reserved;
  192. } DUMMYSTRUCTNAME;
  193. } SLIST_HEADER, *PSLIST_HEADER;
  194. #endif
  195. PALIMPORT VOID PALAPI InitializeSListHead(IN OUT PSLIST_HEADER ListHead);
  196. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPushEntrySList(IN OUT PSLIST_HEADER ListHead, IN OUT PSLIST_ENTRY ListEntry);
  197. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPopEntrySList(IN OUT PSLIST_HEADER ListHead);
  198. // Use overloaded versions of InterlockedExchangeAdd
  199. #define InterlockedExchangeAdd __InterlockedExchangeAdd
  200. inline long InterlockedExchangeAdd(
  201. IN OUT long volatile *Addend,
  202. IN long Value)
  203. {
  204. return __sync_fetch_and_add(Addend, Value);
  205. }
  206. inline unsigned long InterlockedExchangeAdd(
  207. IN OUT unsigned long volatile *Addend,
  208. IN unsigned long Value)
  209. {
  210. return __sync_fetch_and_add(Addend, Value);
  211. }
  212. inline long InterlockedExchangeSubtract(
  213. IN OUT long volatile *Addend,
  214. IN long Value)
  215. {
  216. return __sync_fetch_and_sub(Addend, Value);
  217. }
  218. inline unsigned long InterlockedExchangeSubtract(
  219. IN OUT unsigned long volatile *Addend,
  220. IN unsigned long Value)
  221. {
  222. return __sync_fetch_and_sub(Addend, Value);
  223. }
  224. // xplat-todo: implement these for JIT and Concurrent/Partial GC
  225. uintptr_t _beginthreadex(
  226. void *security,
  227. unsigned stack_size,
  228. unsigned ( __stdcall *start_address )( void * ),
  229. void *arglist,
  230. unsigned initflag,
  231. unsigned *thrdaddr);
  232. BOOL WINAPI GetModuleHandleEx(
  233. _In_ DWORD dwFlags,
  234. _In_opt_ LPCTSTR lpModuleName,
  235. _Out_ HMODULE *phModule
  236. );
  237. // xplat-todo: implement this function to get the stack bounds of the current
  238. // thread
  239. // For Linux, we could use pthread_getattr_np to get the stack limit (end)
  240. // and then use the stack size to calculate the stack base
  241. int GetCurrentThreadStackBounds(char** stackBase, char** stackEnd);
  242. // xplat-todo: cryptographically secure PRNG?
  243. errno_t rand_s(unsigned int* randomValue);
  244. #endif // _WIN32
  245. // Use intsafe.h for internal builds (currently missing some files with stdint.h)
  246. #if defined(_WIN32) && defined(NTBUILD)
  247. #define ENABLE_INTSAFE_SIGNED_FUNCTIONS 1
  248. #include <intsafe.h>
  249. #else
  250. #include <stdint.h>
  251. #endif
  252. #ifdef _MSC_VER
  253. // ms-specific keywords
  254. #define _ABSTRACT abstract
  255. // MSVC2015 does not support C++11 semantics for `typename QualifiedName` declarations
  256. // outside of template code.
  257. #define _TYPENAME
  258. #else
  259. #define _ABSTRACT
  260. #define _TYPENAME typename
  261. #endif
  262. #if defined(_MSC_VER) && _MSC_VER < 1900
  263. // "noexcept" not supported before VS 2015
  264. #define _NOEXCEPT
  265. #else
  266. #define _NOEXCEPT noexcept
  267. #endif