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.

297 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. flushbuf.c
  5. Abstract:
  6. This module contains the code to flush the write buffer or otherwise
  7. synchronize writes on the host processor. Also, contains code
  8. to flush the instruction cache of specified process.
  9. Author:
  10. David N. Cutler 24-Apr-1991
  11. Revision History:
  12. --*/
  13. #include "mi.h"
  14. ULONG
  15. MiFlushRangeFilter (
  16. IN PEXCEPTION_POINTERS ExceptionPointers,
  17. IN PVOID *BaseAddress,
  18. IN PSIZE_T Length,
  19. IN PLOGICAL Retry
  20. );
  21. #ifdef ALLOC_PRAGMA
  22. #pragma alloc_text(PAGE,NtFlushWriteBuffer)
  23. #pragma alloc_text(PAGE,NtFlushInstructionCache)
  24. #pragma alloc_text(PAGE,MiFlushRangeFilter)
  25. #endif
  26. NTSTATUS
  27. NtFlushWriteBuffer (
  28. VOID
  29. )
  30. /*++
  31. Routine Description:
  32. This function flushes the write buffer on the current processor.
  33. Arguments:
  34. None.
  35. Return Value:
  36. STATUS_SUCCESS.
  37. --*/
  38. {
  39. PAGED_CODE();
  40. KeFlushWriteBuffer();
  41. return STATUS_SUCCESS;
  42. }
  43. ULONG
  44. MiFlushRangeFilter (
  45. IN PEXCEPTION_POINTERS ExceptionPointers,
  46. IN PVOID *BaseAddress,
  47. IN PSIZE_T Length,
  48. IN PLOGICAL Retry
  49. )
  50. /*++
  51. Routine Description:
  52. This is the exception handler used by NtFlushInstructionCache to protect
  53. against bad virtual addresses passed to KeSweepIcacheRange. If an
  54. access violation occurs, this routine causes NtFlushInstructionCache to
  55. restart the sweep at the page following the failing page.
  56. Arguments:
  57. ExceptionPointers - Supplies exception information.
  58. BaseAddress - Supplies a pointer to address the base of the region being
  59. flushed. If the failing address is not in the last page of
  60. the region, this routine updates BaseAddress to point to the
  61. next page of the region.
  62. Length - Supplies a pointer the length of the region being flushed.
  63. If the failing address is not in the last page of the region,
  64. this routine updates Length to reflect restarting the flush at
  65. the next page of the region.
  66. Retry - Supplies a pointer to a LOGICAL that the caller has initialized
  67. to FALSE. This routine sets this LOGICAL to TRUE if an access
  68. violation occurs in a page before the last page of the flush region.
  69. Return Value:
  70. EXCEPTION_EXECUTE_HANDLER.
  71. --*/
  72. {
  73. PEXCEPTION_RECORD ExceptionRecord;
  74. ULONG_PTR BadVa;
  75. ULONG_PTR NextVa;
  76. ULONG_PTR EndVa;
  77. ExceptionRecord = ExceptionPointers->ExceptionRecord;
  78. //
  79. // If the exception was an access violation, skip the current page of the
  80. // region and move to the next page.
  81. //
  82. if ( ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION ) {
  83. //
  84. // Get the failing address, calculate the base address of the next page,
  85. // and calculate the address at the end of the region.
  86. //
  87. BadVa = ExceptionRecord->ExceptionInformation[1];
  88. NextVa = ROUND_TO_PAGES( BadVa + 1 );
  89. EndVa = *(PULONG_PTR)BaseAddress + *Length;
  90. //
  91. // If the next page didn't wrap, and the next page is below the end of
  92. // the region, update Length and BaseAddress appropriately and set Retry
  93. // to TRUE to indicate to NtFlushInstructionCache that it should call
  94. // KeSweepIcacheRange again.
  95. //
  96. if ( (NextVa > BadVa) && (NextVa < EndVa) ) {
  97. *Length = (ULONG) (EndVa - NextVa);
  98. *BaseAddress = (PVOID)NextVa;
  99. *Retry = TRUE;
  100. }
  101. }
  102. return EXCEPTION_EXECUTE_HANDLER;
  103. }
  104. NTSTATUS
  105. NtFlushInstructionCache (
  106. IN HANDLE ProcessHandle,
  107. IN PVOID BaseAddress OPTIONAL,
  108. IN SIZE_T Length
  109. )
  110. /*++
  111. Routine Description:
  112. This function flushes the instruction cache for the specified process.
  113. Arguments:
  114. ProcessHandle - Supplies a handle to the process in which the instruction
  115. cache is to be flushed. Must have PROCESS_VM_WRITE access
  116. to the specified process.
  117. BaseAddress - Supplies an optional pointer to base of the region that
  118. is flushed.
  119. Length - Supplies the length of the region that is flushed if the base
  120. address is specified.
  121. Return Value:
  122. STATUS_SUCCESS.
  123. --*/
  124. {
  125. KAPC_STATE ApcState;
  126. KPROCESSOR_MODE PreviousMode;
  127. PEPROCESS Process;
  128. NTSTATUS Status;
  129. LOGICAL Retry;
  130. PVOID RangeBase;
  131. SIZE_T RangeLength;
  132. PAGED_CODE();
  133. PreviousMode = KeGetPreviousMode();
  134. //
  135. // If the base address is not specified, or the base address is specified
  136. // and the length is not zero, then flush the specified instruction cache
  137. // range.
  138. //
  139. if ((ARGUMENT_PRESENT(BaseAddress) == FALSE) || (Length != 0)) {
  140. //
  141. // If previous mode is user and the range specified falls in kernel
  142. // address space, return an error.
  143. //
  144. if ((ARGUMENT_PRESENT(BaseAddress) != FALSE) &&
  145. (PreviousMode != KernelMode)) {
  146. try {
  147. ProbeForRead(BaseAddress, Length, sizeof(UCHAR));
  148. } except(EXCEPTION_EXECUTE_HANDLER) {
  149. return GetExceptionCode();
  150. }
  151. }
  152. //
  153. // If the specified process is not the current process, then
  154. // the process must be attached to during the flush.
  155. //
  156. Process = NULL;
  157. if (ProcessHandle != NtCurrentProcess()) {
  158. //
  159. // Reference the specified process checking for PROCESS_VM_WRITE
  160. // access.
  161. //
  162. Status = ObReferenceObjectByHandle(ProcessHandle,
  163. PROCESS_VM_WRITE,
  164. PsProcessType,
  165. PreviousMode,
  166. (PVOID *)&Process,
  167. NULL);
  168. if (!NT_SUCCESS(Status)) {
  169. return Status;
  170. }
  171. //
  172. // Attach to the process.
  173. //
  174. KeStackAttachProcess (&Process->Pcb, &ApcState);
  175. }
  176. //
  177. // If the base address is not specified, sweep the entire instruction
  178. // cache. If the base address is specified, flush the specified range.
  179. //
  180. if (ARGUMENT_PRESENT(BaseAddress) == FALSE) {
  181. KeSweepIcache(FALSE);
  182. } else {
  183. //
  184. // Parts of the specified range may be invalid. An exception
  185. // handler is used to skip over those parts. Before calling
  186. // KeSweepIcacheRange, we set Retry to FALSE. If an access
  187. // violation occurs in KeSweepIcacheRange, the MiFlushRangeFilter
  188. // exception filter is called. It updates RangeBase and
  189. // RangeLength to skip over the failing page, and sets Retry to
  190. // TRUE. As long as Retry is TRUE, we continue to call
  191. // KeSweepIcacheRange.
  192. //
  193. RangeBase = BaseAddress;
  194. RangeLength = Length;
  195. do {
  196. Retry = FALSE;
  197. try {
  198. KeSweepIcacheRange(FALSE, RangeBase, RangeLength);
  199. } except(MiFlushRangeFilter(GetExceptionInformation(),
  200. &RangeBase,
  201. &RangeLength,
  202. &Retry)) {
  203. NOTHING;
  204. }
  205. } while (Retry != FALSE);
  206. }
  207. //
  208. // If the specified process is not the current process, then
  209. // detach from it and dereference it.
  210. //
  211. if (Process != NULL) {
  212. KeUnstackDetachProcess (&ApcState);
  213. ObDereferenceObject(Process);
  214. }
  215. }
  216. return STATUS_SUCCESS;
  217. }