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.

462 lines
13 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;
  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. // Copy the interrupt dispatch code template into the interrupt object
  92. // and flush the dcache on all processors that the current thread can
  93. // run on to ensure that the code is actually in memory.
  94. //
  95. for (Index = 0; Index < DISPATCH_LENGTH; Index += 1) {
  96. Interrupt->DispatchCode[Index] = KiInterruptTemplate[Index];
  97. }
  98. KeSweepIcache(FALSE);
  99. //
  100. // Set the connected state of the interrupt object to FALSE.
  101. //
  102. Interrupt->Connected = FALSE;
  103. return;
  104. }
  105. BOOLEAN
  106. KeConnectInterrupt (
  107. IN PKINTERRUPT Interrupt
  108. )
  109. /*++
  110. Routine Description:
  111. This function connects an interrupt object to the interrupt vector
  112. specified by the interrupt object. If the interrupt object is already
  113. connected, or an attempt is made to connect to an interrupt that cannot
  114. be connected, then a value of FALSE is returned. Else the specified
  115. interrupt object is connected to the interrupt vector, the connected
  116. state is set to TRUE, and TRUE is returned as the function value.
  117. Arguments:
  118. Interrupt - Supplies a pointer to a control object of type interrupt.
  119. Return Value:
  120. If the interrupt object is already connected or an attempt is made to
  121. connect to an interrupt vector that cannot be connected, then a value
  122. of FALSE is returned. Else a value of TRUE is returned.
  123. --*/
  124. {
  125. BOOLEAN Connected;
  126. PKINTERRUPT Interruptx;
  127. KIRQL Irql;
  128. CHAR Number;
  129. KIRQL OldIrql;
  130. ULONG Vector;
  131. //
  132. // If the interrupt object is already connected, the interrupt vector
  133. // number is invalid, an attempt is being made to connect to a vector
  134. // that cannot be connected, the interrupt request level is invalid,
  135. // the processor number is invalid, of the interrupt vector is less
  136. // than or equal to the highest level and it not equal to the specified
  137. // IRQL, then do not connect the interrupt object. Else connect interrupt
  138. // object to the specified vector and establish the proper interrupt
  139. // dispatcher.
  140. //
  141. Connected = FALSE;
  142. Irql = Interrupt->Irql;
  143. Number = Interrupt->Number;
  144. Vector = Interrupt->Vector;
  145. if (((Vector >= MAXIMUM_VECTOR) || (Irql > HIGH_LEVEL) ||
  146. ((Vector <= HIGH_LEVEL) &&
  147. (((1 << Vector & PCR->ReservedVectors) != 0))) ||
  148. (Number >= KeNumberProcessors)) == FALSE) {
  149. //
  150. // Set system affinity to the specified processor.
  151. //
  152. KeSetSystemAffinityThread((KAFFINITY)(1 << Number));
  153. //
  154. // Raise IRQL to dispatcher level and lock dispatcher database.
  155. //
  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. Else 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. if ( PCR->InterruptRoutine[Vector] ==
  170. (PKINTERRUPT_ROUTINE)(&KxUnexpectedInterrupt.DispatchCode) ) {
  171. Connected = TRUE;
  172. Interrupt->Connected = TRUE;
  173. if (Interrupt->FloatingSave) {
  174. Interrupt->DispatchAddress = KiFloatingDispatch;
  175. } else {
  176. if (Interrupt->Irql == Interrupt->SynchronizeIrql) {
  177. Interrupt->DispatchAddress =
  178. (PKINTERRUPT_ROUTINE)KiInterruptDispatchSame;
  179. } else {
  180. Interrupt->DispatchAddress =
  181. (PKINTERRUPT_ROUTINE)KiInterruptDispatchRaise;
  182. }
  183. }
  184. PCR->InterruptRoutine[Vector] =
  185. (PKINTERRUPT_ROUTINE)(&Interrupt->DispatchCode);
  186. HalEnableSystemInterrupt(Vector, Irql, Interrupt->Mode);
  187. } else {
  188. Interruptx = CONTAINING_RECORD(PCR->InterruptRoutine[Vector],
  189. KINTERRUPT,
  190. DispatchCode[0]);
  191. if (Interrupt->Mode == Interruptx->Mode) {
  192. Connected = TRUE;
  193. Interrupt->Connected = TRUE;
  194. ASSERT (Irql <= KiSynchIrql);
  195. if (Interruptx->DispatchAddress != KiChainedDispatch) {
  196. InitializeListHead(&Interruptx->InterruptListEntry);
  197. Interruptx->DispatchAddress = KiChainedDispatch;
  198. }
  199. InsertTailList(&Interruptx->InterruptListEntry,
  200. &Interrupt->InterruptListEntry);
  201. }
  202. }
  203. }
  204. //
  205. // Unlock dispatcher database and lower IRQL to its previous value.
  206. //
  207. KiUnlockDispatcherDatabase(OldIrql);
  208. //
  209. // Set system affinity back to the original value.
  210. //
  211. KeRevertToUserAffinityThread();
  212. }
  213. //
  214. // Return whether interrupt was connected to the specified vector.
  215. //
  216. return Connected;
  217. }
  218. BOOLEAN
  219. KeDisconnectInterrupt (
  220. IN PKINTERRUPT Interrupt
  221. )
  222. /*++
  223. Routine Description:
  224. This function disconnects an interrupt object from the interrupt vector
  225. specified by the interrupt object. If the interrupt object is not
  226. connected, then a value of FALSE is returned. Else the specified interrupt
  227. object is disconnected from the interrupt vector, the connected state is
  228. set to FALSE, and TRUE is returned as the function value.
  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. Else a value of TRUE is returned.
  234. --*/
  235. {
  236. BOOLEAN Connected;
  237. PKINTERRUPT Interruptx;
  238. PKINTERRUPT Interrupty;
  239. KIRQL Irql;
  240. KIRQL OldIrql;
  241. ULONG Vector;
  242. //
  243. // Set system affinity to the specified processor.
  244. //
  245. KeSetSystemAffinityThread((KAFFINITY)(1 << Interrupt->Number));
  246. //
  247. // Raise IRQL to dispatcher level and lock dispatcher database.
  248. //
  249. KiLockDispatcherDatabase(&OldIrql);
  250. //
  251. // If the interrupt object is connected, then disconnect it from the
  252. // specified vector.
  253. //
  254. Connected = Interrupt->Connected;
  255. if (Connected != FALSE) {
  256. Irql = Interrupt->Irql;
  257. Vector = Interrupt->Vector;
  258. //
  259. // If the specified interrupt vector is not connected to the chained
  260. // interrupt dispatcher, then disconnect it by setting its dispatch
  261. // address to the unexpected interrupt routine. Else remove the
  262. // interrupt object from the interrupt chain. If there is only
  263. // one entry remaining in the list, then reestablish the dispatch
  264. // address.
  265. //
  266. Interruptx = CONTAINING_RECORD(PCR->InterruptRoutine[Vector],
  267. KINTERRUPT,
  268. DispatchCode[0]);
  269. if (Interruptx->DispatchAddress == KiChainedDispatch) {
  270. ASSERT (Irql <= KiSynchIrql);
  271. if (Interrupt == Interruptx) {
  272. Interruptx = CONTAINING_RECORD(Interruptx->InterruptListEntry.Flink,
  273. KINTERRUPT, InterruptListEntry);
  274. Interruptx->DispatchAddress = KiChainedDispatch;
  275. PCR->InterruptRoutine[Vector] =
  276. (PKINTERRUPT_ROUTINE)(&Interruptx->DispatchCode);
  277. }
  278. RemoveEntryList(&Interrupt->InterruptListEntry);
  279. Interrupty = CONTAINING_RECORD(Interruptx->InterruptListEntry.Flink,
  280. KINTERRUPT,
  281. InterruptListEntry);
  282. if (Interruptx == Interrupty) {
  283. if (Interrupty->FloatingSave) {
  284. Interrupty->DispatchAddress = KiFloatingDispatch;
  285. } else {
  286. if (Interrupty->Irql == Interrupty->SynchronizeIrql) {
  287. Interrupty->DispatchAddress =
  288. (PKINTERRUPT_ROUTINE)KiInterruptDispatchSame;
  289. } else {
  290. Interrupty->DispatchAddress =
  291. (PKINTERRUPT_ROUTINE)KiInterruptDispatchRaise;
  292. }
  293. }
  294. PCR->InterruptRoutine[Vector] =
  295. (PKINTERRUPT_ROUTINE)(&Interrupty->DispatchCode);
  296. }
  297. } else {
  298. HalDisableSystemInterrupt(Vector, Irql);
  299. PCR->InterruptRoutine[Vector] =
  300. (PKINTERRUPT_ROUTINE)(&KxUnexpectedInterrupt.DispatchCode);
  301. }
  302. KeSweepIcache(TRUE);
  303. Interrupt->Connected = FALSE;
  304. }
  305. //
  306. // Unlock dispatcher database and lower IRQL to its previous value.
  307. //
  308. KiUnlockDispatcherDatabase(OldIrql);
  309. //
  310. // Set system affinity back to the original value.
  311. //
  312. KeRevertToUserAffinityThread();
  313. //
  314. // Return whether interrupt was disconnected from the specified vector.
  315. //
  316. return Connected;
  317. }
  318. PKTRAP_FRAME
  319. KeGetInterruptTrapFrame(
  320. VOID
  321. )
  322. /*++
  323. Routine Description:
  324. Returns a pointer to the last interrupt trap frame on the
  325. current stack. This allows the machine check handlers in
  326. the HAL to inspect the trap frame without having to pass
  327. the PKTRAP_FRAME to every interrupt handler.
  328. Arguments:
  329. None.
  330. Return Value:
  331. Pointer to the last interrupt trap frame on the stack.
  332. --*/
  333. {
  334. ASSERT(KeGetCurrentIrql() >= DEVICE_LEVEL);
  335. ASSERT(KeGetCurrentPrcb()->InterruptTrapFrame != NULL);
  336. return(KeGetCurrentPrcb()->InterruptTrapFrame);
  337. }