Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
9.3 KiB

  1. #ifndef _ASSEMBLY_HPP_
  2. #define _ASSEMBLY_HPP_
  3. // Ruler
  4. // 1 2 3 4 5 6 7 8
  5. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  6. /********************************************************************/
  7. /* */
  8. /* The standard layout. */
  9. /* */
  10. /* The standard layout for 'hpp' files for this code is as */
  11. /* follows: */
  12. /* */
  13. /* 1. Include files. */
  14. /* 2. Constants exported from the class. */
  15. /* 3. Data structures exported from the class. */
  16. /* 4. Forward references to other data structures. */
  17. /* 5. Class specifications (including inline functions). */
  18. /* 6. Additional large inline functions. */
  19. /* */
  20. /* Any portion that is not required is simply omitted. */
  21. /* */
  22. /********************************************************************/
  23. #include "Global.hpp"
  24. /********************************************************************/
  25. /* */
  26. /* Constants exported from the class. */
  27. /* */
  28. /* The assembly constants indicate the location of the thread */
  29. /* local store. */
  30. /* */
  31. /********************************************************************/
  32. #define PcTeb 0x18
  33. #define IDTeb 0x24
  34. #ifdef WINDOWS_95
  35. CONST SBIT32 TebSlot = 0x88;
  36. #else
  37. CONST SBIT32 TebSlot = 0xE10;
  38. #endif
  39. #ifndef ASSEMBLY_PREFETCH_SUPPORT
  40. #define prefetcht0 __asm _emit 0x0f __asm _emit 0x18 __asm _emit 0x08
  41. #define prefetcht1 __asm _emit 0x0f __asm _emit 0x18 __asm _emit 0x10
  42. #define prefetcht2 __asm _emit 0x0f __asm _emit 0x18 __asm _emit 0x18
  43. #define prefetchnta __asm _emit 0x0f __asm _emit 0x18 __asm _emit 0x00
  44. #endif
  45. #pragma warning( disable : 4035 )
  46. /********************************************************************/
  47. /* */
  48. /* Assembly language for ultra high performance. */
  49. /* */
  50. /* We have coded a few functions in assembly language for */
  51. /* ultra high performance. */
  52. /* */
  53. /********************************************************************/
  54. class ASSEMBLY
  55. {
  56. public:
  57. //
  58. // Public inline functions.
  59. //
  60. ASSEMBLY( VOID )
  61. { /* void */ }
  62. STATIC INLINE SBIT32 AtomicAdd
  63. (
  64. VOLATILE SBIT32 *Address,
  65. SBIT32 Value
  66. )
  67. {
  68. #ifdef ASSEMBLY_X86
  69. __asm
  70. {
  71. mov ecx,Address // Load the address.
  72. mov eax,Value // Load the value.
  73. lock xadd dword ptr[ecx],eax // Increment the value.
  74. }
  75. #else
  76. return
  77. (
  78. (SBIT32) InterlockedExchangeAdd
  79. (
  80. ((LPLONG) Address),
  81. ((LONG) Value)
  82. )
  83. );
  84. #endif
  85. }
  86. STATIC INLINE SBIT32 AtomicCompareExchange
  87. (
  88. VOLATILE SBIT32 *Address,
  89. SBIT32 NewValue,
  90. SBIT32 Value
  91. )
  92. {
  93. #ifdef ASSEMBLY_X86
  94. __asm
  95. {
  96. mov ecx,Address // Load the address.
  97. mov edx,NewValue // Load the new value.
  98. mov eax,Value // Load the value.
  99. lock cmpxchg dword ptr[ecx],edx // Update the value.
  100. }
  101. #else
  102. return
  103. (
  104. (SBIT32) InterlockedCompareExchange
  105. (
  106. ((VOID**) Address),
  107. ((VOID*) NewValue),
  108. ((VOID*) Value)
  109. )
  110. );
  111. #endif
  112. }
  113. #ifdef ASSEMBLY_X86
  114. STATIC INLINE SBIT64 AtomicCompareExchange64
  115. (
  116. VOLATILE SBIT64 *Address,
  117. SBIT64 NewValue,
  118. SBIT64 Value
  119. )
  120. {
  121. __asm
  122. {
  123. mov esi, Address // Load the adrress.
  124. mov ebx, dword ptr NewValue[0] // Load the new value.
  125. mov ecx, dword ptr NewValue[4] // Load the new value.
  126. mov eax, dword ptr Value[0] // Load the value.
  127. mov edx, dword ptr Value[4] // Load the value.
  128. lock cmpxchg8b [esi] // Update the value.
  129. }
  130. }
  131. #endif
  132. STATIC INLINE SBIT32 AtomicDecrement( VOLATILE SBIT32 *Address )
  133. {
  134. #ifdef ASSEMBLY_X86
  135. __asm
  136. {
  137. mov ecx,Address // Load the Address.
  138. mov eax,-1 // Load constant.
  139. lock xadd dword ptr[ecx],eax // Decrement value.
  140. dec eax // Correct result.
  141. }
  142. #else
  143. return ((SBIT32) InterlockedDecrement( ((LONG*) Address) ));
  144. #endif
  145. }
  146. STATIC INLINE SBIT32 AtomicExchange
  147. (
  148. VOLATILE SBIT32 *Address,
  149. SBIT32 NewValue
  150. )
  151. {
  152. #ifdef ASSEMBLY_X86
  153. __asm
  154. {
  155. mov ecx,Address // Load the Address.
  156. mov eax,NewValue // Load the value.
  157. lock xchg dword ptr[ecx],eax // Exchange new value.
  158. }
  159. #else
  160. return
  161. (
  162. (SBIT32) InterlockedExchange
  163. (
  164. ((LONG*) Address),
  165. ((LONG) NewValue)
  166. )
  167. );
  168. #endif
  169. }
  170. STATIC INLINE SBIT32 AtomicIncrement( VOLATILE SBIT32 *Address )
  171. {
  172. #ifdef ASSEMBLY_X86
  173. __asm
  174. {
  175. mov ecx,Address // Load the Address.
  176. mov eax,1 // Load constant.
  177. lock xadd dword ptr[ecx],eax // Increment value.
  178. inc eax // Correct result.
  179. }
  180. #else
  181. return ((SBIT32) InterlockedIncrement( ((LONG*) Address) ));
  182. #endif
  183. }
  184. STATIC INLINE SBIT32 GetThreadId( VOID )
  185. {
  186. #ifdef ASSEMBLY_X86
  187. #ifdef ENABLE_NON_STANDARD_ASSEMBLY
  188. __asm
  189. {
  190. mov eax,fs:[PcTeb] // Load TEB base address.
  191. mov eax,dword ptr[eax+IDTeb] // Load thread ID.
  192. }
  193. #else
  194. return ((SBIT32) GetCurrentThreadId());
  195. #endif
  196. #else
  197. return ((SBIT32) GetCurrentThreadId());
  198. #endif
  199. }
  200. STATIC INLINE VOID PrefetchL1( VOID *Address )
  201. {
  202. #ifdef ASSEMBLY_X86
  203. __asm
  204. {
  205. mov eax,Address // Load Address.
  206. #ifdef ASSEMBLY_PREFETCH_SUPPORT
  207. prefetcht0 [eax] // Prefetch into the L1.
  208. #else
  209. prefetcht0 // Prefetch into the L1.
  210. #endif
  211. }
  212. #endif
  213. }
  214. STATIC INLINE VOID PrefetchL2( VOID *Address )
  215. {
  216. #ifdef ASSEMBLY_X86
  217. __asm
  218. {
  219. mov eax,Address // Load Address.
  220. #ifdef ASSEMBLY_PREFETCH_SUPPORT
  221. prefetcht1 [eax] // Prefetch into the L2.
  222. #else
  223. prefetcht1 // Prefetch into the L2.
  224. #endif
  225. }
  226. #endif
  227. }
  228. STATIC INLINE VOID PrefetchL3( VOID *Address )
  229. {
  230. #ifdef ASSEMBLY_X86
  231. __asm
  232. {
  233. mov eax,Address // Load Address.
  234. #ifdef ASSEMBLY_PREFETCH_SUPPORT
  235. prefetcht2 [eax] // Prefetch into the L3.
  236. #else
  237. prefetcht2 // Prefetch into the L3.
  238. #endif
  239. }
  240. #endif
  241. }
  242. STATIC INLINE VOID PrefetchNta( VOID *Address )
  243. {
  244. #ifdef ASSEMBLY_X86
  245. __asm
  246. {
  247. mov eax,Address // Load Address.
  248. #ifdef ASSEMBLY_PREFETCH_SUPPORT
  249. prefetchnta [eax] // Prefetch into the L1.
  250. #else
  251. prefetchnta // Prefetch into the L1.
  252. #endif
  253. }
  254. #endif
  255. }
  256. #ifdef ASSEMBLY_X86
  257. #ifdef ENABLE_NON_STANDARD_ASSEMBLY
  258. STATIC INLINE VOID *GetTlsAddress( SBIT32 TlsOffset )
  259. {
  260. __asm
  261. {
  262. mov eax,TlsOffset // Load TLS offset.
  263. add eax,fs:[PcTeb] // Add TEB base address.
  264. }
  265. }
  266. #endif
  267. #endif
  268. STATIC INLINE VOID *GetTlsValue( SBIT32 TlsIndex,SBIT32 TlsOffset )
  269. {
  270. #ifdef ASSEMBLY_X86
  271. #ifdef ENABLE_NON_STANDARD_ASSEMBLY
  272. __asm
  273. {
  274. mov eax,TlsOffset // Load TLS offset.
  275. add eax,fs:[PcTeb] // Add TEB base address.
  276. mov eax,[eax] // Load TLS value.
  277. }
  278. #else
  279. return (TlsGetValue( ((DWORD) TlsIndex) ));
  280. #endif
  281. #else
  282. return (TlsGetValue( ((DWORD) TlsIndex) ));
  283. #endif
  284. }
  285. STATIC INLINE VOID SetTlsValue
  286. (
  287. SBIT32 TlsIndex,
  288. SBIT32 TlsOffset,
  289. VOID *NewPointer
  290. )
  291. {
  292. #ifdef ASSEMBLY_X86
  293. #ifdef ENABLE_NON_STANDARD_ASSEMBLY
  294. __asm
  295. {
  296. mov eax,TlsOffset // Load TLS offset.
  297. add eax,fs:[PcTeb] // Add TEB base address.
  298. mov edx,NewPointer // Load new TLS value.
  299. mov [eax],edx // Store new TLS value.
  300. }
  301. #else
  302. (VOID) TlsSetValue( ((DWORD) TlsIndex),NewPointer );
  303. #endif
  304. #else
  305. (VOID) TlsSetValue( ((DWORD) TlsIndex),NewPointer );
  306. #endif
  307. }
  308. ~ASSEMBLY( VOID )
  309. { /* void */ }
  310. private:
  311. //
  312. // Disabled operations.
  313. //
  314. ASSEMBLY( CONST ASSEMBLY & Copy );
  315. VOID operator=( CONST ASSEMBLY & Copy );
  316. };
  317. #pragma warning( default : 4035 )
  318. #endif