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.

557 lines
15 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. devquobj.c
  5. Abstract:
  6. This module implements the kernel device queue object. Functions are
  7. provided to initialize a device queue object and to insert and remove
  8. device queue entries in a device queue object.
  9. Author:
  10. David N. Cutler (davec) 1-Apr-1989
  11. Environment:
  12. Kernel mode only.
  13. Revision History:
  14. --*/
  15. #include "ki.h"
  16. //
  17. // The following assert macro is used to check that an input device queue
  18. // is really a kdevice_queue and not something else, like deallocated pool.
  19. //
  20. #define ASSERT_DEVICE_QUEUE(E) { \
  21. ASSERT((E)->Type == DeviceQueueObject); \
  22. }
  23. VOID
  24. KeInitializeDeviceQueue (
  25. IN PKDEVICE_QUEUE DeviceQueue
  26. )
  27. /*++
  28. Routine Description:
  29. This function initializes a kernel device queue object.
  30. Arguments:
  31. DeviceQueue - Supplies a pointer to a control object of type device
  32. queue.
  33. SpinLock - Supplies a pointer to an executive spin lock.
  34. Return Value:
  35. None.
  36. --*/
  37. {
  38. //
  39. // Initialize standard control object header.
  40. //
  41. DeviceQueue->Type = DeviceQueueObject;
  42. DeviceQueue->Size = sizeof(KDEVICE_QUEUE);
  43. //
  44. // Initialize the device queue list head, spin lock, and busy indicator.
  45. //
  46. InitializeListHead(&DeviceQueue->DeviceListHead);
  47. KeInitializeSpinLock(&DeviceQueue->Lock);
  48. DeviceQueue->Busy = FALSE;
  49. return;
  50. }
  51. BOOLEAN
  52. KeInsertDeviceQueue (
  53. IN PKDEVICE_QUEUE DeviceQueue,
  54. IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
  55. )
  56. /*++
  57. Routine Description:
  58. This function inserts a device queue entry at the tail of the specified
  59. device queue. If the device is not busy, then it is set busy and the entry
  60. is not placed in the device queue. Otherwise the specified entry is placed
  61. at the end of the device queue.
  62. Arguments:
  63. DeviceQueue - Supplies a pointer to a control object of type device queue.
  64. DeviceQueueEntry - Supplies a pointer to a device queue entry.
  65. Return Value:
  66. If the device is not busy, then a value of FALSE is returned. Otherwise a
  67. value of TRUE is returned.
  68. --*/
  69. {
  70. BOOLEAN Busy;
  71. BOOLEAN Inserted;
  72. KLOCK_QUEUE_HANDLE LockHandle;
  73. ASSERT_DEVICE_QUEUE(DeviceQueue);
  74. //
  75. // Set inserted to FALSE and lock specified device queue.
  76. //
  77. Inserted = FALSE;
  78. KiAcquireInStackQueuedSpinLockForDpc(&DeviceQueue->Lock, &LockHandle);
  79. //
  80. // Insert the specified device queue entry at the end of the device queue
  81. // if the device queue is busy. Otherwise set the device queue busy and
  82. // don't insert the device queue entry.
  83. //
  84. Busy = DeviceQueue->Busy;
  85. DeviceQueue->Busy = TRUE;
  86. if (Busy == TRUE) {
  87. InsertTailList(&DeviceQueue->DeviceListHead,
  88. &DeviceQueueEntry->DeviceListEntry);
  89. Inserted = TRUE;
  90. }
  91. DeviceQueueEntry->Inserted = Inserted;
  92. //
  93. // Unlock specified device queue.
  94. //
  95. KiReleaseInStackQueuedSpinLockForDpc(&LockHandle);
  96. return Inserted;
  97. }
  98. BOOLEAN
  99. KeInsertByKeyDeviceQueue (
  100. IN PKDEVICE_QUEUE DeviceQueue,
  101. IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry,
  102. IN ULONG SortKey
  103. )
  104. /*++
  105. Routine Description:
  106. This function inserts a device queue entry into the specified device
  107. queue according to a sort key. If the device is not busy, then it is
  108. set busy and the entry is not placed in the device queue. Otherwise
  109. the specified entry is placed in the device queue at a position such
  110. that the specified sort key is greater than or equal to its predecessor
  111. and less than its successor.
  112. Arguments:
  113. DeviceQueue - Supplies a pointer to a control object of type device queue.
  114. DeviceQueueEntry - Supplies a pointer to a device queue entry.
  115. SortKey - Supplies the sort key by which the position to insert the device
  116. queue entry is to be determined.
  117. Return Value:
  118. If the device is not busy, then a value of FALSE is returned. Otherwise a
  119. value of TRUE is returned.
  120. --*/
  121. {
  122. BOOLEAN Busy;
  123. BOOLEAN Inserted;
  124. KLOCK_QUEUE_HANDLE LockHandle;
  125. PLIST_ENTRY NextEntry;
  126. PKDEVICE_QUEUE_ENTRY QueueEntry;
  127. ASSERT_DEVICE_QUEUE(DeviceQueue);
  128. //
  129. // Set inserted to FALSE and lock specified device queue.
  130. //
  131. Inserted = FALSE;
  132. DeviceQueueEntry->SortKey = SortKey;
  133. KiAcquireInStackQueuedSpinLockForDpc(&DeviceQueue->Lock, &LockHandle);
  134. //
  135. // Insert the specified device queue entry in the device queue at the
  136. // position specified by the sort key if the device queue is busy.
  137. // Otherwise set the device queue busy an don't insert the device queue
  138. // entry.
  139. //
  140. Busy = DeviceQueue->Busy;
  141. DeviceQueue->Busy = TRUE;
  142. if (Busy == TRUE) {
  143. NextEntry = DeviceQueue->DeviceListHead.Flink;
  144. while (NextEntry != &DeviceQueue->DeviceListHead) {
  145. QueueEntry = CONTAINING_RECORD(NextEntry,
  146. KDEVICE_QUEUE_ENTRY,
  147. DeviceListEntry);
  148. if (SortKey < QueueEntry->SortKey) {
  149. break;
  150. }
  151. NextEntry = NextEntry->Flink;
  152. }
  153. NextEntry = NextEntry->Blink;
  154. InsertHeadList(NextEntry, &DeviceQueueEntry->DeviceListEntry);
  155. Inserted = TRUE;
  156. }
  157. DeviceQueueEntry->Inserted = Inserted;
  158. //
  159. // Unlock specified device queue.
  160. //
  161. KiReleaseInStackQueuedSpinLockForDpc(&LockHandle);
  162. return Inserted;
  163. }
  164. PKDEVICE_QUEUE_ENTRY
  165. KeRemoveDeviceQueue (
  166. IN PKDEVICE_QUEUE DeviceQueue
  167. )
  168. /*++
  169. Routine Description:
  170. This function removes an entry from the head of the specified device
  171. queue. If the device queue is empty, then the device is set Not-Busy
  172. and a NULL pointer is returned. Otherwise the next entry is removed
  173. from the head of the device queue and the address of device queue entry
  174. is returned.
  175. Arguments:
  176. DeviceQueue - Supplies a pointer to a control object of type device queue.
  177. Return Value:
  178. A NULL pointer is returned if the device queue is empty. Otherwise a
  179. pointer to a device queue entry is returned.
  180. --*/
  181. {
  182. PKDEVICE_QUEUE_ENTRY DeviceQueueEntry;
  183. KLOCK_QUEUE_HANDLE LockHandle;
  184. PLIST_ENTRY NextEntry;
  185. ASSERT_DEVICE_QUEUE(DeviceQueue);
  186. //
  187. // Set device queue entry NULL and lock specified device queue.
  188. //
  189. DeviceQueueEntry = NULL;
  190. KiAcquireInStackQueuedSpinLockForDpc(&DeviceQueue->Lock, &LockHandle);
  191. //
  192. // If the device queue is not empty, then remove the first entry from
  193. // the queue. Otherwise set the device queue not busy.
  194. //
  195. ASSERT(DeviceQueue->Busy == TRUE);
  196. if (IsListEmpty(&DeviceQueue->DeviceListHead) == TRUE) {
  197. DeviceQueue->Busy = FALSE;
  198. } else {
  199. NextEntry = RemoveHeadList(&DeviceQueue->DeviceListHead);
  200. DeviceQueueEntry = CONTAINING_RECORD(NextEntry,
  201. KDEVICE_QUEUE_ENTRY,
  202. DeviceListEntry);
  203. DeviceQueueEntry->Inserted = FALSE;
  204. }
  205. //
  206. // Unlock specified device queue and return address of device queue
  207. // entry.
  208. //
  209. KiReleaseInStackQueuedSpinLockForDpc(&LockHandle);
  210. return DeviceQueueEntry;
  211. }
  212. PKDEVICE_QUEUE_ENTRY
  213. KeRemoveByKeyDeviceQueue (
  214. IN PKDEVICE_QUEUE DeviceQueue,
  215. IN ULONG SortKey
  216. )
  217. /*++
  218. Routine Description:
  219. This function removes an entry from the specified device
  220. queue. If the device queue is empty, then the device is set Not-Busy
  221. and a NULL pointer is returned. Otherwise the an entry is removed
  222. from the device queue and the address of device queue entry
  223. is returned. The queue is search for the first entry which has a value
  224. greater than or equal to the SortKey. If no such entry is found then the
  225. first entry of the queue is returned.
  226. Arguments:
  227. DeviceQueue - Supplies a pointer to a control object of type device queue.
  228. SortKey - Supplies the sort key by which the position to remove the device
  229. queue entry is to be determined.
  230. Return Value:
  231. A NULL pointer is returned if the device queue is empty. Otherwise a
  232. pointer to a device queue entry is returned.
  233. --*/
  234. {
  235. PKDEVICE_QUEUE_ENTRY DeviceQueueEntry;
  236. KLOCK_QUEUE_HANDLE LockHandle;
  237. PLIST_ENTRY NextEntry;
  238. ASSERT_DEVICE_QUEUE(DeviceQueue);
  239. //
  240. // Set device queue entry NULL and lock specified device queue.
  241. //
  242. DeviceQueueEntry = NULL;
  243. KiAcquireInStackQueuedSpinLockForDpc(&DeviceQueue->Lock, &LockHandle);
  244. //
  245. // If the device queue is not empty, then remove the first entry from
  246. // the queue. Otherwise set the device queue not busy.
  247. //
  248. ASSERT(DeviceQueue->Busy == TRUE);
  249. if (IsListEmpty(&DeviceQueue->DeviceListHead) == TRUE) {
  250. DeviceQueue->Busy = FALSE;
  251. } else {
  252. NextEntry = DeviceQueue->DeviceListHead.Flink;
  253. while (NextEntry != &DeviceQueue->DeviceListHead) {
  254. DeviceQueueEntry = CONTAINING_RECORD(NextEntry,
  255. KDEVICE_QUEUE_ENTRY,
  256. DeviceListEntry);
  257. if (SortKey <= DeviceQueueEntry->SortKey) {
  258. break;
  259. }
  260. NextEntry = NextEntry->Flink;
  261. }
  262. if (NextEntry != &DeviceQueue->DeviceListHead) {
  263. RemoveEntryList(&DeviceQueueEntry->DeviceListEntry);
  264. } else {
  265. NextEntry = RemoveHeadList(&DeviceQueue->DeviceListHead);
  266. DeviceQueueEntry = CONTAINING_RECORD(NextEntry,
  267. KDEVICE_QUEUE_ENTRY,
  268. DeviceListEntry);
  269. }
  270. DeviceQueueEntry->Inserted = FALSE;
  271. }
  272. //
  273. // Unlock specified device queue and return address of device queue
  274. // entry.
  275. //
  276. KiReleaseInStackQueuedSpinLockForDpc(&LockHandle);
  277. return DeviceQueueEntry;
  278. }
  279. PKDEVICE_QUEUE_ENTRY
  280. KeRemoveByKeyDeviceQueueIfBusy (
  281. IN PKDEVICE_QUEUE DeviceQueue,
  282. IN ULONG SortKey
  283. )
  284. /*++
  285. Routine Description:
  286. This function removes an entry from the specified device queue if and
  287. only if the device is currently busy. If the device queue is empty or
  288. the device is not busy, then the device is set Not-Busy and a NULL is
  289. returned. Otherwise, an entry is removed from the device queue and the
  290. address of device queue entry is returned. The queue is search for the
  291. first entry which has a value greater than or equal to the SortKey. If
  292. no such entry is found then the first entry of the queue is returned.
  293. Arguments:
  294. DeviceQueue - Supplies a pointer to a control object of type device queue.
  295. SortKey - Supplies the sort key by which the position to remove the device
  296. queue entry is to be determined.
  297. Return Value:
  298. A NULL pointer is returned if the device queue is empty. Otherwise a
  299. pointer to a device queue entry is returned.
  300. --*/
  301. {
  302. PKDEVICE_QUEUE_ENTRY DeviceQueueEntry;
  303. KLOCK_QUEUE_HANDLE LockHandle;
  304. PLIST_ENTRY NextEntry;
  305. ASSERT_DEVICE_QUEUE(DeviceQueue);
  306. //
  307. // Set device queue entry NULL and lock specified device queue.
  308. //
  309. DeviceQueueEntry = NULL;
  310. KiAcquireInStackQueuedSpinLockForDpc(&DeviceQueue->Lock, &LockHandle);
  311. //
  312. // If the device queue is busy, then attempt to remove an entry from
  313. // the queue using the sort key. Otherwise, set the device queue not
  314. // busy.
  315. //
  316. if (DeviceQueue->Busy != FALSE) {
  317. if (IsListEmpty(&DeviceQueue->DeviceListHead) != FALSE) {
  318. DeviceQueue->Busy = FALSE;
  319. } else {
  320. NextEntry = DeviceQueue->DeviceListHead.Flink;
  321. while (NextEntry != &DeviceQueue->DeviceListHead) {
  322. DeviceQueueEntry = CONTAINING_RECORD(NextEntry,
  323. KDEVICE_QUEUE_ENTRY,
  324. DeviceListEntry);
  325. if (SortKey <= DeviceQueueEntry->SortKey) {
  326. break;
  327. }
  328. NextEntry = NextEntry->Flink;
  329. }
  330. if (NextEntry != &DeviceQueue->DeviceListHead) {
  331. RemoveEntryList(&DeviceQueueEntry->DeviceListEntry);
  332. } else {
  333. NextEntry = RemoveHeadList(&DeviceQueue->DeviceListHead);
  334. DeviceQueueEntry = CONTAINING_RECORD(NextEntry,
  335. KDEVICE_QUEUE_ENTRY,
  336. DeviceListEntry);
  337. }
  338. DeviceQueueEntry->Inserted = FALSE;
  339. }
  340. }
  341. //
  342. // Unlock specified device queue and return address of device queue
  343. // entry.
  344. //
  345. KiReleaseInStackQueuedSpinLockForDpc(&LockHandle);
  346. return DeviceQueueEntry;
  347. }
  348. BOOLEAN
  349. KeRemoveEntryDeviceQueue (
  350. IN PKDEVICE_QUEUE DeviceQueue,
  351. IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
  352. )
  353. /*++
  354. Routine Description:
  355. This function removes a specified entry from the the specified device
  356. queue. If the device queue entry is not in the device queue, then no
  357. operation is performed. Otherwise the specified device queue entry is
  358. removed from the device queue and its inserted status is set to FALSE.
  359. Arguments:
  360. DeviceQueue - Supplies a pointer to a control object of type device queue.
  361. DeviceQueueEntry - Supplies a pointer to a device queue entry which is to
  362. be removed from its device queue.
  363. Return Value:
  364. A value of TRUE is returned if the device queue entry is removed from its
  365. device queue. Otherwise a value of FALSE is returned.
  366. --*/
  367. {
  368. KLOCK_QUEUE_HANDLE LockHandle;
  369. BOOLEAN Removed;
  370. ASSERT_DEVICE_QUEUE(DeviceQueue);
  371. ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
  372. //
  373. // Raise IRQL to dispatcher level and lock specified device queue.
  374. //
  375. KeAcquireInStackQueuedSpinLock(&DeviceQueue->Lock, &LockHandle);
  376. //
  377. // If the device queue entry is not in a device queue, then no operation
  378. // is performed. Otherwise remove the specified device queue entry from its
  379. // device queue.
  380. //
  381. Removed = DeviceQueueEntry->Inserted;
  382. if (Removed == TRUE) {
  383. DeviceQueueEntry->Inserted = FALSE;
  384. RemoveEntryList(&DeviceQueueEntry->DeviceListEntry);
  385. }
  386. //
  387. // Unlock specified device queue, lower IRQL to its previous level, and
  388. // return whether the device queue entry was removed from its queue.
  389. //
  390. KeReleaseInStackQueuedSpinLock(&LockHandle);
  391. return Removed;
  392. }