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.

208 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1990-2001 Microsoft Corporation
  3. Module Name:
  4. kdmove.c
  5. Abstract:
  6. This module contains code to implement the portable kernel debugger
  7. memory mover.
  8. Author:
  9. Mark Lucovsky (markl) 31-Aug-1990
  10. Revision History:
  11. --*/
  12. #include "kdp.h"
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text(PAGEKD, KdpQuickMoveMemory)
  15. #pragma alloc_text(PAGEKD, KdpCopyMemoryChunks)
  16. #endif
  17. VOID
  18. KdpQuickMoveMemory (
  19. IN PCHAR Destination,
  20. IN PCHAR Source,
  21. IN ULONG Length
  22. )
  23. /*++
  24. Routine Description:
  25. This routine does the exact same thing as RtlCopyMemory, BUT it is
  26. private to the debugger. This allows folks to set breakpoints and
  27. watch points in RtlCopyMemory without risk of recursive debugger
  28. entry and the accompanying hang.
  29. N.B. UNLIKE KdpCopyMemoryChunks, this routine does NOT check for
  30. accessibility and may fault! Use it ONLY in the debugger and ONLY
  31. where you could use RtlCopyMemory.
  32. Arguments:
  33. Destination - Supplies a pointer to destination of the move operation.
  34. Source - Supplies a pointer to the source of the move operation.
  35. Length - Supplies the length of the move operation.
  36. Return Value:
  37. None.
  38. --*/
  39. {
  40. while (Length > 0) {
  41. *Destination = *Source;
  42. Destination++;
  43. Source++;
  44. Length--;
  45. }
  46. }
  47. NTSTATUS
  48. KdpCopyMemoryChunks(
  49. ULONG64 Address,
  50. PVOID Buffer,
  51. ULONG TotalSize,
  52. ULONG ChunkSize,
  53. ULONG Flags,
  54. PULONG ActualSize OPTIONAL
  55. )
  56. /*++
  57. Routine Description:
  58. Copies memory to/from a buffer to/from a system address.
  59. The address can be physical or virtual.
  60. The buffer is assumed to be valid for the duration of this call.
  61. Arguments:
  62. Address - System address.
  63. Buffer - Buffer to read from or write to.
  64. TotalSize - Number of bytes to read/write.
  65. ChunkSize - Maximum single item transfer size, must
  66. be 1, 2, 4 or 8.
  67. 0 means choose a default.
  68. Flags - MMDBG_COPY flags for MmDbgCopyMemory.
  69. ActualSize - Number of bytes actually read/written.
  70. Return Value:
  71. NTSTATUS
  72. --*/
  73. {
  74. ULONG Length;
  75. ULONG CopyChunk;
  76. NTSTATUS Status;
  77. #if defined(_IA64_)
  78. ULONG64 AddressStart = Address;
  79. #endif
  80. if (ChunkSize > MMDBG_COPY_MAX_SIZE) {
  81. ChunkSize = MMDBG_COPY_MAX_SIZE;
  82. } else if (ChunkSize == 0) {
  83. // Default to 4 byte chunks as that's
  84. // what the previous code did.
  85. ChunkSize = 4;
  86. }
  87. //
  88. // MmDbgCopyMemory only copies a single aligned chunk at a
  89. // time. It is Kd's responsibility to chunk up a larger
  90. // request for individual copy requests. This gives Kd
  91. // the flexibility to pick a chunk size and also frees
  92. // Mm from having to worry about more than a page at a time.
  93. // Additionally, it is important that we access memory with the
  94. // largest size possible because we could be accessing
  95. // memory-mapped I/O space.
  96. //
  97. Length = TotalSize;
  98. CopyChunk = 1;
  99. while (Length > 0) {
  100. // Expand the chunk size as long as:
  101. // We haven't hit the chunk limit.
  102. // We have enough data left.
  103. // The address is properly aligned.
  104. while (CopyChunk < ChunkSize &&
  105. (CopyChunk << 1) <= Length &&
  106. (Address & ((CopyChunk << 1) - 1)) == 0) {
  107. CopyChunk <<= 1;
  108. }
  109. // Shrink the chunk size to fit the available data.
  110. while (CopyChunk > Length) {
  111. CopyChunk >>= 1;
  112. }
  113. Status = MmDbgCopyMemory(Address, Buffer, CopyChunk, Flags);
  114. if (!NT_SUCCESS(Status)) {
  115. break;
  116. }
  117. Address += CopyChunk;
  118. Buffer = (PVOID)((PUCHAR)Buffer + CopyChunk);
  119. Length -= CopyChunk;
  120. }
  121. if (ActualSize)
  122. {
  123. *ActualSize = TotalSize - Length;
  124. }
  125. //
  126. // Flush the instruction cache in case the write was into the instruction
  127. // stream. Only do this when writing into the kernel address space,
  128. // and if any bytes were actually written
  129. //
  130. if ((Flags & MMDBG_COPY_WRITE) &&
  131. Length < TotalSize) {
  132. #if defined(_IA64_)
  133. //
  134. // KeSweepCurrentIcacheRange requires a valid virtual address.
  135. // It is used because KeSweepCurrentICache does not work until
  136. // the HAL as been initialized.
  137. //
  138. if (Flags & MMDBG_COPY_PHYSICAL) {
  139. KeSweepCurrentIcache();
  140. } else{
  141. KeSweepCurrentIcacheRange((PVOID)AddressStart, TotalSize - Length);
  142. }
  143. #else
  144. KeSweepCurrentIcache();
  145. #endif
  146. }
  147. return Length != 0 ? STATUS_UNSUCCESSFUL : STATUS_SUCCESS;
  148. }