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.

452 lines
14 KiB

  1. /*++
  2. Copyright (c) 1990 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) 3-Apr-1990
  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, Synchronized IRQL, 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 an optional pointer to an executive spin lock.
  42. Vector - Supplies the index of the entry in the Interrupt Dispatch Table
  43. that is to be associated with the ServiceRoutine function.
  44. Irql - Supplies the request priority of the interrupting source.
  45. SynchronizeIrql - The request priority that the interrupt should be
  46. synchronized with.
  47. InterruptMode - Supplies the mode of the interrupt; LevelSensitive or
  48. Latched.
  49. ShareVector - Supplies a boolean value that specifies whether the
  50. vector can be shared with other interrupt objects or not. If FALSE
  51. then the vector may not be shared, if TRUE it may be.
  52. Latched.
  53. ProcessorNumber - Supplies the number of the processor to which the
  54. interrupt will be connected.
  55. FloatingSave - Supplies a boolean value that determines whether the
  56. floating point registers and pipe line are to be saved before calling
  57. the ServiceRoutine function.
  58. Return Value:
  59. None.
  60. --*/
  61. {
  62. LONG Index;
  63. //
  64. // Initialize standard control object header.
  65. //
  66. Interrupt->Type = InterruptObject;
  67. Interrupt->Size = sizeof(KINTERRUPT);
  68. //
  69. // Initialize the address of the service routine, the service context,
  70. // the address of the spin lock, the address of the actual spin lock
  71. // that will be used, the vector number, the IRQL of the interrupting
  72. // source, the Synchronized IRQL of the interrupt object, the interrupt
  73. // mode, the processor number, and the floating context save flag.
  74. //
  75. Interrupt->ServiceRoutine = ServiceRoutine; // function pointer
  76. Interrupt->ServiceContext = ServiceContext;
  77. if (ARGUMENT_PRESENT(SpinLock)) {
  78. Interrupt->ActualLock = SpinLock;
  79. } else {
  80. Interrupt->SpinLock = 0;
  81. Interrupt->ActualLock = &Interrupt->SpinLock;
  82. }
  83. Interrupt->Vector = Vector;
  84. Interrupt->Irql = Irql;
  85. Interrupt->SynchronizeIrql = SynchronizeIrql;
  86. Interrupt->Mode = InterruptMode;
  87. Interrupt->ShareVector = ShareVector;
  88. Interrupt->Number = ProcessorNumber;
  89. Interrupt->FloatingSave = FloatingSave;
  90. //
  91. // Set the connected state of the interrupt object to FALSE.
  92. //
  93. Interrupt->Connected = FALSE;
  94. return;
  95. }
  96. BOOLEAN
  97. KeConnectInterrupt (
  98. IN PKINTERRUPT Interrupt
  99. )
  100. /*++
  101. Routine Description:
  102. This function connects an interrupt object to the interrupt vector
  103. specified by the interrupt object. If the interrupt object is already
  104. connected, or an attempt is made to connect to an interrupt that cannot
  105. be connected, then a value of FALSE is returned. Else the specified
  106. interrupt object is connected to the interrupt vector, the connected
  107. state is set to TRUE, and TRUE is returned as the function value.
  108. Arguments:
  109. Interrupt - Supplies a pointer to a control object of type interrupt.
  110. Return Value:
  111. If the interrupt object is already connected or an attempt is made to
  112. connect to an interrupt vector that cannot be connected, then a value
  113. of FALSE is returned. Else a value of TRUE is returned.
  114. --*/
  115. {
  116. BOOLEAN Connected;
  117. PKINTERRUPT Interruptx;
  118. KIRQL Irql;
  119. CHAR Number;
  120. KIRQL OldIrql;
  121. ULONG Vector;
  122. UCHAR IDTEntry;
  123. //
  124. // If the interrupt object is already connected, the interrupt vector
  125. // number is invalid, an attempt is being made to connect to a vector
  126. // that cannot be connected, the interrupt request level is invalid,
  127. // the processor number is invalid, of the interrupt vector is less
  128. // than or equal to the highest level and it not equal to the specified
  129. // IRQL, then do not connect the interrupt object. Else connect interrupt
  130. // object to the specified vector and establish the proper interrupt
  131. // dispatcher.
  132. //
  133. Connected = FALSE;
  134. Irql = Interrupt->Irql;
  135. Number = Interrupt->Number;
  136. Vector = Interrupt->Vector;
  137. IDTEntry = HalVectorToIDTEntry(Vector);
  138. if ((((IDTEntry >= MAXIMUM_VECTOR) || (Irql > HIGH_LEVEL) ||
  139. ((IDTEntry <= HIGH_LEVEL) &&
  140. ((((1 << IDTEntry) & PCR->ReservedVectors) != 0) || (IDTEntry != Irql))) ||
  141. (Number >= KeNumberProcessors))) == FALSE) {
  142. //
  143. // Set system affinity to the specified processor.
  144. //
  145. KeSetSystemAffinityThread(AFFINITY_MASK(Number));
  146. //
  147. // Raise IRQL to dispatcher level and lock dispatcher database.
  148. //
  149. KiLockDispatcherDatabase(&OldIrql);
  150. //
  151. // If the specified interrupt vector is not connected, then
  152. // connect the interrupt vector to the interrupt object dispatch
  153. // code, establish the dispatcher address, and set the new
  154. // interrupt mode and enable masks. Else if the interrupt is
  155. // already chained, then add the new interrupt object at the end
  156. // of the chain. If the interrupt vector is not chained, then
  157. // start a chain with the previous interrupt object at the front
  158. // of the chain. The interrupt mode of all interrupt objects in
  159. // a chain must be the same.
  160. //
  161. if (Interrupt->Connected == FALSE) {
  162. if (PCR->InterruptRoutine[IDTEntry] ==
  163. (PKINTERRUPT_ROUTINE)(&KxUnexpectedInterrupt.DispatchCode)) {
  164. Connected = TRUE;
  165. Interrupt->Connected = TRUE;
  166. if (Interrupt->FloatingSave != FALSE) {
  167. Interrupt->DispatchAddress = KiFloatingDispatch;
  168. } else {
  169. if (Interrupt->Irql == Interrupt->SynchronizeIrql) {
  170. Interrupt->DispatchAddress =
  171. (PKINTERRUPT_ROUTINE)KiInterruptDispatchSame;
  172. } else {
  173. Interrupt->DispatchAddress =
  174. (PKINTERRUPT_ROUTINE)KiInterruptDispatchRaise;
  175. }
  176. }
  177. //
  178. // Copy the plabel for the Dispatch routine into DispatchCode.
  179. // This will be used by KiExternalInterruptHandler to
  180. // dispatch the interrupt.
  181. //
  182. RtlMoveMemory(Interrupt->DispatchCode,
  183. Interrupt->DispatchAddress,
  184. DISPATCH_LENGTH*4);
  185. PCR->InterruptRoutine[IDTEntry] =
  186. (PKINTERRUPT_ROUTINE)(&Interrupt->DispatchCode);
  187. HalEnableSystemInterrupt(Vector, Irql, Interrupt->Mode);
  188. } else {
  189. Interruptx = CONTAINING_RECORD(PCR->InterruptRoutine[IDTEntry],
  190. KINTERRUPT,
  191. DispatchCode[0]);
  192. if (Interrupt->Mode == Interruptx->Mode) {
  193. Connected = TRUE;
  194. Interrupt->Connected = TRUE;
  195. ASSERT (Irql <= KiSynchIrql);
  196. if (Interruptx->DispatchAddress != KiChainedDispatch) {
  197. InitializeListHead(&Interruptx->InterruptListEntry);
  198. Interruptx->DispatchAddress = KiChainedDispatch;
  199. RtlMoveMemory(Interruptx->DispatchCode,
  200. Interruptx->DispatchAddress,
  201. DISPATCH_LENGTH*4);
  202. }
  203. InsertTailList(&Interruptx->InterruptListEntry,
  204. &Interrupt->InterruptListEntry);
  205. }
  206. }
  207. }
  208. //
  209. // Unlock dispatcher database and lower IRQL to its previous value.
  210. //
  211. KiUnlockDispatcherDatabase(OldIrql);
  212. //
  213. // Set system affinity back to the original value.
  214. //
  215. KeRevertToUserAffinityThread();
  216. }
  217. //
  218. // Return whether interrupt was connected to the specified vector.
  219. //
  220. return Connected;
  221. }
  222. BOOLEAN
  223. KeDisconnectInterrupt (
  224. IN PKINTERRUPT Interrupt
  225. )
  226. /*++
  227. Routine Description:
  228. This function disconnects an interrupt object from the interrupt vector
  229. specified by the interrupt object. If the interrupt object is not
  230. connected, then a value of FALSE is returned. Else the specified interrupt
  231. object is disconnected from the interrupt vector, the connected state is
  232. set to FALSE, and TRUE is returned as the function value.
  233. Arguments:
  234. Interrupt - Supplies a pointer to a control object of type interrupt.
  235. Return Value:
  236. If the interrupt object is not connected, then a value of FALSE is
  237. returned. Else a value of TRUE is returned.
  238. --*/
  239. {
  240. BOOLEAN Connected;
  241. PKINTERRUPT Interruptx;
  242. PKINTERRUPT Interrupty;
  243. KIRQL Irql;
  244. KIRQL OldIrql;
  245. ULONG Vector;
  246. UCHAR IDTEntry;
  247. //
  248. // Set system affinity to the specified processor.
  249. //
  250. KeSetSystemAffinityThread(AFFINITY_MASK(Interrupt->Number));
  251. //
  252. // Raise IRQL to dispatcher level and lock dispatcher database.
  253. //
  254. KiLockDispatcherDatabase(&OldIrql);
  255. //
  256. // If the interrupt object is connected, then disconnect it from the
  257. // specified vector.
  258. //
  259. Connected = Interrupt->Connected;
  260. if (Connected != FALSE) {
  261. Irql = Interrupt->Irql;
  262. Vector = Interrupt->Vector;
  263. IDTEntry = HalVectorToIDTEntry(Vector);
  264. //
  265. // If the specified interrupt vector is not connected to the chained
  266. // interrupt dispatcher, then disconnect it by setting its dispatch
  267. // address to the unexpected interrupt routine. Else remove the
  268. // interrupt object from the interrupt chain. If there is only
  269. // one entry remaining in the list, then reestablish the dispatch
  270. // address.
  271. //
  272. Interruptx = CONTAINING_RECORD(PCR->InterruptRoutine[IDTEntry],
  273. KINTERRUPT,
  274. DispatchCode[0]);
  275. if (Interruptx->DispatchAddress ==
  276. (PKINTERRUPT_ROUTINE)KiChainedDispatch) {
  277. ASSERT (Irql <= KiSynchIrql);
  278. if (Interrupt == Interruptx) {
  279. Interruptx = CONTAINING_RECORD(Interruptx->InterruptListEntry.Flink,
  280. KINTERRUPT, InterruptListEntry);
  281. Interruptx->DispatchAddress =
  282. (PKINTERRUPT_ROUTINE)KiChainedDispatch;
  283. RtlMoveMemory(Interruptx->DispatchCode,
  284. Interruptx->DispatchAddress,
  285. DISPATCH_LENGTH*4);
  286. PCR->InterruptRoutine[IDTEntry] =
  287. (PKINTERRUPT_ROUTINE)(&Interruptx->DispatchCode);
  288. }
  289. RemoveEntryList(&Interrupt->InterruptListEntry);
  290. Interrupty = CONTAINING_RECORD(Interruptx->InterruptListEntry.Flink,
  291. KINTERRUPT,
  292. InterruptListEntry);
  293. if (Interruptx == Interrupty) {
  294. if (Interrupty->FloatingSave != FALSE) {
  295. Interrupty->DispatchAddress = KiFloatingDispatch;
  296. } else {
  297. if (Interrupty->Irql == Interrupty->SynchronizeIrql) {
  298. Interrupty->DispatchAddress =
  299. (PKINTERRUPT_ROUTINE)KiInterruptDispatchSame;
  300. } else {
  301. Interrupty->DispatchAddress =
  302. (PKINTERRUPT_ROUTINE)KiInterruptDispatchRaise;
  303. }
  304. }
  305. //
  306. // Copy the plabel for the Dispatch routine into DispatchCode.
  307. // This will be used by KiExternalInterruptHandler to
  308. // dispatch the interrupt.
  309. //
  310. RtlMoveMemory(Interrupty->DispatchCode,
  311. Interrupty->DispatchAddress,
  312. DISPATCH_LENGTH*4);
  313. PCR->InterruptRoutine[IDTEntry] =
  314. (PKINTERRUPT_ROUTINE)(&Interrupty->DispatchCode);
  315. }
  316. } else {
  317. HalDisableSystemInterrupt(Vector, Irql);
  318. PCR->InterruptRoutine[IDTEntry] =
  319. (PKINTERRUPT_ROUTINE)(&KxUnexpectedInterrupt.DispatchCode);
  320. }
  321. Interrupt->Connected = FALSE;
  322. }
  323. //
  324. // Unlock dispatcher database and lower IRQL to its previous value.
  325. //
  326. KiUnlockDispatcherDatabase(OldIrql);
  327. //
  328. // Set system affinity back to the original value.
  329. //
  330. KeRevertToUserAffinityThread();
  331. //
  332. // Return whether interrupt was disconnected from the specified vector.
  333. //
  334. return Connected;
  335. }