dbgport.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. dbgport.c
  5. Abstract:
  6. This module attempts to set up the kernel debugging transport.
  7. Author:
  8. Evan Green 25-Mar-2014
  9. Environment:
  10. Boot
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/kernel/kernel.h>
  16. #include "firmware.h"
  17. #include "bootlib.h"
  18. #include "paging.h"
  19. #include "loader.h"
  20. #include "dbgport.h"
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // Define the amount of uncached memory to allocate for the debug device.
  26. //
  27. #define DEBUG_DEVICE_MEMORY_SIZE 0x2000
  28. //
  29. // ------------------------------------------------------ Data Type Definitions
  30. //
  31. //
  32. // ----------------------------------------------- Internal Function Prototypes
  33. //
  34. KSTATUS
  35. BopMapDebugPortTable (
  36. PKERNEL_INITIALIZATION_BLOCK KernelParameters,
  37. PDEBUG_PORT_TABLE2 Table
  38. );
  39. //
  40. // -------------------------------------------------------------------- Globals
  41. //
  42. //
  43. // ------------------------------------------------------------------ Functions
  44. //
  45. VOID
  46. BoSetUpKernelDebugTransport (
  47. PKERNEL_INITIALIZATION_BLOCK KernelParameters
  48. )
  49. /*++
  50. Routine Description:
  51. This routine attempts to set up the kernel debugger transport.
  52. Arguments:
  53. KernelParameters - Supplies a pointer to the kernel initialization block.
  54. Return Value:
  55. None. Failure here is not fatal so it is not reported.
  56. --*/
  57. {
  58. PDEBUG_PORT_TABLE2 DebugPortTable;
  59. PDEBUG_PORT_TABLE2 GeneratedDebugPortTable;
  60. KSTATUS Status;
  61. GeneratedDebugPortTable = NULL;
  62. //
  63. // Go exploring PCI for debug devices. This is done even if there is a
  64. // debug port table so that legacy interrupts can be found and squelched.
  65. //
  66. Status = BopExploreForDebugDevice(&GeneratedDebugPortTable);
  67. if (!KSUCCESS(Status)) {
  68. return;
  69. }
  70. //
  71. // Find the debug port table. If there isn't one, use the generated one.
  72. // Otherwise, free the generated one.
  73. //
  74. DebugPortTable = BoGetAcpiTable(DBG2_SIGNATURE, NULL);
  75. if (DebugPortTable == NULL) {
  76. DebugPortTable = GeneratedDebugPortTable;
  77. } else if (GeneratedDebugPortTable != NULL) {
  78. BoFreeMemory(GeneratedDebugPortTable);
  79. GeneratedDebugPortTable = NULL;
  80. }
  81. if (GeneratedDebugPortTable != NULL) {
  82. BoAddFirmwareTable(KernelParameters, GeneratedDebugPortTable);
  83. }
  84. if (DebugPortTable != NULL) {
  85. BopMapDebugPortTable(KernelParameters, DebugPortTable);
  86. }
  87. return;
  88. }
  89. //
  90. // --------------------------------------------------------- Internal Functions
  91. //
  92. KSTATUS
  93. BopMapDebugPortTable (
  94. PKERNEL_INITIALIZATION_BLOCK KernelParameters,
  95. PDEBUG_PORT_TABLE2 Table
  96. )
  97. /*++
  98. Routine Description:
  99. This routine maps the given debug port table and creates a system resource.
  100. Arguments:
  101. KernelParameters - Supplies a pointer to the kernel initialization block.
  102. Table - Supplies a pointer to the debug port table.
  103. Return Value:
  104. Status code.
  105. --*/
  106. {
  107. PGENERIC_ADDRESS Address;
  108. ULONG AddressIndex;
  109. PULONG AddressSize;
  110. PDEBUG_DEVICE_INFORMATION Device;
  111. ULONG DeviceIndex;
  112. PSYSTEM_RESOURCE_DEBUG_DEVICE Resource;
  113. KSTATUS Status;
  114. if (Table->DeviceInformationCount == 0) {
  115. return STATUS_SUCCESS;
  116. }
  117. Resource = NULL;
  118. //
  119. // Loop through every debug device.
  120. //
  121. Device = (PDEBUG_DEVICE_INFORMATION)((PUCHAR)Table +
  122. Table->DeviceInformationOffset);
  123. for (DeviceIndex = 0;
  124. DeviceIndex < Table->DeviceInformationCount;
  125. DeviceIndex += 1) {
  126. //
  127. // Work through every address.
  128. //
  129. Address = (PGENERIC_ADDRESS)((PUCHAR)Device +
  130. READ_UNALIGNED16(&(Device->BaseAddressRegisterOffset)));
  131. AddressSize = (PULONG)((PUCHAR)Device +
  132. READ_UNALIGNED16(&(Device->AddressSizeOffset)));
  133. for (AddressIndex = 0;
  134. AddressIndex < Device->GenericAddressCount;
  135. AddressIndex += 1) {
  136. //
  137. // If it's memory, map it.
  138. //
  139. if (Address->AddressSpaceId == AddressSpaceMemory) {
  140. Resource =
  141. BoAllocateMemory(sizeof(SYSTEM_RESOURCE_DEBUG_DEVICE));
  142. if (Resource == NULL) {
  143. Status = STATUS_INSUFFICIENT_RESOURCES;
  144. goto MapDebugPortTableEnd;
  145. }
  146. RtlZeroMemory(Resource, sizeof(SYSTEM_RESOURCE_DEBUG_DEVICE));
  147. Resource->Header.Type = SystemResourceDebugDevice;
  148. Resource->Header.PhysicalAddress =
  149. READ_UNALIGNED64(&(Address->Address));
  150. Resource->Header.Size = READ_UNALIGNED32(AddressSize);
  151. Resource->Header.VirtualAddress = (PVOID)-1;
  152. Status = BoMapPhysicalAddress(
  153. &(Resource->Header.VirtualAddress),
  154. Resource->Header.PhysicalAddress,
  155. Resource->Header.Size,
  156. MAP_FLAG_CACHE_DISABLE | MAP_FLAG_GLOBAL,
  157. MemoryTypeHardware);
  158. if (!KSUCCESS(Status)) {
  159. goto MapDebugPortTableEnd;
  160. }
  161. INSERT_BEFORE(&(Resource->Header.ListEntry),
  162. &(KernelParameters->SystemResourceListHead));
  163. }
  164. //
  165. // Move to the next address.
  166. //
  167. Address += 1;
  168. AddressSize += 1;
  169. }
  170. //
  171. // Move to the next device.
  172. //
  173. Device = (PDEBUG_DEVICE_INFORMATION)((PUCHAR)Device +
  174. READ_UNALIGNED16(&(Device->Length)));
  175. }
  176. Status = STATUS_SUCCESS;
  177. MapDebugPortTableEnd:
  178. if (!KSUCCESS(Status)) {
  179. if (Resource != NULL) {
  180. if (Resource->Header.VirtualAddress != NULL) {
  181. BoUnmapPhysicalAddress(Resource->Header.VirtualAddress,
  182. Resource->Header.Size / MmPageSize());
  183. }
  184. BoFreeMemory(Resource);
  185. }
  186. }
  187. return Status;
  188. }