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.

418 lines
12 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. intobj.c
  5. Abstract:
  6. This module implements the kernel interrupt object. Functions are provided
  7. to initialize, connect, and disconnect interrupt objects.
  8. Author:
  9. David N. Cutler (davec) 7-May-2000
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. --*/
  14. #include "ki.h"
  15. VOID
  16. KeInitializeInterrupt (
  17. IN PKINTERRUPT Interrupt,
  18. IN PKSERVICE_ROUTINE ServiceRoutine,
  19. IN PVOID ServiceContext,
  20. IN PKSPIN_LOCK SpinLock OPTIONAL,
  21. IN ULONG Vector,
  22. IN KIRQL Irql,
  23. IN KIRQL SynchronizeIrql,
  24. IN KINTERRUPT_MODE InterruptMode,
  25. IN BOOLEAN ShareVector,
  26. IN CCHAR ProcessorNumber,
  27. IN BOOLEAN FloatingSave
  28. )
  29. /*++
  30. Routine Description:
  31. This function initializes a kernel interrupt object. The service routine,
  32. service context, spin lock, vector, IRQL, SynchronizeIrql, and floating
  33. context save flag are initialized.
  34. Arguments:
  35. Interrupt - Supplies a pointer to a control object of type interrupt.
  36. ServiceRoutine - Supplies a pointer to a function that is to be
  37. executed when an interrupt occurs via the specified interrupt
  38. vector.
  39. ServiceContext - Supplies a pointer to an arbitrary data structure which is
  40. to be passed to the function specified by the ServiceRoutine parameter.
  41. SpinLock - Supplies a pointer to an executive spin lock.
  42. Vector - Supplies the HAL-generated interrupt vector. Note that this
  43. is not be directly used as an index into the Interrupt Dispatch Table.
  44. Irql - Supplies the request priority of the interrupting source.
  45. SynchronizeIrql - Supplies the request priority that the interrupt should be
  46. synchronized with.
  47. InterruptMode - Supplies the mode of the interrupt; LevelSensitive or
  48. ShareVector - Supplies a boolean value that specifies whether the
  49. vector can be shared with other interrupt objects or not. If FALSE
  50. then the vector may not be shared, if TRUE it may be.
  51. Latched.
  52. ProcessorNumber - Supplies the number of the processor to which the
  53. interrupt will be connected.
  54. FloatingSave - Supplies a boolean value that determines whether the
  55. floating point registers are to be saved before calling the service
  56. routine function. N.B. This argument is ignored.
  57. Return Value:
  58. None.
  59. --*/
  60. {
  61. LONG Index;
  62. //
  63. // Initialize standard control object header.
  64. //
  65. Interrupt->Type = InterruptObject;
  66. Interrupt->Size = sizeof(KINTERRUPT);
  67. //
  68. // Initialize the address of the service routine, the service context,
  69. // the address of the spin lock, the address of the actual spinlock
  70. // that will be used, the vector number, the IRQL of the interrupting
  71. // source, the IRQL used for synchronize execution, the interrupt mode,
  72. // the processor number, and the floating context save flag.
  73. //
  74. Interrupt->ServiceRoutine = ServiceRoutine;
  75. Interrupt->ServiceContext = ServiceContext;
  76. if (ARGUMENT_PRESENT(SpinLock)) {
  77. Interrupt->ActualLock = SpinLock;
  78. } else {
  79. KeInitializeSpinLock (&Interrupt->SpinLock);
  80. Interrupt->ActualLock = &Interrupt->SpinLock;
  81. }
  82. Interrupt->Vector = Vector;
  83. Interrupt->Irql = Irql;
  84. Interrupt->SynchronizeIrql = SynchronizeIrql;
  85. Interrupt->Mode = InterruptMode;
  86. Interrupt->ShareVector = ShareVector;
  87. Interrupt->Number = ProcessorNumber;
  88. //
  89. // Copy the interrupt dispatch code template into the interrupt object.
  90. //
  91. for (Index = 0; Index < NORMAL_DISPATCH_LENGTH; Index += 1) {
  92. Interrupt->DispatchCode[Index] = KiInterruptTemplate[Index];
  93. }
  94. #if defined(_AMD64_)
  95. //
  96. // Set DispatchAddress to KiInterruptDispatch as a default value.
  97. // The AMD64 HAL expects this to be set here. Other clients will
  98. // overwrite this value as approriate via KeConnectInterrupt().
  99. //
  100. Interrupt->DispatchAddress = &KiInterruptDispatch;
  101. #endif
  102. //
  103. // Set the connected state of the interrupt object to FALSE.
  104. //
  105. Interrupt->Connected = FALSE;
  106. return;
  107. }
  108. BOOLEAN
  109. KeConnectInterrupt (
  110. IN PKINTERRUPT Interrupt
  111. )
  112. /*++
  113. Routine Description:
  114. This function connects an interrupt object to the interrupt vector
  115. specified by the interrupt object.
  116. Arguments:
  117. Interrupt - Supplies a pointer to a control object of type interrupt.
  118. Return Value:
  119. If the interrupt object is already connected or an attempt is made to
  120. connect to an interrupt vector that cannot be connected, then a value
  121. of FALSE is returned. Otherwise, a value of TRUE is returned.
  122. --*/
  123. {
  124. BOOLEAN Connected;
  125. PVOID Dispatch;
  126. ULONG IdtIndex;
  127. PKINTERRUPT Interruptx;
  128. KIRQL Irql;
  129. CCHAR Number;
  130. KIRQL OldIrql;
  131. PVOID Unexpected;
  132. ULONG Vector;
  133. //
  134. // If the interrupt object is already connected, the interrupt vector
  135. // number is invalid, an attempt is being made to connect to a vector
  136. // that cannot be connected, the interrupt request level is invalid, or
  137. // the processor number is invalid, then do not connect the interrupt
  138. // object. Otherwise, connect the interrupt object to the specified
  139. // vector and establish the proper interrupt dispatcher.
  140. //
  141. Connected = FALSE;
  142. Irql = Interrupt->Irql;
  143. Number = Interrupt->Number;
  144. Vector = Interrupt->Vector;
  145. IdtIndex = HalVectorToIDTEntry(Vector);
  146. if (((IdtIndex > MAXIMUM_PRIMARY_VECTOR) ||
  147. (Irql > HIGH_LEVEL) ||
  148. (Irql != (IdtIndex >> 4)) ||
  149. (Number >= KeNumberProcessors) ||
  150. (Interrupt->SynchronizeIrql < Irql)) == FALSE) {
  151. //
  152. // Set the system affinity to the specified processor, raise IRQL to
  153. // dispatcher level, and lock the dispatcher database.
  154. //
  155. KeSetSystemAffinityThread(AFFINITY_MASK(Number));
  156. KiLockDispatcherDatabase(&OldIrql);
  157. //
  158. // If the specified interrupt vector is not connected, then
  159. // connect the interrupt vector to the interrupt object dispatch
  160. // code, establish the dispatcher address, and set the new
  161. // interrupt mode and enable masks. Otherwise, if the interrupt is
  162. // already chained, then add the new interrupt object at the end
  163. // of the chain. If the interrupt vector is not chained, then
  164. // start a chain with the previous interrupt object at the front
  165. // of the chain. The interrupt mode of all interrupt objects in
  166. // a chain must be the same.
  167. //
  168. if (Interrupt->Connected == FALSE) {
  169. KeGetIdtHandlerAddress(Vector, &Dispatch);
  170. Unexpected = &KxUnexpectedInterrupt0[IdtIndex];
  171. if (Unexpected == Dispatch) {
  172. //
  173. // The interrupt vector is not connected.
  174. //
  175. Connected = HalEnableSystemInterrupt(Vector,
  176. Irql,
  177. Interrupt->Mode);
  178. if (Connected != FALSE) {
  179. Interrupt->DispatchAddress = &KiInterruptDispatch;
  180. KeSetIdtHandlerAddress(Vector, &Interrupt->DispatchCode[0]);
  181. }
  182. } else if (IdtIndex >= PRIMARY_VECTOR_BASE) {
  183. //
  184. // The interrupt vector is connected. Make sure the interrupt
  185. // mode matchs and that both interrupt objects allow sharing
  186. // of the interrupt vector.
  187. //
  188. Interruptx = CONTAINING_RECORD(Dispatch,
  189. KINTERRUPT,
  190. DispatchCode[0]);
  191. if ((Interrupt->Mode == Interruptx->Mode) &&
  192. (Interrupt->ShareVector != FALSE) &&
  193. (Interruptx->ShareVector != FALSE)) {
  194. Connected = TRUE;
  195. //
  196. // If the chained dispatch routine is not being used,
  197. // then switch to chained dispatch.
  198. //
  199. if (Interruptx->DispatchAddress != &KiChainedDispatch) {
  200. InitializeListHead(&Interruptx->InterruptListEntry);
  201. Interruptx->DispatchAddress = &KiChainedDispatch;
  202. }
  203. InsertTailList(&Interruptx->InterruptListEntry,
  204. &Interrupt->InterruptListEntry);
  205. }
  206. }
  207. }
  208. //
  209. // Unlock dispatcher database, lower IRQL to its previous value, and
  210. // set the system affinity back to the original value.
  211. //
  212. KiUnlockDispatcherDatabase(OldIrql);
  213. KeRevertToUserAffinityThread();
  214. }
  215. //
  216. // Return whether interrupt was connected to the specified vector.
  217. //
  218. Interrupt->Connected = Connected;
  219. return Connected;
  220. }
  221. BOOLEAN
  222. KeDisconnectInterrupt (
  223. IN PKINTERRUPT Interrupt
  224. )
  225. /*++
  226. Routine Description:
  227. This function disconnects an interrupt object from the interrupt vector
  228. specified by the interrupt object.
  229. Arguments:
  230. Interrupt - Supplies a pointer to a control object of type interrupt.
  231. Return Value:
  232. If the interrupt object is not connected, then a value of FALSE is
  233. returned. Otherwise, a value of TRUE is returned.
  234. --*/
  235. {
  236. BOOLEAN Disconnected;
  237. PVOID Dispatch;
  238. ULONG IdtIndex;
  239. PKINTERRUPT Interruptx;
  240. PKINTERRUPT Interrupty;
  241. KIRQL Irql;
  242. KIRQL OldIrql;
  243. PVOID Unexpected;
  244. ULONG Vector;
  245. //
  246. // Set the system affinity to the specified processor, raise IRQL to
  247. // dispatcher level, and lock dispatcher database.
  248. //
  249. KeSetSystemAffinityThread(AFFINITY_MASK(Interrupt->Number));
  250. KiLockDispatcherDatabase(&OldIrql);
  251. //
  252. // If the interrupt object is connected, then disconnect it from the
  253. // specified vector.
  254. //
  255. Disconnected = Interrupt->Connected;
  256. if (Disconnected != FALSE) {
  257. Irql = Interrupt->Irql;
  258. Vector = Interrupt->Vector;
  259. IdtIndex = HalVectorToIDTEntry(Vector);
  260. //
  261. // If the specified interrupt vector is not connected to the chained
  262. // interrupt dispatcher, then disconnect it by setting its dispatch
  263. // address to the unexpected interrupt routine. Otherwise, remove the
  264. // interrupt object from the interrupt chain. If there is only
  265. // one entry remaining in the list, then reestablish the dispatch
  266. // address.
  267. //
  268. KeGetIdtHandlerAddress(Vector, &Dispatch);
  269. Interruptx = CONTAINING_RECORD(Dispatch, KINTERRUPT, DispatchCode[0]);
  270. if (Interruptx->DispatchAddress == &KiChainedDispatch) {
  271. //
  272. // The interrupt object is connected to the chained dispatcher.
  273. //
  274. if (Interrupt == Interruptx) {
  275. Interruptx = CONTAINING_RECORD(Interruptx->InterruptListEntry.Flink,
  276. KINTERRUPT,
  277. InterruptListEntry);
  278. Interruptx->DispatchAddress = &KiChainedDispatch;
  279. KeSetIdtHandlerAddress(Vector, &Interruptx->DispatchCode[0]);
  280. }
  281. RemoveEntryList(&Interrupt->InterruptListEntry);
  282. Interrupty = CONTAINING_RECORD(Interruptx->InterruptListEntry.Flink,
  283. KINTERRUPT,
  284. InterruptListEntry);
  285. if (Interruptx == Interrupty) {
  286. Interrupty->DispatchAddress = KiDispatchInterrupt;
  287. KeSetIdtHandlerAddress(Vector, &Interrupty->DispatchCode[0]);
  288. }
  289. } else {
  290. //
  291. // The interrupt object is not connected to the chained interrupt
  292. // dispatcher.
  293. //
  294. HalDisableSystemInterrupt(Vector, Irql);
  295. Unexpected = &KxUnexpectedInterrupt0[IdtIndex];
  296. KeSetIdtHandlerAddress(Vector, Unexpected);
  297. }
  298. Interrupt->Connected = FALSE;
  299. }
  300. //
  301. // Unlock dispatcher database, lower IRQL to its previous value, and
  302. // set the system affinity back to the original value.
  303. //
  304. KiUnlockDispatcherDatabase(OldIrql);
  305. KeRevertToUserAffinityThread();
  306. //
  307. // Return whether interrupt was disconnected from the specified vector.
  308. //
  309. return Disconnected;
  310. }