Source code of Windows XP (NT5)
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.

389 lines
20 KiB

  1. /*++
  2. Copyright (c) 1990-1995 Microsoft Corporation
  3. Module Name:
  4. ndisnt.h
  5. Abstract:
  6. Windows NT Specific macros
  7. Author:
  8. Environment:
  9. Kernel mode, FSD
  10. Revision History:
  11. Nov-95 Jameel Hyder Split up from a monolithic file
  12. --*/
  13. #define Increment(a,b) InterlockedIncrement(a)
  14. #define Decrement(a,b) InterlockedDecrement(a)
  15. #define CURRENT_THREAD PsGetCurrentThread()
  16. #define CURRENT_PROCESSOR KeGetCurrentProcessorNumber()
  17. #define CopyMemory(Destination,Source,Length) RtlCopyMemory(Destination,Source,Length)
  18. #define MoveMemory(Destination,Source,Length) RtlMoveMemory(Destination,Source,Length)
  19. #define ZeroMemory(Destination,Length) RtlZeroMemory(Destination,Length)
  20. #define INITIALIZE_SPIN_LOCK(_L_) KeInitializeSpinLock(_L_)
  21. #define ACQUIRE_SPIN_LOCK(_SpinLock, _pOldIrql) ExAcquireSpinLock(_SpinLock, _pOldIrql)
  22. #define RELEASE_SPIN_LOCK(_SpinLock, _OldIrql) ExReleaseSpinLock(_SpinLock, _OldIrql)
  23. #define ACQUIRE_SPIN_LOCK_DPC(_SpinLock) \
  24. { \
  25. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  26. ExAcquireSpinLockAtDpcLevel(_SpinLock); \
  27. }
  28. #define RELEASE_SPIN_LOCK_DPC(_SpinLock) \
  29. { \
  30. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  31. ExReleaseSpinLockFromDpcLevel(_SpinLock); \
  32. }
  33. #define NDIS_ACQUIRE_SPIN_LOCK(_SpinLock, _pOldIrql) ExAcquireSpinLock(&(_SpinLock)->SpinLock, _pOldIrql)
  34. #define NDIS_RELEASE_SPIN_LOCK(_SpinLock, _OldIrql) ExReleaseSpinLock(&(_SpinLock)->SpinLock, _OldIrql)
  35. #define NDIS_ACQUIRE_SPIN_LOCK_DPC(_SpinLock) \
  36. { \
  37. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  38. ExAcquireSpinLockAtDpcLevel(&(_SpinLock)->SpinLock); \
  39. }
  40. #define NDIS_RELEASE_SPIN_LOCK_DPC(_SpinLock) \
  41. { \
  42. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  43. ExReleaseSpinLockFromDpcLevel(&(_SpinLock)->SpinLock); \
  44. }
  45. #define SET_LOCK_DBG(_M) \
  46. { \
  47. (_M)->LockDbg = (MODULE_NUMBER + __LINE__); \
  48. }
  49. #define SET_LOCK_DBGX(_M) \
  50. { \
  51. (_M)->LockDbgX = (MODULE_NUMBER + __LINE__); \
  52. (_M)->LockThread = CURRENT_THREAD; \
  53. }
  54. #define CLEAR_LOCK_DBG(_M) \
  55. { \
  56. (_M)->LockDbg = 0; \
  57. }
  58. #define CLEAR_LOCK_DBGX(_M) \
  59. { \
  60. (_M)->LockDbgX = 0; \
  61. (_M)->LockThread = NULL; \
  62. }
  63. #define NDIS_ACQUIRE_COMMON_SPIN_LOCK(_M, _pS, _pIrql, _pT) \
  64. { \
  65. ExAcquireSpinLock(_pS, _pIrql); \
  66. ASSERT((_pT) == NULL); \
  67. (_pT) = CURRENT_THREAD; \
  68. SET_LOCK_DBG(_M); \
  69. }
  70. #define NDIS_RELEASE_COMMON_SPIN_LOCK(_M, _pS, _Irql, _pT) \
  71. { \
  72. ASSERT(_pT == CURRENT_THREAD); \
  73. _pT = NULL; \
  74. CLEAR_LOCK_DBG(_M); \
  75. ExReleaseSpinLock(_pS, _Irql); \
  76. }
  77. #define NDIS_ACQUIRE_COMMON_SPIN_LOCK_DPC(_M, _pS, _pT) \
  78. { \
  79. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  80. \
  81. ExAcquireSpinLockAtDpcLevel(_pS); \
  82. ASSERT((_pT) == NULL); \
  83. (_pT) = CURRENT_THREAD; \
  84. SET_LOCK_DBG(_M); \
  85. }
  86. #define NDIS_RELEASE_COMMON_SPIN_LOCK_DPC(_M, _pS, _pT) \
  87. { \
  88. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  89. \
  90. ASSERT(_pT == CURRENT_THREAD); \
  91. _pT = NULL; \
  92. ExReleaseSpinLockFromDpcLevel(_pS); \
  93. CLEAR_LOCK_DBG(_M); \
  94. }
  95. #define NDIS_ACQUIRE_MINIPORT_SPIN_LOCK(_M, _pIrql) \
  96. NDIS_ACQUIRE_COMMON_SPIN_LOCK((_M), &(_M)->Lock, (_pIrql), (_M)->MiniportThread)
  97. #define NDIS_RELEASE_MINIPORT_SPIN_LOCK(_M, _Irql) \
  98. NDIS_RELEASE_COMMON_SPIN_LOCK((_M), &(_M)->Lock, (_Irql), (_M)->MiniportThread)
  99. #define NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M) \
  100. NDIS_ACQUIRE_COMMON_SPIN_LOCK_DPC((_M), &(_M)->Lock, (_M)->MiniportThread)
  101. #define NDIS_RELEASE_MINIPORT_SPIN_LOCK_DPC(_M) \
  102. NDIS_RELEASE_COMMON_SPIN_LOCK_DPC(_M, &(_M)->Lock, (_M)->MiniportThread)
  103. //
  104. // Some macros for platform independence
  105. //
  106. #define NDIS_INTERNAL_STALL(_N_) \
  107. { \
  108. volatile UINT _cnt; \
  109. for (_cnt = 0; _cnt < _N_; _cnt++) \
  110. NOTHING; \
  111. }
  112. #define LOCK_MINIPORT(_M, _L) \
  113. { \
  114. (_L) = 0; \
  115. if ((_M)->LockAcquired == 0) \
  116. { \
  117. (_M)->LockAcquired = 0x01; \
  118. SET_LOCK_DBGX(_M); \
  119. (_L) = 0x01; \
  120. } \
  121. }
  122. #define BLOCK_LOCK_MINIPORT_DPC_L(_M) \
  123. { \
  124. do \
  125. { \
  126. if ((_M)->LockAcquired == 0) \
  127. { \
  128. (_M)->LockAcquired = 0x01; \
  129. SET_LOCK_DBGX(_M); \
  130. break; \
  131. } \
  132. else \
  133. { \
  134. NDIS_RELEASE_MINIPORT_SPIN_LOCK_DPC(_M); \
  135. NDIS_INTERNAL_STALL(50); \
  136. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  137. } \
  138. } while (TRUE); \
  139. }
  140. #define BLOCK_LOCK_MINIPORT_LOCKED(_M, _I) \
  141. { \
  142. do \
  143. { \
  144. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK(_M, &(_I)); \
  145. if ((_M)->LockAcquired == 0) \
  146. { \
  147. (_M)->LockAcquired = 0x01; \
  148. SET_LOCK_DBGX(_M); \
  149. break; \
  150. } \
  151. NDIS_RELEASE_MINIPORT_SPIN_LOCK(_M, _I); \
  152. NDIS_INTERNAL_STALL(50); \
  153. } while (TRUE); \
  154. }
  155. #define UNLOCK_MINIPORT(_M, _L) \
  156. { \
  157. if (_L) \
  158. { \
  159. UNLOCK_MINIPORT_L(_M); \
  160. } \
  161. }
  162. #define UNLOCK_MINIPORT_L(_M) \
  163. { \
  164. ASSERT(MINIPORT_LOCK_ACQUIRED(_M)); \
  165. (_M)->LockAcquired = 0; \
  166. CLEAR_LOCK_DBGX(_M); \
  167. }
  168. #define UNLOCK_MINIPORT_U(_M, _I) \
  169. { \
  170. ASSERT(MINIPORT_LOCK_ACQUIRED(_M)); \
  171. (_M)->LockAcquired = 0; \
  172. CLEAR_LOCK_DBGX(_M); \
  173. \
  174. NDIS_RELEASE_MINIPORT_SPIN_LOCK(_M, _I); \
  175. }
  176. #if TRACK_MEMORY
  177. #define ALLOC_FROM_POOL(_Size_, _Tag_) AllocateM(_Size_, \
  178. (MODULE_NUMBER + __LINE__),\
  179. _Tag_)
  180. #define FREE_POOL(_P_) FreeM(_P_)
  181. #else
  182. #define ALLOC_FROM_POOL(_Size_, _Tag_) ExAllocatePoolWithTag(NonPagedPool, \
  183. _Size_, \
  184. _Tag_)
  185. #define FREE_POOL(_P_) ExFreePool(_P_)
  186. #endif
  187. #define INITIALIZE_WORK_ITEM(_W, _R, _C) ExInitializeWorkItem(_W, _R, _C)
  188. #define XQUEUE_WORK_ITEM(_W, _Q) ExQueueWorkItem(_W, _Q)
  189. #define QUEUE_WORK_ITEM(_W, _Q) KeInsertQueue(&ndisWorkerQueue, &(_W)->List)
  190. #define CURRENT_IRQL KeGetCurrentIrql()
  191. #define RAISE_IRQL_TO_DISPATCH(_pIrql_) KeRaiseIrql(DISPATCH_LEVEL, _pIrql_)
  192. #define LOWER_IRQL(_OldIrql_, _CurIrql_) \
  193. { \
  194. if (_OldIrql_ != _CurIrql_) KeLowerIrql(_OldIrql_); \
  195. }
  196. #define CURRENT_PROCESSOR KeGetCurrentProcessorNumber()
  197. #define INITIALIZE_TIMER(_Timer_) KeInitializeTimer(_Timer_)
  198. #define INITIALIZE_TIMER_EX(_Timer_,_Type_) KeInitializeTimerEx(_Timer_, _Type_)
  199. #define CANCEL_TIMER(_Timer_) KeCancelTimer(_Timer_)
  200. #define SET_TIMER(_Timer_, _Time_, _Dpc_) KeSetTimer(_Timer_, _Time_, _Dpc_)
  201. #define SET_PERIODIC_TIMER(_Timer_, _DueTime_, _PeriodicTime_, _Dpc_) \
  202. KeSetTimerEx(_Timer_, _DueTime_, _PeriodicTime_, _Dpc_)
  203. #define INITIALIZE_EVENT(_pEvent_) KeInitializeEvent(_pEvent_, NotificationEvent, FALSE)
  204. #define SET_EVENT(_pEvent_) KeSetEvent(_pEvent_, 0, FALSE)
  205. #define RESET_EVENT(_pEvent_) KeResetEvent(_pEvent_)
  206. #define INITIALIZE_MUTEX(_M_) KeInitializeMutex(_M_, 0xFFFF)
  207. #define RELEASE_MUTEX(_M_) KeReleaseMutex(_M_, FALSE)
  208. #define WAIT_FOR_OBJECT(_O_, _TO_) KeWaitForSingleObject(_O_, \
  209. Executive,\
  210. KernelMode,\
  211. FALSE, \
  212. _TO_) \
  213. #define GET_CURRENT_TICK_IN_SECONDS(_pCurrTick) \
  214. { \
  215. LARGE_INTEGER _CurrentTick; \
  216. \
  217. KeQueryTickCount(&_CurrentTick); \
  218. /* Convert to seconds */ \
  219. _CurrentTick.QuadPart = (_CurrentTick.QuadPart*ndisTimeIncrement)/(10*1000*1000);\
  220. *(_pCurrTick) = _CurrentTick.LowPart; \
  221. }
  222. #define GET_CURRENT_TICK(_pCurrTick) KeQueryTickCount(_pCurrTick)
  223. #if NOISY_WAIT
  224. #define WAIT_FOR_OBJECT_MSG(_O_, _MSG, _STR) \
  225. { \
  226. NTSTATUS Status; \
  227. LARGE_INTEGER Time; \
  228. \
  229. /* Block 5 seconds */ \
  230. Time.QuadPart = Int32x32To64(5000, -10000); \
  231. do \
  232. { \
  233. Status = KeWaitForSingleObject(_O_, \
  234. Executive, \
  235. KernelMode, \
  236. FALSE, \
  237. &Time); \
  238. if (NT_SUCCESS(Status)) \
  239. { \
  240. break; \
  241. } \
  242. DbgPrint(_MSG, _STR); \
  243. } while (TRUE); \
  244. }
  245. #define WAIT_FOR_PROTOCOL(_pProt, _O) \
  246. { \
  247. WAIT_FOR_OBJECT_MSG(_O, \
  248. "NDIS: Waiting for protocol %Z\n", \
  249. &(_pProt)->ProtocolCharacteristics.Name); \
  250. }
  251. #define WAIT_FOR_PROTO_MUTEX(_pProt) \
  252. { \
  253. WAIT_FOR_OBJECT_MSG(&(_pProt)->Mutex, \
  254. "NDIS: Waiting for protocol %Z\n", \
  255. &(_pProt)->ProtocolCharacteristics.Name); \
  256. (_pProt)->MutexOwner = (MODULE_NUMBER + __LINE__);\
  257. }
  258. #else
  259. #define WAIT_FOR_PROTOCOL(_pProt, _O) \
  260. { \
  261. WAIT_FOR_OBJECT(_O, NULL); \
  262. }
  263. #define WAIT_FOR_PROTO_MUTEX(_pProt) \
  264. { \
  265. WAIT_FOR_OBJECT(&(_pProt)->Mutex, NULL); \
  266. (_pProt)->MutexOwner = (MODULE_NUMBER + __LINE__); \
  267. }
  268. #endif
  269. #define RELEASE_PROT_MUTEX(_pProt) \
  270. { \
  271. (_pProt)->MutexOwner = 0; \
  272. RELEASE_MUTEX(&(_pProt)->Mutex); \
  273. }
  274. #define WAIT_FOR_PNP_MUTEX() \
  275. { \
  276. WAIT_FOR_OBJECT(&ndisPnPMutex, NULL); \
  277. ndisPnPMutexOwner = (MODULE_NUMBER + __LINE__); \
  278. }
  279. #define RELEASE_PNP_MUTEX() \
  280. { \
  281. ndisPnPMutexOwner = 0; \
  282. RELEASE_MUTEX(&ndisPnPMutex); \
  283. }
  284. #define QUEUE_DPC(_pDpc_) KeInsertQueueDpc(_pDpc_, NULL, NULL)
  285. #define INITIALIZE_DPC(_pDpc_, _R_, _C_) KeInitializeDpc(_pDpc_, _R_, _C_)
  286. #define SET_DPC_IMPORTANCE(_pDpc_) KeSetImportanceDpc(_pDpc_, LowImportance)
  287. #define SET_PROCESSOR_DPC(_pDpc_, _R_) if (!ndisSkipProcessorAffinity) \
  288. KeSetTargetProcessorDpc(_pDpc_, _R_)
  289. #define SYNC_WITH_ISR(_O_, _F_, _C_) KeSynchronizeExecution(_O_, \
  290. (PKSYNCHRONIZE_ROUTINE)(_F_), \
  291. _C_)
  292. #define MDL_ADDRESS(_MDL_) MmGetSystemAddressForMdl(_MDL_) // Don't use
  293. #define MDL_ADDRESS_SAFE(_MDL_, _PRIORITY_) MmGetSystemAddressForMdlSafe(_MDL_, _PRIORITY_)
  294. #define MDL_SIZE(_MDL_) MmGetMdlByteCount(_MDL_)
  295. #define MDL_OFFSET(_MDL_) MmGetMdlByteOffset(_MDL_)
  296. #define MDL_VA(_MDL_) MmGetMdlVirtualAddress(_MDL_)
  297. #define max(_a, _b) (((_a) > (_b)) ? (_a) : (_b))
  298. #define min(_a, _b) (((_a) < (_b)) ? (_a) : (_b))
  299. #define NDIS_EQUAL_UNICODE_STRING(s1, s2) (((s1)->Length == (s2)->Length) && \
  300. RtlEqualMemory((s1)->Buffer, (s2)->Buffer, (s1)->Length))
  301. #define NDIS_PARTIAL_MATCH_UNICODE_STRING(s1, s2) \
  302. (((s1)->Length != (s2)->Length) && \
  303. RtlEqualMemory((s1)->Buffer, (s2)->Buffer, min((s1)->Length, (s2)->Length)))
  304. #define CHAR_TO_INT(_s, _b, _p) RtlCharToInteger(_s, _b, _p)
  305. #define BAD_MINIPORT(_M, _S) DbgPrint(" ***NDIS*** : Miniport %Z - %s\n", (_M)->pAdapterInstanceName, _S)
  306. #define REF_NDIS_DRIVER_OBJECT() ObfReferenceObject(ndisDriverObject)
  307. #define DEREF_NDIS_DRIVER_OBJECT() ObfDereferenceObject(ndisDriverObject)