2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/kd64/kdapi.c
5 * PURPOSE: KD64 Public Routines and Internal Support
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* PRIVATE FUNCTIONS *********************************************************/
19 KdpCopyMemoryChunks(IN ULONG64 Address
,
24 OUT PULONG ActualSize OPTIONAL
)
29 /* Check if this is physical or virtual copy */
30 if (Flags
& MMDBG_COPY_PHYSICAL
)
32 /* Fail physical memory read/write for now */
33 if (Flags
& MMDBG_COPY_WRITE
)
35 KdpDprintf("KdpCopyMemoryChunks: Failing write for Physical Address 0x%I64x Length: %x\n",
41 KdpDprintf("KdpCopyMemoryChunks: Failing read for Physical Address 0x%I64x Length: %x\n",
48 Status
= STATUS_UNSUCCESSFUL
;
52 /* Protect against NULL */
55 if (ActualSize
) *ActualSize
= 0;
56 return STATUS_UNSUCCESSFUL
;
59 /* Check if this is read or write */
60 if (Flags
& MMDBG_COPY_WRITE
)
63 RtlCopyMemory((PVOID
)(ULONG_PTR
)Address
,
71 (PVOID
)(ULONG_PTR
)Address
,
75 /* Set size and status */
77 Status
= STATUS_SUCCESS
;
80 /* Return the actual length if requested */
81 if (ActualSize
) *ActualSize
= Length
;
89 KdpQueryMemory(IN PDBGKD_MANIPULATE_STATE64 State
,
92 PDBGKD_QUERY_MEMORY Memory
= &State
->u
.QueryMemory
;
94 NTSTATUS Status
= STATUS_SUCCESS
;
96 /* Validate the address space */
97 if (Memory
->AddressSpace
== DBGKD_QUERY_MEMORY_VIRTUAL
)
99 /* Check if this is process memory */
100 if ((PVOID
)(ULONG_PTR
)Memory
->Address
< MmHighestUserAddress
)
103 Memory
->AddressSpace
= DBGKD_QUERY_MEMORY_PROCESS
;
107 /* FIXME: Check if it's session space */
108 Memory
->AddressSpace
= DBGKD_QUERY_MEMORY_KERNEL
;
112 Memory
->Flags
= DBGKD_QUERY_MEMORY_READ
|
113 DBGKD_QUERY_MEMORY_WRITE
|
114 DBGKD_QUERY_MEMORY_EXECUTE
;
119 Status
= STATUS_INVALID_PARAMETER
;
122 /* Return structure */
123 State
->ReturnStatus
= Status
;
124 Memory
->Reserved
= 0;
127 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
128 Header
.Buffer
= (PCHAR
)State
;
130 /* Send the packet */
131 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
139 KdpSearchMemory(IN PDBGKD_MANIPULATE_STATE64 State
,
144 KdpDprintf("KdpSearchMemory called\n");
150 KdpFillMemory(IN PDBGKD_MANIPULATE_STATE64 State
,
155 KdpDprintf("KdpFillMemory called\n");
161 KdpWriteBreakpoint(IN PDBGKD_MANIPULATE_STATE64 State
,
165 PDBGKD_WRITE_BREAKPOINT64 Breakpoint
= &State
->u
.WriteBreakPoint
;
169 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
170 Header
.Buffer
= (PCHAR
)State
;
171 ASSERT(Data
->Length
== 0);
173 /* Create the breakpoint */
174 Breakpoint
->BreakPointHandle
=
175 KdpAddBreakpoint((PVOID
)(ULONG_PTR
)Breakpoint
->BreakPointAddress
);
176 if (!Breakpoint
->BreakPointHandle
)
179 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
184 State
->ReturnStatus
= STATUS_SUCCESS
;
187 /* Send the packet */
188 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
196 KdpRestoreBreakpoint(IN PDBGKD_MANIPULATE_STATE64 State
,
200 PDBGKD_RESTORE_BREAKPOINT RestoreBp
= &State
->u
.RestoreBreakPoint
;
203 /* Fill out the header */
204 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
205 Header
.Buffer
= (PCHAR
)State
;
206 ASSERT(Data
->Length
== 0);
208 /* Get the version block */
209 if (KdpDeleteBreakpoint(RestoreBp
->BreakPointHandle
))
212 State
->ReturnStatus
= STATUS_SUCCESS
;
217 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
220 /* Send the packet */
221 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
229 KdpWriteBreakPointEx(IN PDBGKD_MANIPULATE_STATE64 State
,
234 KdpDprintf("KdpWriteBreakPointEx called\n");
236 return STATUS_UNSUCCESSFUL
;
241 KdpRestoreBreakPointEx(IN PDBGKD_MANIPULATE_STATE64 State
,
246 KdpDprintf("KdpRestoreBreakPointEx called\n");
252 DumpTraceData(OUT PSTRING TraceData
)
254 /* Update the buffer */
255 TraceDataBuffer
[0] = TraceDataBufferPosition
;
257 /* Setup the trace data */
258 TraceData
->Length
= TraceDataBufferPosition
* sizeof(ULONG
);
259 TraceData
->Buffer
= (PCHAR
)TraceDataBuffer
;
261 /* Reset the buffer location */
262 TraceDataBufferPosition
= 1;
267 KdpSetCommonState(IN ULONG NewState
,
269 OUT PDBGKD_WAIT_STATE_CHANGE64 WaitStateChange
)
271 USHORT InstructionCount
;
272 BOOLEAN HadBreakpoints
;
274 /* Setup common stuff available for all CPU architectures */
275 WaitStateChange
->NewState
= NewState
;
276 WaitStateChange
->ProcessorLevel
= KeProcessorLevel
;
277 WaitStateChange
->Processor
= (USHORT
)KeGetCurrentPrcb()->Number
;
278 WaitStateChange
->NumberProcessors
= (ULONG
)KeNumberProcessors
;
279 WaitStateChange
->Thread
= (ULONG64
)(LONG_PTR
)KeGetCurrentThread();
280 WaitStateChange
->ProgramCounter
= (ULONG64
)(LONG_PTR
)KeGetContextPc(Context
);
282 /* Zero out the Control Report */
283 RtlZeroMemory(&WaitStateChange
->ControlReport
,
284 sizeof(DBGKD_CONTROL_REPORT
));
286 /* Now copy the instruction stream and set the count */
287 RtlCopyMemory(&WaitStateChange
->ControlReport
.InstructionStream
[0],
288 (PVOID
)(ULONG_PTR
)WaitStateChange
->ProgramCounter
,
290 InstructionCount
= DBGKD_MAXSTREAM
;
291 WaitStateChange
->ControlReport
.InstructionCount
= InstructionCount
;
293 /* Clear all the breakpoints in this region */
295 KdpDeleteBreakpointRange((PVOID
)(ULONG_PTR
)WaitStateChange
->ProgramCounter
,
296 (PVOID
)((ULONG_PTR
)WaitStateChange
->ProgramCounter
+
297 WaitStateChange
->ControlReport
.InstructionCount
- 1));
300 /* Copy the instruction stream again, this time without breakpoints */
301 RtlCopyMemory(&WaitStateChange
->ControlReport
.InstructionStream
[0],
302 (PVOID
)(ULONG_PTR
)WaitStateChange
->ProgramCounter
,
303 WaitStateChange
->ControlReport
.InstructionCount
);
309 KdpGetVersion(IN PDBGKD_MANIPULATE_STATE64 State
)
313 /* Fill out the header */
314 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
315 Header
.Buffer
= (PCHAR
)State
;
317 /* Get the version block */
318 KdpSysGetVersion(&State
->u
.GetVersion64
);
320 /* Fill out the state */
321 State
->ApiNumber
= DbgKdGetVersionApi
;
322 State
->ReturnStatus
= STATUS_SUCCESS
;
324 /* Send the packet */
325 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
333 KdpReadVirtualMemory(IN PDBGKD_MANIPULATE_STATE64 State
,
337 PDBGKD_READ_MEMORY64 ReadMemory
= &State
->u
.ReadMemory
;
339 ULONG Length
= ReadMemory
->TransferCount
;
340 ULONG64 TargetBaseAddress
= State
->u
.ReadMemory
.TargetBaseAddress
;
342 /* Setup the header */
343 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
344 Header
.Buffer
= (PCHAR
)State
;
345 ASSERT(Data
->Length
== 0);
347 /* Validate length */
348 if (Length
> (PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
)))
350 /* Overflow, set it to maximum possible */
351 Length
= PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
);
355 State
->ReturnStatus
= KdpCopyMemoryChunks(ReadMemory
->TargetBaseAddress
,
362 /* Return the actual length read */
363 Data
->Length
= ReadMemory
->ActualBytesRead
= Length
;
365 /* Send the packet */
366 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
374 KdpWriteVirtualMemory(IN PDBGKD_MANIPULATE_STATE64 State
,
378 PDBGKD_WRITE_MEMORY64 WriteMemory
= &State
->u
.WriteMemory
;
381 /* Setup the header */
382 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
383 Header
.Buffer
= (PCHAR
)State
;
386 State
->ReturnStatus
= KdpCopyMemoryChunks(WriteMemory
->TargetBaseAddress
,
392 &WriteMemory
->ActualBytesWritten
);
394 /* Send the packet */
395 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
403 KdpReadPhysicalmemory(IN PDBGKD_MANIPULATE_STATE64 State
,
407 PDBGKD_READ_MEMORY64 ReadMemory
= &State
->u
.ReadMemory
;
409 ULONG Length
= ReadMemory
->TransferCount
;
410 ULONG Flags
, CacheFlags
;
412 /* Setup the header */
413 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
414 Header
.Buffer
= (PCHAR
)State
;
415 ASSERT(Data
->Length
== 0);
417 /* Validate length */
418 if (Length
> (PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
)))
420 /* Overflow, set it to maximum possible */
421 Length
= PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
);
424 /* Start with the default flags */
425 Flags
= MMDBG_COPY_UNSAFE
| MMDBG_COPY_PHYSICAL
;
427 /* Get the caching flags and check if a type is specified */
428 CacheFlags
= ReadMemory
->ActualBytesRead
;
429 if (CacheFlags
== DBGKD_CACHING_CACHED
)
432 Flags
|= MMDBG_COPY_CACHED
;
434 else if (CacheFlags
== DBGKD_CACHING_UNCACHED
)
437 Flags
|= MMDBG_COPY_UNCACHED
;
439 else if (CacheFlags
== DBGKD_CACHING_UNCACHED
)
442 Flags
|= DBGKD_CACHING_WRITE_COMBINED
;
446 State
->ReturnStatus
= KdpCopyMemoryChunks(ReadMemory
->TargetBaseAddress
,
453 /* Return the actual length read */
454 Data
->Length
= ReadMemory
->ActualBytesRead
= Length
;
456 /* Send the packet */
457 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
465 KdpWritePhysicalmemory(IN PDBGKD_MANIPULATE_STATE64 State
,
469 PDBGKD_WRITE_MEMORY64 WriteMemory
= &State
->u
.WriteMemory
;
471 ULONG Flags
, CacheFlags
;
473 /* Setup the header */
474 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
475 Header
.Buffer
= (PCHAR
)State
;
477 /* Start with the default flags */
478 Flags
= MMDBG_COPY_UNSAFE
| MMDBG_COPY_WRITE
| MMDBG_COPY_PHYSICAL
;
480 /* Get the caching flags and check if a type is specified */
481 CacheFlags
= WriteMemory
->ActualBytesWritten
;
482 if (CacheFlags
== DBGKD_CACHING_CACHED
)
485 Flags
|= MMDBG_COPY_CACHED
;
487 else if (CacheFlags
== DBGKD_CACHING_UNCACHED
)
490 Flags
|= MMDBG_COPY_UNCACHED
;
492 else if (CacheFlags
== DBGKD_CACHING_UNCACHED
)
495 Flags
|= DBGKD_CACHING_WRITE_COMBINED
;
499 State
->ReturnStatus
= KdpCopyMemoryChunks(WriteMemory
->TargetBaseAddress
,
504 &WriteMemory
->ActualBytesWritten
);
506 /* Send the packet */
507 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
515 KdpReadControlSpace(IN PDBGKD_MANIPULATE_STATE64 State
,
519 PDBGKD_READ_MEMORY64 ReadMemory
= &State
->u
.ReadMemory
;
523 /* Setup the header */
524 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
525 Header
.Buffer
= (PCHAR
)State
;
526 ASSERT(Data
->Length
== 0);
528 /* Check the length requested */
529 Length
= ReadMemory
->TransferCount
;
530 if (Length
> (PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
)))
532 /* Use maximum allowed */
533 Length
= PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
);
536 /* Call the internal routine */
537 State
->ReturnStatus
= KdpSysReadControlSpace(State
->Processor
,
538 ReadMemory
->TargetBaseAddress
,
543 /* Return the actual length read */
544 Data
->Length
= ReadMemory
->ActualBytesRead
= Length
;
547 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
555 KdpWriteControlSpace(IN PDBGKD_MANIPULATE_STATE64 State
,
559 PDBGKD_WRITE_MEMORY64 WriteMemory
= &State
->u
.WriteMemory
;
562 /* Setup the header */
563 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
564 Header
.Buffer
= (PCHAR
)State
;
566 /* Call the internal routine */
567 State
->ReturnStatus
= KdpSysWriteControlSpace(State
->Processor
,
568 WriteMemory
->TargetBaseAddress
,
571 &WriteMemory
->ActualBytesWritten
);
574 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
582 KdpGetContext(IN PDBGKD_MANIPULATE_STATE64 State
,
589 /* Setup the header */
590 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
591 Header
.Buffer
= (PCHAR
)State
;
592 ASSERT(Data
->Length
== 0);
594 /* Make sure that this is a valid request */
595 if (State
->Processor
< KeNumberProcessors
)
597 /* Check if the request is for this CPU */
598 if (State
->Processor
== KeGetCurrentPrcb()->Number
)
600 /* We're just copying our own context */
601 ControlStart
= Context
;
605 /* SMP not yet handled */
606 KdpDprintf("KdpGetContext: SMP UNHANDLED\n");
611 /* Copy the memory */
612 RtlCopyMemory(Data
->Buffer
, ControlStart
, sizeof(CONTEXT
));
613 Data
->Length
= sizeof(CONTEXT
);
616 State
->ReturnStatus
= STATUS_SUCCESS
;
620 /* Invalid request */
621 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
625 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
633 KdpSetContext(IN PDBGKD_MANIPULATE_STATE64 State
,
640 /* Setup the header */
641 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
642 Header
.Buffer
= (PCHAR
)State
;
643 ASSERT(Data
->Length
== sizeof(CONTEXT
));
645 /* Make sure that this is a valid request */
646 if (State
->Processor
< KeNumberProcessors
)
648 /* Check if the request is for this CPU */
649 if (State
->Processor
== KeGetCurrentPrcb()->Number
)
651 /* We're just copying our own context */
652 ControlStart
= Context
;
656 /* SMP not yet handled */
657 KdpDprintf("KdpSetContext: SMP UNHANDLED\n");
662 /* Copy the memory */
663 RtlCopyMemory(ControlStart
, Data
->Buffer
, sizeof(CONTEXT
));
666 State
->ReturnStatus
= STATUS_SUCCESS
;
670 /* Invalid request */
671 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
675 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
683 KdpCauseBugCheck(IN PDBGKD_MANIPULATE_STATE64 State
)
685 /* Crash with the special code */
686 KeBugCheck(MANUALLY_INITIATED_CRASH
);
691 KdpReadMachineSpecificRegister(IN PDBGKD_MANIPULATE_STATE64 State
,
696 PDBGKD_READ_WRITE_MSR ReadMsr
= &State
->u
.ReadWriteMsr
;
697 LARGE_INTEGER MsrValue
;
699 /* Setup the header */
700 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
701 Header
.Buffer
= (PCHAR
)State
;
702 ASSERT(Data
->Length
== 0);
704 /* Call the internal routine */
705 State
->ReturnStatus
= KdpSysReadMsr(ReadMsr
->Msr
,
708 /* Return the data */
709 ReadMsr
->DataValueLow
= MsrValue
.LowPart
;
710 ReadMsr
->DataValueHigh
= MsrValue
.HighPart
;
713 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
721 KdpWriteMachineSpecificRegister(IN PDBGKD_MANIPULATE_STATE64 State
,
726 PDBGKD_READ_WRITE_MSR WriteMsr
= &State
->u
.ReadWriteMsr
;
727 LARGE_INTEGER MsrValue
;
729 /* Setup the header */
730 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
731 Header
.Buffer
= (PCHAR
)State
;
732 ASSERT(Data
->Length
== 0);
734 /* Call the internal routine */
735 MsrValue
.LowPart
= WriteMsr
->DataValueLow
;
736 MsrValue
.HighPart
= WriteMsr
->DataValueHigh
;
737 State
->ReturnStatus
= KdpSysWriteMsr(WriteMsr
->Msr
,
741 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
749 KdpGetBusData(IN PDBGKD_MANIPULATE_STATE64 State
,
754 PDBGKD_GET_SET_BUS_DATA GetBusData
= &State
->u
.GetSetBusData
;
757 /* Setup the header */
758 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
759 Header
.Buffer
= (PCHAR
)State
;
760 ASSERT(Data
->Length
== 0);
762 /* Check the length requested */
763 Length
= GetBusData
->Length
;
764 if (Length
> (PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
)))
766 /* Use maximum allowed */
767 Length
= PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
);
770 /* Call the internal routine */
771 State
->ReturnStatus
= KdpSysReadBusData(GetBusData
->BusDataType
,
772 GetBusData
->BusNumber
,
773 GetBusData
->SlotNumber
,
779 /* Return the actual length read */
780 Data
->Length
= GetBusData
->Length
= Length
;
783 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
791 KdpSetBusData(IN PDBGKD_MANIPULATE_STATE64 State
,
796 PDBGKD_GET_SET_BUS_DATA SetBusData
= &State
->u
.GetSetBusData
;
799 /* Setup the header */
800 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
801 Header
.Buffer
= (PCHAR
)State
;
803 /* Call the internal routine */
804 State
->ReturnStatus
= KdpSysWriteBusData(SetBusData
->BusDataType
,
805 SetBusData
->BusNumber
,
806 SetBusData
->SlotNumber
,
812 /* Return the actual length written */
813 SetBusData
->Length
= Length
;
816 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
824 KdpReadIoSpace(IN PDBGKD_MANIPULATE_STATE64 State
,
829 PDBGKD_READ_WRITE_IO64 ReadIo
= &State
->u
.ReadWriteIo
;
831 /* Setup the header */
832 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
833 Header
.Buffer
= (PCHAR
)State
;
834 ASSERT(Data
->Length
== 0);
837 * Clear the value so 1 or 2 byte reads
838 * don't leave the higher bits unmodified
840 ReadIo
->DataValue
= 0;
842 /* Call the internal routine */
843 State
->ReturnStatus
= KdpSysReadIoSpace(Isa
,
852 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
860 KdpWriteIoSpace(IN PDBGKD_MANIPULATE_STATE64 State
,
865 PDBGKD_READ_WRITE_IO64 WriteIo
= &State
->u
.ReadWriteIo
;
867 /* Setup the header */
868 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
869 Header
.Buffer
= (PCHAR
)State
;
870 ASSERT(Data
->Length
== 0);
872 /* Call the internal routine */
873 State
->ReturnStatus
= KdpSysWriteIoSpace(Isa
,
882 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
890 KdpReadIoSpaceExtended(IN PDBGKD_MANIPULATE_STATE64 State
,
895 PDBGKD_READ_WRITE_IO_EXTENDED64 ReadIoExtended
= &State
->u
.
898 /* Setup the header */
899 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
900 Header
.Buffer
= (PCHAR
)State
;
901 ASSERT(Data
->Length
== 0);
904 * Clear the value so 1 or 2 byte reads
905 * don't leave the higher bits unmodified
907 ReadIoExtended
->DataValue
= 0;
909 /* Call the internal routine */
910 State
->ReturnStatus
= KdpSysReadIoSpace(ReadIoExtended
->InterfaceType
,
911 ReadIoExtended
->BusNumber
,
912 ReadIoExtended
->AddressSpace
,
913 ReadIoExtended
->IoAddress
,
914 &ReadIoExtended
->DataValue
,
915 ReadIoExtended
->DataSize
,
916 &ReadIoExtended
->DataSize
);
919 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
927 KdpWriteIoSpaceExtended(IN PDBGKD_MANIPULATE_STATE64 State
,
932 PDBGKD_READ_WRITE_IO_EXTENDED64 WriteIoExtended
= &State
->u
.
935 /* Setup the header */
936 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
937 Header
.Buffer
= (PCHAR
)State
;
938 ASSERT(Data
->Length
== 0);
940 /* Call the internal routine */
941 State
->ReturnStatus
= KdpSysReadIoSpace(WriteIoExtended
->InterfaceType
,
942 WriteIoExtended
->BusNumber
,
943 WriteIoExtended
->AddressSpace
,
944 WriteIoExtended
->IoAddress
,
945 &WriteIoExtended
->DataValue
,
946 WriteIoExtended
->DataSize
,
947 &WriteIoExtended
->DataSize
);
950 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
958 KdpCheckLowMemory(IN PDBGKD_MANIPULATE_STATE64 State
)
962 /* Setup the header */
963 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
964 Header
.Buffer
= (PCHAR
)State
;
966 /* Call the internal routine */
967 State
->ReturnStatus
= KdpSysCheckLowMemory(MMDBG_COPY_UNSAFE
);
970 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
978 KdpSendWaitContinue(IN ULONG PacketType
,
979 IN PSTRING SendHeader
,
980 IN PSTRING SendData OPTIONAL
,
981 IN OUT PCONTEXT Context
)
984 DBGKD_MANIPULATE_STATE64 ManipulateState
;
988 /* Setup the Manipulate State structure */
989 Header
.MaximumLength
= sizeof(DBGKD_MANIPULATE_STATE64
);
990 Header
.Buffer
= (PCHAR
)&ManipulateState
;
991 Data
.MaximumLength
= sizeof(KdpMessageBuffer
);
992 Data
.Buffer
= KdpMessageBuffer
;
993 //KdpContextSent = FALSE;
996 /* Send the Packet */
997 KdSendPacket(PacketType
, SendHeader
, SendData
, &KdpContext
);
999 /* If the debugger isn't present anymore, just return success */
1000 if (KdDebuggerNotPresent
) return ContinueSuccess
;
1002 /* Main processing Loop */
1008 /* Wait to get a reply to our packet */
1009 RecvCode
= KdReceivePacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
1015 /* If we got a resend request, do it */
1016 if (RecvCode
== KdPacketNeedsResend
) goto SendPacket
;
1017 } while (RecvCode
== KdPacketTimedOut
);
1019 /* Now check what API we got */
1020 switch (ManipulateState
.ApiNumber
)
1022 case DbgKdReadVirtualMemoryApi
:
1024 /* Read virtual memory */
1025 KdpReadVirtualMemory(&ManipulateState
, &Data
, Context
);
1028 case DbgKdWriteVirtualMemoryApi
:
1030 /* Write virtual memory */
1031 KdpWriteVirtualMemory(&ManipulateState
, &Data
, Context
);
1034 case DbgKdGetContextApi
:
1036 /* Get the current context */
1037 KdpGetContext(&ManipulateState
, &Data
, Context
);
1040 case DbgKdSetContextApi
:
1042 /* Set a new context */
1043 KdpSetContext(&ManipulateState
, &Data
, Context
);
1046 case DbgKdWriteBreakPointApi
:
1048 /* Write the breakpoint */
1049 KdpWriteBreakpoint(&ManipulateState
, &Data
, Context
);
1052 case DbgKdRestoreBreakPointApi
:
1054 /* Restore the breakpoint */
1055 KdpRestoreBreakpoint(&ManipulateState
, &Data
, Context
);
1058 case DbgKdContinueApi
:
1060 /* Simply continue */
1061 return NT_SUCCESS(ManipulateState
.u
.Continue
.ContinueStatus
);
1063 case DbgKdReadControlSpaceApi
:
1065 /* Read control space */
1066 KdpReadControlSpace(&ManipulateState
, &Data
, Context
);
1069 case DbgKdWriteControlSpaceApi
:
1071 /* Write control space */
1072 KdpWriteControlSpace(&ManipulateState
, &Data
, Context
);
1075 case DbgKdReadIoSpaceApi
:
1077 /* Read I/O Space */
1078 KdpReadIoSpace(&ManipulateState
, &Data
, Context
);
1081 case DbgKdWriteIoSpaceApi
:
1083 /* Write I/O Space */
1084 KdpWriteIoSpace(&ManipulateState
, &Data
, Context
);
1087 case DbgKdRebootApi
:
1089 /* Reboot the system */
1090 HalReturnToFirmware(HalRebootRoutine
);
1093 case DbgKdContinueApi2
:
1095 /* Check if caller reports success */
1096 if (NT_SUCCESS(ManipulateState
.u
.Continue2
.ContinueStatus
))
1098 /* Update the state */
1099 KdpGetStateChange(&ManipulateState
, Context
);
1100 return ContinueSuccess
;
1104 /* Return an error */
1105 return ContinueError
;
1108 case DbgKdReadPhysicalMemoryApi
:
1110 /* Read physical memory */
1111 KdpReadPhysicalmemory(&ManipulateState
, &Data
, Context
);
1114 case DbgKdWritePhysicalMemoryApi
:
1116 /* Write physical memory */
1117 KdpWritePhysicalmemory(&ManipulateState
, &Data
, Context
);
1120 case DbgKdQuerySpecialCallsApi
:
1123 KdpDprintf("DbgKdQuerySpecialCallsApi called\n");
1127 case DbgKdSetSpecialCallApi
:
1130 KdpDprintf("DbgKdSetSpecialCallApi called\n");
1134 case DbgKdClearSpecialCallsApi
:
1137 KdpDprintf("DbgKdClearSpecialCallsApi called\n");
1141 case DbgKdSetInternalBreakPointApi
:
1144 KdpDprintf("DbgKdSetInternalBreakPointApi called\n");
1148 case DbgKdGetInternalBreakPointApi
:
1151 KdpDprintf("DbgKdGetInternalBreakPointApi called\n");
1155 case DbgKdReadIoSpaceExtendedApi
:
1157 /* Read I/O Space */
1158 KdpReadIoSpaceExtended(&ManipulateState
, &Data
, Context
);
1161 case DbgKdWriteIoSpaceExtendedApi
:
1163 /* Write I/O Space */
1164 KdpWriteIoSpaceExtended(&ManipulateState
, &Data
, Context
);
1167 case DbgKdGetVersionApi
:
1169 /* Get version data */
1170 KdpGetVersion(&ManipulateState
);
1173 case DbgKdWriteBreakPointExApi
:
1175 /* Write the breakpoint and check if it failed */
1176 if (!NT_SUCCESS(KdpWriteBreakPointEx(&ManipulateState
,
1180 /* Return an error */
1181 return ContinueError
;
1185 case DbgKdRestoreBreakPointExApi
:
1187 /* Restore the breakpoint */
1188 KdpRestoreBreakPointEx(&ManipulateState
, &Data
, Context
);
1191 case DbgKdCauseBugCheckApi
:
1193 /* Crash the system */
1194 KdpCauseBugCheck(&ManipulateState
);
1197 case DbgKdSwitchProcessor
:
1200 KdpDprintf("DbgKdSwitchProcessor called\n");
1204 case DbgKdPageInApi
:
1207 KdpDprintf("DbgKdPageInApi called\n");
1211 case DbgKdReadMachineSpecificRegister
:
1213 /* Read from the specified MSR */
1214 KdpReadMachineSpecificRegister(&ManipulateState
, &Data
, Context
);
1217 case DbgKdWriteMachineSpecificRegister
:
1219 /* Write to the specified MSR */
1220 KdpWriteMachineSpecificRegister(&ManipulateState
, &Data
, Context
);
1223 case DbgKdSearchMemoryApi
:
1226 KdpSearchMemory(&ManipulateState
, &Data
, Context
);
1229 case DbgKdGetBusDataApi
:
1231 /* Read from the bus */
1232 KdpGetBusData(&ManipulateState
, &Data
, Context
);
1235 case DbgKdSetBusDataApi
:
1237 /* Write to the bus */
1238 KdpSetBusData(&ManipulateState
, &Data
, Context
);
1241 case DbgKdCheckLowMemoryApi
:
1243 /* Check for memory corruption in the lower 4 GB */
1244 KdpCheckLowMemory(&ManipulateState
);
1247 case DbgKdClearAllInternalBreakpointsApi
:
1249 /* Just clear the counter */
1250 KdpNumInternalBreakpoints
= 0;
1253 case DbgKdFillMemoryApi
:
1256 KdpFillMemory(&ManipulateState
, &Data
, Context
);
1259 case DbgKdQueryMemoryApi
:
1262 KdpQueryMemory(&ManipulateState
, Context
);
1265 case DbgKdSwitchPartition
:
1268 KdpDprintf("DbgKdSwitchPartition called\n");
1272 /* Unsupported Message */
1275 /* Setup an empty message, with failure */
1276 KdpDprintf("Received Unhandled API %lx\n", ManipulateState
.ApiNumber
);
1278 ManipulateState
.ReturnStatus
= STATUS_UNSUCCESSFUL
;
1281 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
1292 KdpReportLoadSymbolsStateChange(IN PSTRING PathName
,
1293 IN PKD_SYMBOLS_INFO SymbolInfo
,
1295 IN OUT PCONTEXT Context
)
1298 STRING Data
, Header
;
1299 DBGKD_WAIT_STATE_CHANGE64 WaitStateChange
;
1300 KCONTINUE_STATUS Status
;
1302 /* Start wait loop */
1305 /* Build the architecture common parts of the message */
1306 KdpSetCommonState(DbgKdLoadSymbolsStateChange
,
1310 /* Now finish creating the structure */
1311 KdpSetContextState(&WaitStateChange
, Context
);
1313 /* Fill out load data */
1314 WaitStateChange
.u
.LoadSymbols
.UnloadSymbols
= Unload
;
1315 WaitStateChange
.u
.LoadSymbols
.BaseOfDll
= (ULONG64
)(LONG_PTR
)SymbolInfo
->BaseOfDll
;
1316 WaitStateChange
.u
.LoadSymbols
.ProcessId
= SymbolInfo
->ProcessId
;
1317 WaitStateChange
.u
.LoadSymbols
.CheckSum
= SymbolInfo
->CheckSum
;
1318 WaitStateChange
.u
.LoadSymbols
.SizeOfImage
= SymbolInfo
->SizeOfImage
;
1320 /* Check if we have a symbol name */
1323 /* Setup the information */
1324 WaitStateChange
.u
.LoadSymbols
.PathNameLength
= PathName
->Length
;
1325 RtlCopyMemory(KdpPathBuffer
, PathName
->Buffer
, PathName
->Length
);
1326 Data
.Buffer
= KdpPathBuffer
;
1327 Data
.Length
= WaitStateChange
.u
.LoadSymbols
.PathNameLength
;
1333 WaitStateChange
.u
.LoadSymbols
.PathNameLength
= 0;
1337 /* Setup the header */
1338 Header
.Length
= sizeof(DBGKD_WAIT_STATE_CHANGE64
);
1339 Header
.Buffer
= (PCHAR
)&WaitStateChange
;
1341 /* Send the packet */
1342 Status
= KdpSendWaitContinue(PACKET_TYPE_KD_STATE_CHANGE64
,
1346 } while (Status
== ContinueProcessorReselected
);
1354 KdpReportExceptionStateChange(IN PEXCEPTION_RECORD ExceptionRecord
,
1355 IN OUT PCONTEXT Context
,
1356 IN BOOLEAN SecondChanceException
)
1358 STRING Header
, Data
;
1359 DBGKD_WAIT_STATE_CHANGE64 WaitStateChange
;
1360 KCONTINUE_STATUS Status
;
1362 /* Start report loop */
1365 /* Build the architecture common parts of the message */
1366 KdpSetCommonState(DbgKdExceptionStateChange
, Context
, &WaitStateChange
);
1368 /* Copy the Exception Record and set First Chance flag */
1369 CopyExceptionRecord(ExceptionRecord
,
1370 &WaitStateChange
.u
.Exception
.ExceptionRecord
);
1371 WaitStateChange
.u
.Exception
.FirstChance
= !SecondChanceException
;
1373 /* Now finish creating the structure */
1374 KdpSetContextState(&WaitStateChange
, Context
);
1376 /* Setup the actual header to send to KD */
1377 Header
.Length
= sizeof(DBGKD_WAIT_STATE_CHANGE64
) - sizeof(CONTEXT
);
1378 Header
.Buffer
= (PCHAR
)&WaitStateChange
;
1380 /* Setup the trace data */
1381 DumpTraceData(&Data
);
1383 /* Send State Change packet and wait for a reply */
1384 Status
= KdpSendWaitContinue(PACKET_TYPE_KD_STATE_CHANGE64
,
1388 } while (Status
== ContinueProcessorReselected
);
1396 KdpTimeSlipDpcRoutine(IN PKDPC Dpc
,
1397 IN PVOID DeferredContext
,
1398 IN PVOID SystemArgument1
,
1399 IN PVOID SystemArgument2
)
1401 LONG OldSlip
, NewSlip
, PendingSlip
;
1403 /* Get the current pending slip */
1404 PendingSlip
= KdpTimeSlipPending
;
1407 /* Save the old value and either disable or enable it now. */
1408 OldSlip
= PendingSlip
;
1409 NewSlip
= OldSlip
> 1 ? 1 : 0;
1411 /* Try to change the value */
1412 } while (InterlockedCompareExchange(&KdpTimeSlipPending
,
1414 OldSlip
) != OldSlip
);
1416 /* If the New Slip value is 1, then do the Time Slipping */
1417 if (NewSlip
) ExQueueWorkItem(&KdpTimeSlipWorkItem
, DelayedWorkQueue
);
1422 KdpTimeSlipWork(IN PVOID Context
)
1425 LARGE_INTEGER DueTime
;
1427 /* Update the System time from the CMOS */
1428 ExAcquireTimeRefreshLock(FALSE
);
1429 ExUpdateSystemTimeFromCmos(FALSE
, 0);
1430 ExReleaseTimeRefreshLock();
1432 /* Check if we have a registered Time Slip Event and signal it */
1433 KeAcquireSpinLock(&KdpTimeSlipEventLock
, &OldIrql
);
1434 if (KdpTimeSlipEvent
) KeSetEvent(KdpTimeSlipEvent
, 0, FALSE
);
1435 KeReleaseSpinLock(&KdpTimeSlipEventLock
, OldIrql
);
1437 /* Delay the DPC until it runs next time */
1438 DueTime
.QuadPart
= -1800000000;
1439 KeSetTimer(&KdpTimeSlipTimer
, DueTime
, &KdpTimeSlipDpc
);
1444 KdpSwitchProcessor(IN PEXCEPTION_RECORD ExceptionRecord
,
1445 IN OUT PCONTEXT ContextRecord
,
1446 IN BOOLEAN SecondChanceException
)
1450 /* Save the port data */
1453 /* Report a state change */
1454 Status
= KdpReportExceptionStateChange(ExceptionRecord
,
1456 SecondChanceException
);
1458 /* Restore the port data and return */
1465 KdpQueryPerformanceCounter(IN PKTRAP_FRAME TrapFrame
)
1467 LARGE_INTEGER Null
= {{0}};
1469 /* Check if interrupts were disabled */
1470 if (!KeGetTrapFrameInterruptState(TrapFrame
))
1472 /* Nothing to return */
1476 /* Otherwise, do the call */
1477 return KeQueryPerformanceCounter(NULL
);
1482 KdpAllowDisable(VOID
)
1484 /* Check if we are on MP */
1485 if (KeNumberProcessors
> 1)
1488 KdpDprintf("KdpAllowDisable: SMP UNHANDLED\n");
1493 return STATUS_SUCCESS
;
1498 KdEnterDebugger(IN PKTRAP_FRAME TrapFrame
,
1499 IN PKEXCEPTION_FRAME ExceptionFrame
)
1503 /* Check if we have a trap frame */
1506 /* Calculate the time difference for the enter */
1507 KdTimerStop
= KdpQueryPerformanceCounter(TrapFrame
);
1508 KdTimerDifference
.QuadPart
= KdTimerStop
.QuadPart
-
1509 KdTimerStart
.QuadPart
;
1513 /* No trap frame, so can't calculate */
1514 KdTimerStop
.QuadPart
= 0;
1517 /* Save the current IRQL */
1518 KeGetCurrentPrcb()->DebuggerSavedIRQL
= KeGetCurrentIrql();
1520 /* Freeze all CPUs */
1521 Entered
= KeFreezeExecution(TrapFrame
, ExceptionFrame
);
1523 /* Lock the port, save the state and set debugger entered */
1524 KdpPortLocked
= KeTryToAcquireSpinLockAtDpcLevel(&KdpDebuggerLock
);
1526 KdEnteredDebugger
= TRUE
;
1528 /* Check freeze flag */
1529 if (KiFreezeFlag
& 1)
1531 /* Print out errror */
1532 KdpDprintf("FreezeLock was jammed! Backup SpinLock was used!\n");
1535 /* Check processor state */
1536 if (KiFreezeFlag
& 2)
1538 /* Print out errror */
1539 KdpDprintf("Some processors not frozen in debugger!\n");
1542 /* Make sure we acquired the port */
1543 if (!KdpPortLocked
) KdpDprintf("Port lock was not acquired!\n");
1545 /* Return enter state */
1551 KdExitDebugger(IN BOOLEAN Entered
)
1555 /* Restore the state and unlock the port */
1557 if (KdpPortLocked
) KdpPortUnlock();
1559 /* Unfreeze the CPUs */
1560 KeThawExecution(Entered
);
1562 /* Compare time with the one from KdEnterDebugger */
1563 if (!KdTimerStop
.QuadPart
)
1565 /* We didn't get a trap frame earlier in so never got the time */
1566 KdTimerStart
= KdTimerStop
;
1570 /* Query the timer */
1571 KdTimerStart
= KeQueryPerformanceCounter(NULL
);
1574 /* Check if a Time Slip was on queue */
1575 TimeSlip
= InterlockedIncrement(&KdpTimeSlipPending
);
1578 /* Queue a DPC for the time slip */
1579 InterlockedIncrement(&KdpTimeSlipPending
);
1580 KeInsertQueueDpc(&KdpTimeSlipDpc
, NULL
, NULL
);
1586 KdEnableDebuggerWithLock(IN BOOLEAN NeedLock
)
1590 #if defined(__GNUC__)
1591 /* Make gcc happy */
1592 OldIrql
= PASSIVE_LEVEL
;
1595 /* Check if enabling the debugger is blocked */
1598 /* It is, fail the enable */
1599 return STATUS_ACCESS_DENIED
;
1602 /* Check if we need to acquire the lock */
1606 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1610 /* Check if we're not disabled */
1611 if (!KdDisableCount
)
1613 /* Check if we had locked the port before */
1617 KeLowerIrql(OldIrql
);
1620 /* Fail: We're already enabled */
1621 return STATUS_INVALID_PARAMETER
;
1626 * This can only happen if we are called from a bugcheck
1627 * and were never initialized, so initialize the debugger now.
1629 KdInitSystem(0, NULL
);
1631 /* Return success since we initialized */
1632 return STATUS_SUCCESS
;
1636 /* Decrease the disable count */
1637 if (!(--KdDisableCount
))
1639 /* We're now enabled again! Were we enabled before, too? */
1640 if (KdPreviouslyEnabled
)
1642 /* Reinitialize the Debugger */
1643 KdInitSystem(0, NULL
) ;
1644 KdpRestoreAllBreakpoints();
1648 /* Check if we had locked the port before */
1651 /* Yes, now unlock it */
1652 KeLowerIrql(OldIrql
);
1657 return STATUS_SUCCESS
;
1662 KdDisableDebuggerWithLock(IN BOOLEAN NeedLock
)
1667 #if defined(__GNUC__)
1668 /* Make gcc happy */
1669 OldIrql
= PASSIVE_LEVEL
;
1673 * If enabling the debugger is blocked
1674 * then there is nothing to disable (duh)
1679 return STATUS_ACCESS_DENIED
;
1682 /* Check if we need to acquire the lock */
1686 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1690 /* Check if we're not disabled */
1691 if (!KdDisableCount
)
1693 /* Check if the debugger was never actually initialized */
1694 if (!(KdDebuggerEnabled
) && !(KdPitchDebugger
))
1696 /* It wasn't, so don't re-enable it later */
1697 KdPreviouslyEnabled
= FALSE
;
1701 /* It was, so we will re-enable it later */
1702 KdPreviouslyEnabled
= TRUE
;
1705 /* Check if we were called from the exported API and are enabled */
1706 if ((NeedLock
) && (KdPreviouslyEnabled
))
1708 /* Check if it is safe to disable the debugger */
1709 Status
= KdpAllowDisable();
1710 if (!NT_SUCCESS(Status
))
1712 /* Release the lock and fail */
1713 KeLowerIrql(OldIrql
);
1719 /* Only disable the debugger if it is enabled */
1720 if (KdDebuggerEnabled
)
1723 * Disable the debugger; suspend breakpoints
1724 * and reset the debug stub
1726 KdpSuspendAllBreakPoints();
1727 KiDebugRoutine
= KdpStub
;
1729 /* We are disabled now */
1730 KdDebuggerEnabled
= FALSE
;
1731 #undef KdDebuggerEnabled
1732 SharedUserData
->KdDebuggerEnabled
= FALSE
;
1733 #define KdDebuggerEnabled _KdDebuggerEnabled
1737 /* Increment the disable count */
1740 /* Check if we had locked the port before */
1743 /* Yes, now unlock it */
1744 KeLowerIrql(OldIrql
);
1749 return STATUS_SUCCESS
;
1752 /* PUBLIC FUNCTIONS **********************************************************/
1759 KdEnableDebugger(VOID
)
1761 /* Use the internal routine */
1762 return KdEnableDebuggerWithLock(TRUE
);
1770 KdDisableDebugger(VOID
)
1772 /* Use the internal routine */
1773 return KdDisableDebuggerWithLock(TRUE
);
1781 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
1782 IN PVOID InputBuffer
,
1783 IN ULONG InputBufferLength
,
1784 OUT PVOID OutputBuffer
,
1785 IN ULONG OutputBufferLength
,
1786 IN OUT PULONG ReturnLength
,
1787 IN KPROCESSOR_MODE PreviousMode
)
1790 return STATUS_SUCCESS
;
1798 KdChangeOption(IN KD_OPTION Option
,
1799 IN ULONG InBufferBytes OPTIONAL
,
1801 IN ULONG OutBufferBytes OPTIONAL
,
1802 OUT PVOID OutBuffer
,
1803 OUT PULONG OutBufferNeeded OPTIONAL
)
1806 return STATUS_SUCCESS
;
1814 KdPowerTransition(IN DEVICE_POWER_STATE NewState
)
1817 return STATUS_SUCCESS
;
1825 KdRefreshDebuggerNotPresent(VOID
)
1828 return KdDebuggerNotPresent
;
1833 NtQueryDebugFilterState(ULONG ComponentId
,
1837 return STATUS_SUCCESS
;
1842 NtSetDebugFilterState(ULONG ComponentId
,
1847 return STATUS_SUCCESS
;