VirtualBox

source: vbox/trunk/src/VBox/Debugger/DBGCRemoteKd.cpp@ 86105

Last change on this file since 86105 was 86105, checked in by vboxsync, 4 years ago

Debugger/RemoteKd: Add beginnings of debug stub talking the KD remote protocol (WinDbg and friends), heavy work in progress

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 95.4 KB
Line 
1/* $Id: DBGCRemoteKd.cpp 86105 2020-09-13 08:28:59Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, Windows Kd Remote Stub.
4 */
5
6/*
7 * Copyright (C) 2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/dbg.h>
23#include <VBox/vmm/dbgf.h>
24#include <VBox/vmm/vmapi.h> /* VMR3GetVM() */
25#include <VBox/vmm/hm.h> /* HMR3IsEnabled */
26#include <VBox/vmm/nem.h> /* NEMR3IsEnabled */
27#include <iprt/assertcompile.h>
28#include <iprt/cdefs.h>
29#include <iprt/err.h>
30#include <iprt/list.h>
31#include <iprt/mem.h>
32#include <iprt/sg.h>
33#include <iprt/string.h>
34#include <iprt/time.h>
35#include <iprt/x86.h>
36#include <iprt/formats/pecoff.h>
37#include <iprt/formats/mz.h>
38
39#include <stdlib.h>
40
41#include "DBGCInternal.h"
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47
48/** Number of milliseconds we wait for new data to arrive when a new packet was detected. */
49#define DBGC_KD_RECV_TIMEOUT_MS UINT32_C(1000)
50
51/** NT status code - Success. */
52#define NTSTATUS_SUCCESS 0
53/** NT status code - operation unsuccesful. */
54#define NTSTATUS_UNSUCCESSFUL UINT32_C(0xc0000001)
55/** NT status code - operation not implemented. */
56#define NTSTATUS_NOT_IMPLEMENTED UINT32_C(0xc0000002)
57
58/*********************************************************************************************************************************
59* Structures and Typedefs *
60*********************************************************************************************************************************/
61
62/**
63 * KD packet header as sent over the wire.
64 */
65typedef struct KDPACKETHDR
66{
67 /** Packet signature (leader) - defines the type of packet. */
68 uint32_t u32Signature;
69 /** Packet (sub) type. */
70 uint16_t u16SubType;
71 /** Size of the packet body in bytes.*/
72 uint16_t cbBody;
73 /** Packet ID. */
74 uint32_t idPacket;
75 /** Checksum of the packet body. */
76 uint32_t u32ChkSum;
77} KDPACKETHDR;
78AssertCompileSize(KDPACKETHDR, 16);
79/** Pointer to a packet header. */
80typedef KDPACKETHDR *PKDPACKETHDR;
81/** Pointer to a const packet header. */
82typedef const KDPACKETHDR *PCKDPACKETHDR;
83
84/** Signature for a data packet. */
85#define KD_PACKET_HDR_SIGNATURE_DATA UINT32_C(0x30303030)
86/** First byte for a data packet header. */
87#define KD_PACKET_HDR_SIGNATURE_DATA_BYTE 0x30
88/** Signature for a control packet. */
89#define KD_PACKET_HDR_SIGNATURE_CONTROL UINT32_C(0x69696969)
90/** First byte for a control packet header. */
91#define KD_PACKET_HDR_SIGNATURE_CONTROL_BYTE 0x69
92/** Signature for a breakin packet. */
93#define KD_PACKET_HDR_SIGNATURE_BREAKIN UINT32_C(0x62626262)
94/** First byte for a breakin packet header. */
95#define KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE 0x62
96
97/** @name Packet sub types.
98 * @{ */
99#define KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE32 UINT16_C(1)
100#define KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE UINT16_C(2)
101#define KD_PACKET_HDR_SUB_TYPE_DEBUG_IO UINT16_C(3)
102#define KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE UINT16_C(4)
103#define KD_PACKET_HDR_SUB_TYPE_RESEND UINT16_C(5)
104#define KD_PACKET_HDR_SUB_TYPE_RESET UINT16_C(6)
105#define KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE64 UINT16_C(7)
106#define KD_PACKET_HDR_SUB_TYPE_POLL_BREAKIN UINT16_C(8)
107#define KD_PACKET_HDR_SUB_TYPE_TRACE_IO UINT16_C(9)
108#define KD_PACKET_HDR_SUB_TYPE_CONTROL_REQUEST UINT16_C(10)
109#define KD_PACKET_HDR_SUB_TYPE_FILE_IO UINT16_C(11)
110#define KD_PACKET_HDR_SUB_TYPE_MAX UINT16_C(12)
111/** @} */
112
113/** Initial packet ID value. */
114#define KD_PACKET_HDR_ID_INITIAL UINT32_C(0x80800800)
115/** Packet ID value after a resync. */
116#define KD_PACKET_HDR_ID_RESET UINT32_C(0x80800000)
117
118/** Trailing byte of a packet. */
119#define KD_PACKET_TRAILING_BYTE 0xaa
120
121
122/** Maximum number of parameters in the exception record. */
123#define KDPACKETEXCP_PARMS_MAX 15
124
125/**
126 * 64bit exception record.
127 */
128typedef struct KDPACKETEXCP64
129{
130 /** The exception code identifying the excpetion. */
131 uint32_t u32ExcpCode;
132 /** Flags associated with the exception. */
133 uint32_t u32ExcpFlags;
134 /** Pointer to a chained exception record. */
135 uint64_t u64PtrExcpRecNested;
136 /** Address where the exception occurred. */
137 uint64_t u64PtrExcpAddr;
138 /** Number of parameters in the exception information array. */
139 uint32_t cExcpParms;
140 /** Alignment. */
141 uint32_t u32Alignment;
142 /** Exception parameters array. */
143 uint64_t au64ExcpParms[KDPACKETEXCP_PARMS_MAX];
144} KDPACKETEXCP64;
145AssertCompileSize(KDPACKETEXCP64, 152);
146/** Pointer to an exception record. */
147typedef KDPACKETEXCP64 *PKDPACKETEXCP64;
148/** Pointer to a const exception record. */
149typedef const KDPACKETEXCP64 *PCKDPACKETEXCP64;
150
151
152/**
153 * amd64 NT context structure.
154 */
155typedef struct NTCONTEXT64
156{
157 /** The P[1-6]Home members. */
158 uint64_t au64PHome[6];
159 /** Context flags indicating the valid bits, see NTCONTEXT_F_XXX. */
160 uint32_t fContext;
161 /** MXCSR register. */
162 uint32_t u32RegMxCsr;
163 /** CS selector. */
164 uint16_t u16SegCs;
165 /** DS selector. */
166 uint16_t u16SegDs;
167 /** ES selector. */
168 uint16_t u16SegEs;
169 /** FS selector. */
170 uint16_t u16SegFs;
171 /** GS selector. */
172 uint16_t u16SegGs;
173 /** SS selector. */
174 uint16_t u16SegSs;
175 /** EFlags register. */
176 uint32_t u32RegEflags;
177 /** DR0 register. */
178 uint64_t u64RegDr0;
179 /** DR1 register. */
180 uint64_t u64RegDr1;
181 /** DR2 register. */
182 uint64_t u64RegDr2;
183 /** DR3 register. */
184 uint64_t u64RegDr3;
185 /** DR6 register. */
186 uint64_t u64RegDr6;
187 /** DR7 register. */
188 uint64_t u64RegDr7;
189 /** RAX register. */
190 uint64_t u64RegRax;
191 /** RCX register. */
192 uint64_t u64RegRcx;
193 /** RDX register. */
194 uint64_t u64RegRdx;
195 /** RBX register. */
196 uint64_t u64RegRbx;
197 /** RSP register. */
198 uint64_t u64RegRsp;
199 /** RBP register. */
200 uint64_t u64RegRbp;
201 /** RSI register. */
202 uint64_t u64RegRsi;
203 /** RDI register. */
204 uint64_t u64RegRdi;
205 /** R8 register. */
206 uint64_t u64RegR8;
207 /** R9 register. */
208 uint64_t u64RegR9;
209 /** R10 register. */
210 uint64_t u64RegR10;
211 /** R11 register. */
212 uint64_t u64RegR11;
213 /** R12 register. */
214 uint64_t u64RegR12;
215 /** R13 register. */
216 uint64_t u64RegR13;
217 /** R14 register. */
218 uint64_t u64RegR14;
219 /** R15 register. */
220 uint64_t u64RegR15;
221 /** RIP register. */
222 uint64_t u64RegRip;
223 /** Extended floating point save area. */
224 X86FXSTATE FxSave;
225 /** AVX(?) vector registers. */
226 RTUINT128U aRegsVec[26];
227 /** Vector control register. */
228 uint64_t u64RegVecCtrl;
229 /** Debug control. */
230 uint64_t u64DbgCtrl;
231 /** @todo */
232 uint64_t u64LastBrToRip;
233 uint64_t u64LastBrFromRip;
234 uint64_t u64LastExcpToRip;
235 uint64_t u64LastExcpFromRip;
236} NTCONTEXT64;
237AssertCompileSize(NTCONTEXT64, 1232);
238AssertCompileMemberOffset(NTCONTEXT64, FxSave, 0x100);
239AssertCompileMemberOffset(NTCONTEXT64, aRegsVec[0], 0x300);
240/** Pointer to an amd64 NT context. */
241typedef NTCONTEXT64 *PNTCONTEXT64;
242/** Pointer to a const amd64 NT context. */
243typedef const NTCONTEXT64 *PCNTCONTEXT64;
244
245
246/**
247 * [GI]DT descriptor.
248 */
249typedef struct NTKCONTEXTDESC64
250{
251 /** Alignment. */
252 uint16_t au16Alignment[3];
253 /** Limit. */
254 uint16_t u16Limit;
255 /** Base address. */
256 uint64_t u64PtrBase;
257} NTKCONTEXTDESC64;
258AssertCompileSize(NTKCONTEXTDESC64, 2 * 8);
259/** Pointer to an amd64 NT context. */
260typedef NTKCONTEXTDESC64 *PNTKCONTEXTDESC64;
261/** Pointer to a const amd64 NT context. */
262typedef const NTKCONTEXTDESC64 *PCNTKCONTEXTDESC64;
263
264
265/**
266 * Kernel context as queried by KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE
267 */
268typedef struct NTKCONTEXT64
269{
270 /** CR0 register. */
271 uint64_t u64RegCr0;
272 /** CR2 register. */
273 uint64_t u64RegCr2;
274 /** CR3 register. */
275 uint64_t u64RegCr3;
276 /** CR4 register. */
277 uint64_t u64RegCr4;
278 /** DR0 register. */
279 uint64_t u64RegDr0;
280 /** DR1 register. */
281 uint64_t u64RegDr1;
282 /** DR2 register. */
283 uint64_t u64RegDr2;
284 /** DR3 register. */
285 uint64_t u64RegDr3;
286 /** DR6 register. */
287 uint64_t u64RegDr6;
288 /** DR7 register. */
289 uint64_t u64RegDr7;
290 /** GDTR. */
291 NTKCONTEXTDESC64 Gdtr;
292 /** IDTR. */
293 NTKCONTEXTDESC64 Idtr;
294 /** TR register. */
295 uint16_t u16RegTr;
296 /** LDTR register. */
297 uint16_t u16RegLdtr;
298 /** MXCSR register. */
299 uint32_t u32RegMxCsr;
300 /** Debug control. */
301 uint64_t u64DbgCtrl;
302 /** @todo */
303 uint64_t u64LastBrToRip;
304 uint64_t u64LastBrFromRip;
305 uint64_t u64LastExcpToRip;
306 uint64_t u64LastExcpFromRip;
307 /** CR8 register. */
308 uint64_t u64RegCr8;
309 /** GS base MSR register. */
310 uint64_t u64MsrGsBase;
311 /** Kernel GS base MSR register. */
312 uint64_t u64MsrKernelGsBase;
313 /** STAR MSR register. */
314 uint64_t u64MsrStar;
315 /** LSTAR MSR register. */
316 uint64_t u64MsrLstar;
317 /** CSTAR MSR register. */
318 uint64_t u64MsrCstar;
319 /** SFMASK MSR register. */
320 uint64_t u64MsrSfMask;
321 /** XCR0 register. */
322 uint64_t u64RegXcr0;
323 /** Standard context. */
324 NTCONTEXT64 Ctx;
325} NTKCONTEXT64;
326/** Pointer to an amd64 NT context. */
327typedef NTKCONTEXT64 *PNTKCONTEXT64;
328/** Pointer to a const amd64 NT context. */
329typedef const NTKCONTEXT64 *PCNTKCONTEXT64;
330
331
332/** x86 context. */
333#define NTCONTEXT_F_X86 UINT32_C(0x00010000)
334/** AMD64 context. */
335#define NTCONTEXT_F_AMD64 UINT32_C(0x00100000)
336/** Control registers valid (CS, (R)SP, (R)IP, FLAGS and BP). */
337#define NTCONTEXT_F_CONTROL RT_BIT_32(0)
338/** Integer registers valid. */
339#define NTCONTEXT_F_INTEGER RT_BIT_32(1)
340/** Segment registers valid. */
341#define NTCONTEXT_F_SEGMENTS RT_BIT_32(2)
342/** Floating point registers valid. */
343#define NTCONTEXT_F_FLOATING_POINT RT_BIT_32(3)
344/** Debug registers valid. */
345#define NTCONTEXT_F_DEBUG RT_BIT_32(4)
346/** Extended registers valid (x86 only). */
347#define NTCONTEXT_F_EXTENDED RT_BIT_32(5)
348/** Full x86 context valid. */
349#define NTCONTEXT32_F_FULL (NTCONTEXT_F_X86 | NTCONTEXT_F_CONTROL | NTCONTEXT_F_INTEGER | NTCONTEXT_F_SEGMENTS)
350/** Full amd64 context valid. */
351#define NTCONTEXT64_F_FULL (NTCONTEXT_F_AMD64 | NTCONTEXT_F_CONTROL | NTCONTEXT_F_INTEGER | NTCONTEXT_F_SEGMENTS)
352
353
354/**
355 * 32bit exception record.
356 */
357typedef struct KDPACKETEXCP32
358{
359 /** The exception code identifying the excpetion. */
360 uint32_t u32ExcpCode;
361 /** Flags associated with the exception. */
362 uint32_t u32ExcpFlags;
363 /** Pointer to a chained exception record. */
364 uint32_t u32PtrExcpRecNested;
365 /** Address where the exception occurred. */
366 uint32_t u32PtrExcpAddr;
367 /** Number of parameters in the exception information array. */
368 uint32_t cExcpParms;
369 /** Exception parameters array. */
370 uint32_t au32ExcpParms[KDPACKETEXCP_PARMS_MAX];
371} KDPACKETEXCP32;
372AssertCompileSize(KDPACKETEXCP32, 80);
373/** Pointer to an exception record. */
374typedef KDPACKETEXCP32 *PKDPACKETEXCP32;
375/** Pointer to a const exception record. */
376typedef const KDPACKETEXCP32 *PCKDPACKETEXCP32;
377
378
379/** @name Exception codes.
380 * @{ */
381/** A breakpoint was hit. */
382#define KD_PACKET_EXCP_CODE_BKPT UINT32_C(0x80000003)
383/** An instruction was single stepped. */
384#define KD_PACKET_EXCP_CODE_SINGLE_STEP UINT32_C(0x80000004)
385/** @} */
386
387
388/** Maximum number of bytes in the instruction stream. */
389#define KD_PACKET_CTRL_REPORT_INSN_STREAM_MAX 16
390
391/**
392 * 64bit control report record.
393 */
394typedef struct KDPACKETCTRLREPORT64
395{
396 /** Value of DR6. */
397 uint64_t u64RegDr6;
398 /** Value of DR7. */
399 uint64_t u64RegDr7;
400 /** EFLAGS. */
401 uint32_t u32RegEflags;
402 /** Number of instruction bytes in the instruction stream. */
403 uint16_t cbInsnStream;
404 /** Report flags. */
405 uint16_t fFlags;
406 /** The instruction stream. */
407 uint8_t abInsn[KD_PACKET_CTRL_REPORT_INSN_STREAM_MAX];
408 /** CS selector. */
409 uint16_t u16SegCs;
410 /** DS selector. */
411 uint16_t u16SegDs;
412 /** ES selector. */
413 uint16_t u16SegEs;
414 /** FS selector. */
415 uint16_t u16SegFs;
416} KDPACKETCTRLREPORT64;
417AssertCompileSize(KDPACKETCTRLREPORT64, 2 * 8 + 4 + 2 * 2 + 16 + 4 * 2);
418/** Pointer to a control report record. */
419typedef KDPACKETCTRLREPORT64 *PKDPACKETCTRLREPORT64;
420/** Pointer to a const control report record. */
421typedef const KDPACKETCTRLREPORT64 *PCKDPACKETCTRLREPORT64;
422
423
424/**
425 * 64bit state change packet body.
426 */
427typedef struct KDPACKETSTATECHANGE64
428{
429 /** The new state. */
430 uint32_t u32StateNew;
431 /** The processor level. */
432 uint16_t u16CpuLvl;
433 /** The processor ID generating the state change. */
434 uint16_t idCpu;
435 /** Number of processors in the system. */
436 uint32_t cCpus;
437 /** Alignment. */
438 uint32_t u32Alignment;
439 /** The thread ID currently executing when the state change occurred. */
440 uint64_t idThread;
441 /** Program counter of the thread. */
442 uint64_t u64RipThread;
443 /** Data based on the state. */
444 union
445 {
446 /** Exception occurred data. */
447 struct
448 {
449 /** The exception record. */
450 KDPACKETEXCP64 ExcpRec;
451 /** First chance(?). */
452 uint32_t u32FirstChance;
453 } Exception;
454 } u;
455 /** The control report */
456 union
457 {
458 /** AMD64 control report. */
459 KDPACKETCTRLREPORT64 Amd64;
460 } uCtrlReport;
461} KDPACKETSTATECHANGE64;
462//AssertCompileSize(KDPACKETSTATECHANGE64, 4 + 2 * 2 + 2 * 4 + 2 * 8 + sizeof(KDPACKETEXCP64) + 4 + sizeof(KDPACKETCTRLREPORT64));
463/** Pointer to a 64bit state change packet body. */
464typedef KDPACKETSTATECHANGE64 *PKDPACKETSTATECHANGE64;
465/** Pointer to a const 64bit state change packet body. */
466typedef const KDPACKETSTATECHANGE64 *PCKDPACKETSTATECHANGE64;
467
468
469/** @name State change state types.
470 * @{ */
471/** Minimum state change type. */
472#define KD_PACKET_STATE_CHANGE_MIN UINT32_C(0x00003030)
473/** An exception occured. */
474#define KD_PACKET_STATE_CHANGE_EXCEPTION KD_PACKET_STATE_CHANGE_MIN
475/** Symbols were loaded(?). */
476#define KD_PACKET_STATE_CHANGE_LOAD_SYMBOLS UINT32_C(0x00003031)
477/** Command string (custom command was executed?). */
478#define KD_PACKET_STATE_CHANGE_CMD_STRING UINT32_C(0x00003032)
479/** Maximum state change type (exclusive). */
480#define KD_PACKET_STATE_CHANGE_MAX UINT32_C(0x00003033)
481/** @} */
482
483
484/**
485 * 64bit get version manipulate payload.
486 */
487typedef struct KDPACKETMANIPULATE_GETVERSION64
488{
489 /** Major version. */
490 uint16_t u16VersMaj;
491 /** Minor version. */
492 uint16_t u16VersMin;
493 /** Protocol version. */
494 uint8_t u8VersProtocol;
495 /** KD secondary version. */
496 uint8_t u8VersKdSecondary;
497 /** Flags. */
498 uint16_t fFlags;
499 /** Machine type. */
500 uint16_t u16MachineType;
501 /** Maximum packet type. */
502 uint8_t u8MaxPktType;
503 /** Maximum state change */
504 uint8_t u8MaxStateChange;
505 /** Maximum manipulate request ID. */
506 uint8_t u8MaxManipulate;
507 /** Some simulation flag. */
508 uint8_t u8Simulation;
509 /** Padding. */
510 uint16_t u16Padding;
511 /** Kernel base. */
512 uint64_t u64PtrKernBase;
513 /** Pointer of the loaded module list head. */
514 uint64_t u64PtrPsLoadedModuleList;
515 /** Pointer of the debugger data list. */
516 uint64_t u64PtrDebuggerDataList;
517} KDPACKETMANIPULATE_GETVERSION64;
518AssertCompileSize(KDPACKETMANIPULATE_GETVERSION64, 8 * 2 + 3 * 8);
519/** Pointer to a 64bit get version manipulate payload. */
520typedef KDPACKETMANIPULATE_GETVERSION64 *PKDPACKETMANIPULATE_GETVERSION64;
521/** Pointer to a const 64bit get version manipulate payload. */
522typedef const KDPACKETMANIPULATE_GETVERSION64 *PCKDPACKETMANIPULATE_GETVERSION64;
523
524
525/**
526 * 64bit memory transfer manipulate payload.
527 */
528typedef struct KDPACKETMANIPULATE_XFERMEM64
529{
530 /** Target base address. */
531 uint64_t u64PtrTarget;
532 /** Requested number of bytes to transfer*/
533 uint32_t cbXferReq;
534 /** Number of bytes actually transferred (response). */
535 uint32_t cbXfered;
536 /** Some padding?. */
537 uint64_t au64Pad[3];
538} KDPACKETMANIPULATE_XFERMEM64;
539AssertCompileSize(KDPACKETMANIPULATE_XFERMEM64, 4 * 8 + 2 * 4);
540/** Pointer to a 64bit memory transfer manipulate payload. */
541typedef KDPACKETMANIPULATE_XFERMEM64 *PKDPACKETMANIPULATE_XFERMEM64;
542/** Pointer to a const 64bit memory transfer manipulate payload. */
543typedef const KDPACKETMANIPULATE_XFERMEM64 *PCKDPACKETMANIPULATE_XFERMEM64;
544
545
546/**
547 * 64bit control space transfer manipulate payload.
548 *
549 * @note Same layout as the memory transfer but the pointer has a different meaning so
550 * we moved it into a separate request structure.
551 */
552typedef struct KDPACKETMANIPULATE_XFERCTRLSPACE64
553{
554 /** Identifier of the item to transfer in the control space. */
555 uint64_t u64IdXfer;
556 /** Requested number of bytes to transfer*/
557 uint32_t cbXferReq;
558 /** Number of bytes actually transferred (response). */
559 uint32_t cbXfered;
560 /** Some padding?. */
561 uint64_t au64Pad[3];
562} KDPACKETMANIPULATE_XFERCTRLSPACE64;
563AssertCompileSize(KDPACKETMANIPULATE_XFERCTRLSPACE64, 4 * 8 + 2 * 4);
564/** Pointer to a 64bit memory transfer manipulate payload. */
565typedef KDPACKETMANIPULATE_XFERCTRLSPACE64 *PKDPACKETMANIPULATE_XFERCTRLSPACE64;
566/** Pointer to a const 64bit memory transfer manipulate payload. */
567typedef const KDPACKETMANIPULATE_XFERCTRLSPACE64 *PCKDPACKETMANIPULATE_XFERCTRLSPACE64;
568
569
570/** @name Known control space identifiers.
571 * @{ */
572/** Read/Write KPCR address. */
573#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCR UINT64_C(0)
574/** Read/Write KPCRB address. */
575#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCRB UINT64_C(1)
576/** Read/Write Kernel context. */
577#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KCTX UINT64_C(2)
578/** Read/Write current kernel thread. */
579#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KTHRD UINT64_C(3)
580/** @} */
581
582
583/**
584 * 64bit restore breakpoint manipulate payload.
585 */
586typedef struct KDPACKETMANIPULATE_RESTOREBKPT64
587{
588 /** The breakpoint handle to restore. */
589 uint32_t u32HndBkpt;
590} KDPACKETMANIPULATE_RESTOREBKPT64;
591AssertCompileSize(KDPACKETMANIPULATE_RESTOREBKPT64, 4);
592/** Pointer to a 64bit restore breakpoint manipulate payload. */
593typedef KDPACKETMANIPULATE_RESTOREBKPT64 *PKDPACKETMANIPULATE_RESTOREBKPT64;
594/** Pointer to a const 64bit restore breakpoint manipulate payload. */
595typedef const KDPACKETMANIPULATE_RESTOREBKPT64 *PCKDPACKETMANIPULATE_RESTOREBKPT64;
596
597
598/**
599 * context extended manipulate payload.
600 */
601typedef struct KDPACKETMANIPULATE_CONTEXTEX
602{
603 /** Where to start copying the context. */
604 uint32_t offStart;
605 /** Number of bytes to transfer. */
606 uint32_t cbXfer;
607 /** Number of bytes actually transfered. */
608 uint32_t cbXfered;
609} KDPACKETMANIPULATE_CONTEXTEX;
610AssertCompileSize(KDPACKETMANIPULATE_CONTEXTEX, 3 * 4);
611/** Pointer to a context extended manipulate payload. */
612typedef KDPACKETMANIPULATE_CONTEXTEX *PKDPACKETMANIPULATE_CONTEXTEX;
613/** Pointer to a const context extended manipulate payload. */
614typedef const KDPACKETMANIPULATE_CONTEXTEX *PCKDPACKETMANIPULATE_CONTEXTEX;
615
616
617/**
618 * Manipulate request packet header (Same for 32bit and 64bit).
619 */
620typedef struct KDPACKETMANIPULATEHDR
621{
622 /** The request to execute. */
623 uint32_t idReq;
624 /** The processor level to execute the request on. */
625 uint16_t u16CpuLvl;
626 /** The processor ID to execute the request on. */
627 uint16_t idCpu;
628 /** Return status code. */
629 uint32_t u32NtStatus;
630 /** Alignment. */
631 uint32_t u32Alignment;
632} KDPACKETMANIPULATEHDR;
633AssertCompileSize(KDPACKETMANIPULATEHDR, 3 * 4 + 2 * 2);
634/** Pointer to a manipulate request packet header. */
635typedef KDPACKETMANIPULATEHDR *PKDPACKETMANIPULATEHDR;
636/** Pointer to a const manipulate request packet header. */
637typedef const KDPACKETMANIPULATEHDR *PCPKDPACKETMANIPULATEHDR;
638
639
640/**
641 * 64bit manipulate state request packet.
642 */
643typedef struct KDPACKETMANIPULATE64
644{
645 /** Header. */
646 KDPACKETMANIPULATEHDR Hdr;
647 /** Request payloads. */
648 union
649 {
650 /** Get Version. */
651 KDPACKETMANIPULATE_GETVERSION64 GetVersion;
652 /** Read/Write memory. */
653 KDPACKETMANIPULATE_XFERMEM64 XferMem;
654 /** Read/Write control space. */
655 KDPACKETMANIPULATE_XFERCTRLSPACE64 XferCtrlSpace;
656 /** Restore breakpoint. */
657 KDPACKETMANIPULATE_RESTOREBKPT64 RestoreBkpt;
658 /** Context extended. */
659 KDPACKETMANIPULATE_CONTEXTEX ContextEx;
660 } u;
661} KDPACKETMANIPULATE64;
662/** Pointer to a 64bit manipulate state request packet. */
663typedef KDPACKETMANIPULATE64 *PKDPACKETMANIPULATE64;
664/** Pointer to a const 64bit manipulate state request packet. */
665typedef const KDPACKETMANIPULATE64 *PCKDPACKETMANIPULATE64;
666
667/** @name Manipulate requests.
668 * @{ */
669/** Minimum available request. */
670#define KD_PACKET_MANIPULATE_REQ_MIN UINT32_C(0x00003130)
671/** Read virtual memory request. */
672#define KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM KD_PACKET_MANIPULATE_REQ_MIN
673/** Write virtual memory request. */
674#define KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM UINT32_C(0x00003131)
675/** Get context request. */
676#define KD_PACKET_MANIPULATE_REQ_GET_CONTEXT UINT32_C(0x00003132)
677/** Set context request. */
678#define KD_PACKET_MANIPULATE_REQ_SET_CONTEXT UINT32_C(0x00003133)
679/** Write breakpoint request. */
680#define KD_PACKET_MANIPULATE_REQ_WRITE_BKPT UINT32_C(0x00003134)
681/** Restore breakpoint request. */
682#define KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT UINT32_C(0x00003135)
683/** Continue request. */
684#define KD_PACKET_MANIPULATE_REQ_CONTINUE UINT32_C(0x00003136)
685/** Read control space request. */
686#define KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE UINT32_C(0x00003137)
687/** Write control space request. */
688#define KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE UINT32_C(0x00003138)
689/** Read I/O space request. */
690#define KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE UINT32_C(0x00003139)
691/** Write I/O space request. */
692#define KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE UINT32_C(0x0000313a)
693/** Reboot request. */
694#define KD_PACKET_MANIPULATE_REQ_REBOOT UINT32_C(0x0000313b)
695/** continue 2nd version request. */
696#define KD_PACKET_MANIPULATE_REQ_CONTINUE2 UINT32_C(0x0000313c)
697/** Read physical memory request. */
698#define KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM UINT32_C(0x0000313d)
699/** Write physical memory request. */
700#define KD_PACKET_MANIPULATE_REQ_WRITE_PHYS_MEM UINT32_C(0x0000313e)
701/** Query special calls request. */
702#define KD_PACKET_MANIPULATE_REQ_QUERY_SPEC_CALLS UINT32_C(0x0000313f)
703/** Set special calls request. */
704#define KD_PACKET_MANIPULATE_REQ_SET_SPEC_CALLS UINT32_C(0x00003140)
705/** Clear special calls request. */
706#define KD_PACKET_MANIPULATE_REQ_CLEAR_SPEC_CALLS UINT32_C(0x00003141)
707/** Set internal breakpoint request. */
708#define KD_PACKET_MANIPULATE_REQ_SET_INTERNAL_BKPT UINT32_C(0x00003142)
709/** Get internal breakpoint request. */
710#define KD_PACKET_MANIPULATE_REQ_GET_INTERNAL_BKPT UINT32_C(0x00003143)
711/** Read I/O space extended request. */
712#define KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE_EX UINT32_C(0x00003144)
713/** Write I/O space extended request. */
714#define KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE_EX UINT32_C(0x00003145)
715/** Get version request. */
716#define KD_PACKET_MANIPULATE_REQ_GET_VERSION UINT32_C(0x00003146)
717/** @todo */
718/** Clear all internal breakpoints request. */
719#define KD_PACKET_MANIPULATE_REQ_CLEAR_ALL_INTERNAL_BKPT UINT32_C(0x0000315a)
720/** @todo */
721/** Get context extended request. */
722#define KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX UINT32_C(0x0000315f)
723/** @todo */
724/** Maximum available request (exclusive). */
725#define KD_PACKET_MANIPULATE_REQ_MAX UINT32_C(0x00003161)
726/** @} */
727
728/**
729 * KD stub receive state.
730 */
731typedef enum KDRECVSTATE
732{
733 /** Invalid state. */
734 KDRECVSTATE_INVALID = 0,
735 /** Receiving the first byte of the packet header. */
736 KDRECVSTATE_PACKET_HDR_FIRST_BYTE,
737 /** Receiving the second byte of the packet header. */
738 KDRECVSTATE_PACKET_HDR_SECOND_BYTE,
739 /** Receiving the header. */
740 KDRECVSTATE_PACKET_HDR,
741 /** Receiving the packet body. */
742 KDRECVSTATE_PACKET_BODY,
743 /** Receiving the trailing byte. */
744 KDRECVSTATE_PACKET_TRAILER,
745 /** Blow up the enum to 32bits for easier alignment of members in structs. */
746 KDRECVSTATE_32BIT_HACK = 0x7fffffff
747} KDRECVSTATE;
748
749
750/**
751 * KD context data.
752 */
753typedef struct KDCTX
754{
755 /** Internal debugger console data. */
756 DBGC Dbgc;
757 /** Number of bytes received left for the current state. */
758 size_t cbRecvLeft;
759 /** Pointer where to write the next received data. */
760 uint8_t *pbRecv;
761 /** The current state when receiving a new packet. */
762 KDRECVSTATE enmState;
763 /** The timeout waiting for new data. */
764 RTMSINTERVAL msRecvTimeout;
765 /** Timestamp when we last received data from the remote end. */
766 uint64_t tsRecvLast;
767 /** Packet header being received. */
768 union
769 {
770 KDPACKETHDR Fields;
771 uint8_t ab[16];
772 } PktHdr;
773 /** The next packet ID to send. */
774 uint32_t idPktNext;
775 /** Offset into the body receive buffer. */
776 size_t offBodyRecv;
777 /** Body data. */
778 uint8_t abBody[_4K];
779 /** The trailer byte storage. */
780 uint8_t bTrailer;
781 /** Flag whether a breakin packet was received since the last time it was reset. */
782 bool fBreakinRecv;
783
784 /** Pointer to the OS digger WinNt interface if a matching guest was detected. */
785 PDBGFOSIWINNT pIfWinNt;
786} KDCTX;
787/** Pointer to the KD context data. */
788typedef KDCTX *PKDCTX;
789/** Pointer to const KD context data. */
790typedef const KDCTX *PCKDCTX;
791/** Pointer to a KD context data pointer. */
792typedef PKDCTX *PPKDCTX;
793
794
795/*********************************************************************************************************************************
796* Internal Functions *
797*********************************************************************************************************************************/
798
799
800/**
801 * Returns a human readable string of the given packet sub type.
802 *
803 * @returns Pointer to sub type string.
804 * @param u16SubType The sub type to convert to a string.
805 */
806static const char *dbgcKdPktDumpSubTypeToStr(uint16_t u16SubType)
807{
808 switch (u16SubType)
809 {
810 case KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE32: return "StateChange32";
811 case KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE: return "Manipulate";
812 case KD_PACKET_HDR_SUB_TYPE_DEBUG_IO: return "DebugIo";
813 case KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE: return "Ack";
814 case KD_PACKET_HDR_SUB_TYPE_RESEND: return "Resend";
815 case KD_PACKET_HDR_SUB_TYPE_RESET: return "Reset";
816 case KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE64: return "StateChange64";
817 case KD_PACKET_HDR_SUB_TYPE_POLL_BREAKIN: return "PollBreakin";
818 case KD_PACKET_HDR_SUB_TYPE_TRACE_IO: return "TraceIo";
819 case KD_PACKET_HDR_SUB_TYPE_CONTROL_REQUEST: return "ControlRequest";
820 case KD_PACKET_HDR_SUB_TYPE_FILE_IO: return "FileIo";
821 default: break;
822 }
823
824 return "<UNKNOWN>";
825}
826
827
828/**
829 * Returns a human readable string of the given manipulate request ID.
830 *
831 * @returns nothing.
832 * @param idReq Request ID (API number in KD speak).
833 */
834static const char *dbgcKdPktDumpManipulateReqToStr(uint32_t idReq)
835{
836 switch (idReq)
837 {
838 case KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM: return "ReadVirtMem";
839 case KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM: return "WriteVirtMem";
840 case KD_PACKET_MANIPULATE_REQ_GET_CONTEXT: return "GetContext";
841 case KD_PACKET_MANIPULATE_REQ_SET_CONTEXT: return "SetContext";
842 case KD_PACKET_MANIPULATE_REQ_WRITE_BKPT: return "WriteBkpt";
843 case KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT: return "RestoreBkpt";
844 case KD_PACKET_MANIPULATE_REQ_CONTINUE: return "Continue";
845 case KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE: return "ReadCtrlSpace";
846 case KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE: return "WriteCtrlSpace";
847 case KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE: return "ReadIoSpace";
848 case KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE: return "WriteIoSpace";
849 case KD_PACKET_MANIPULATE_REQ_REBOOT: return "Reboot";
850 case KD_PACKET_MANIPULATE_REQ_CONTINUE2: return "Continue2";
851 case KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM: return "ReadPhysMem";
852 case KD_PACKET_MANIPULATE_REQ_WRITE_PHYS_MEM: return "WritePhysMem";
853 case KD_PACKET_MANIPULATE_REQ_QUERY_SPEC_CALLS: return "QuerySpecCalls";
854 case KD_PACKET_MANIPULATE_REQ_SET_SPEC_CALLS: return "SetSpecCalls";
855 case KD_PACKET_MANIPULATE_REQ_CLEAR_SPEC_CALLS: return "ClrSpecCalls";
856 case KD_PACKET_MANIPULATE_REQ_SET_INTERNAL_BKPT: return "SetIntBkpt";
857 case KD_PACKET_MANIPULATE_REQ_GET_INTERNAL_BKPT: return "GetIntBkpt";
858 case KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE_EX: return "ReadIoSpaceEx";
859 case KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE_EX: return "WriteIoSpaceEx";
860 case KD_PACKET_MANIPULATE_REQ_GET_VERSION: return "GetVersion";
861 case KD_PACKET_MANIPULATE_REQ_CLEAR_ALL_INTERNAL_BKPT: return "ClrAllIntBkpt";
862 case KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX: return "GetContextEx";
863 default: break;
864 }
865
866 return "<UNKNOWN>";
867}
868
869
870/**
871 * Dumps the content of a manipulate packet.
872 *
873 * @returns nothing.
874 * @param pSgBuf S/G buffer containing the manipulate packet payload.
875 */
876static void dbgcKdPktDumpManipulate(PRTSGBUF pSgBuf)
877{
878 KDPACKETMANIPULATEHDR Hdr;
879 size_t cbCopied = RTSgBufCopyToBuf(pSgBuf, &Hdr, sizeof(Hdr));
880
881 if (cbCopied == sizeof(Hdr))
882 {
883 const char *pszReq = dbgcKdPktDumpManipulateReqToStr(Hdr.idReq);
884
885 Log3((" MANIPULATE(%#x (%s), %#x, %u, %#x)\n",
886 Hdr.idReq, pszReq, Hdr.u16CpuLvl, Hdr.idCpu, Hdr.u32NtStatus));
887
888 switch (Hdr.idReq)
889 {
890 case KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM:
891 case KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM:
892 case KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM:
893 case KD_PACKET_MANIPULATE_REQ_WRITE_PHYS_MEM:
894 {
895 KDPACKETMANIPULATE_XFERMEM64 XferMem64;
896 cbCopied = RTSgBufCopyToBuf(pSgBuf, &XferMem64, sizeof(XferMem64));
897 if (cbCopied == sizeof(XferMem64))
898 {
899 Log3((" u64PtrTarget: %RX64\n"
900 " cbXferReq: %RX32\n"
901 " cbXfered: %RX32\n",
902 XferMem64.u64PtrTarget, XferMem64.cbXferReq, XferMem64.cbXfered));
903 }
904 else
905 Log3((" Payload to small, expected %u, got %zu\n", sizeof(XferMem64), cbCopied));
906 break;
907 }
908 case KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE:
909 case KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE:
910 {
911 KDPACKETMANIPULATE_XFERCTRLSPACE64 XferCtrlSpace64;
912 cbCopied = RTSgBufCopyToBuf(pSgBuf, &XferCtrlSpace64, sizeof(XferCtrlSpace64));
913 if (cbCopied == sizeof(XferCtrlSpace64))
914 {
915 Log3((" u64IdXfer: %RX64\n"
916 " cbXferReq: %RX32\n"
917 " cbXfered: %RX32\n",
918 XferCtrlSpace64.u64IdXfer, XferCtrlSpace64.cbXferReq, XferCtrlSpace64.cbXfered));
919 }
920 else
921 Log3((" Payload to small, expected %u, got %zu\n", sizeof(XferCtrlSpace64), cbCopied));
922 break;
923 }
924 default:
925 break;
926 }
927 }
928 else
929 Log3((" MANIPULATE(Header too small, expected %u, got %zu)\n", sizeof(Hdr), cbCopied));
930}
931
932
933/**
934 * Dumps the received packet to the debug log.
935 *
936 * @returns VBox status code.
937 * @param pPktHdr The packet header to dump.
938 * @param fRx Flag whether the packet was received (false indicates an outgoing packet).
939 */
940static void dbgcKdPktDump(PCKDPACKETHDR pPktHdr, PCRTSGSEG paSegs, uint32_t cSegs, bool fRx)
941{
942 RTSGBUF SgBuf;
943
944 RTSgBufInit(&SgBuf, paSegs, cSegs);
945
946 Log3(("%s KDPKTHDR(%#x, %#x (%s), %u, %#x, %#x)\n",
947 fRx ? "=>" : "<=",
948 pPktHdr->u32Signature, pPktHdr->u16SubType, dbgcKdPktDumpSubTypeToStr(pPktHdr->u16SubType),
949 pPktHdr->cbBody, pPktHdr->idPacket, pPktHdr->u32ChkSum));
950 switch (pPktHdr->u16SubType)
951 {
952 case KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE:
953 dbgcKdPktDumpManipulate(&SgBuf);
954 break;
955 default:
956 break;
957 }
958}
959
960
961/**
962 * Wrapper for the I/O interface write callback.
963 *
964 * @returns Status code.
965 * @param pThis The KD context.
966 * @param pvPkt The packet data to send.
967 * @param cbPkt Size of the packet in bytes.
968 */
969DECLINLINE(int) dbgcKdCtxWrite(PKDCTX pThis, const void *pvPkt, size_t cbPkt)
970{
971 return pThis->Dbgc.pBack->pfnWrite(pThis->Dbgc.pBack, pvPkt, cbPkt, NULL /*pcbWritten*/);
972}
973
974
975/**
976 * Fills in the given 64bit NT context structure with the requested values.
977 *
978 * @returns VBox status code.
979 * @param pThis The KD context.
980 * @param idCpu The CPU to query the context for.
981 * @param pNtCtx The NT context structure to fill in.
982 * @param pCtxFlags Combination of NTCONTEXT_F_XXX determining what to fill in.
983 */
984static int dbgcKdCtxQueryNtCtx64(PKDCTX pThis, VMCPUID idCpu, PNTCONTEXT64 pNtCtx, uint32_t fCtxFlags)
985{
986 RT_BZERO(pNtCtx, sizeof(*pNtCtx));
987
988 pNtCtx->fContext = NTCONTEXT_F_AMD64;
989 int rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_MXCSR, &pNtCtx->u32RegMxCsr);
990
991 if ( RT_SUCCESS(rc)
992 && fCtxFlags & NTCONTEXT_F_CONTROL)
993 {
994 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_CS, &pNtCtx->u16SegCs);
995 if (RT_SUCCESS(rc))
996 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_SS, &pNtCtx->u16SegSs);
997 if (RT_SUCCESS(rc))
998 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RIP, &pNtCtx->u64RegRip);
999 if (RT_SUCCESS(rc))
1000 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RSP, &pNtCtx->u64RegRsp);
1001 if (RT_SUCCESS(rc))
1002 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RBP, &pNtCtx->u64RegRbp);
1003 if (RT_SUCCESS(rc))
1004 rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_EFLAGS, &pNtCtx->u32RegEflags);
1005 if (RT_SUCCESS(rc))
1006 pNtCtx->fContext |= NTCONTEXT_F_CONTROL;
1007 }
1008
1009 if ( RT_SUCCESS(rc)
1010 && fCtxFlags & NTCONTEXT_F_INTEGER)
1011 {
1012 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RAX, &pNtCtx->u64RegRax);
1013 if (RT_SUCCESS(rc))
1014 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RCX, &pNtCtx->u64RegRcx);
1015 if (RT_SUCCESS(rc))
1016 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RDX, &pNtCtx->u64RegRdx);
1017 if (RT_SUCCESS(rc))
1018 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RBX, &pNtCtx->u64RegRbx);
1019 if (RT_SUCCESS(rc))
1020 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RSI, &pNtCtx->u64RegRsi);
1021 if (RT_SUCCESS(rc))
1022 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_RDI, &pNtCtx->u64RegRdi);
1023 if (RT_SUCCESS(rc))
1024 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R8, &pNtCtx->u64RegR8);
1025 if (RT_SUCCESS(rc))
1026 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R9, &pNtCtx->u64RegR9);
1027 if (RT_SUCCESS(rc))
1028 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R10, &pNtCtx->u64RegR10);
1029 if (RT_SUCCESS(rc))
1030 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R11, &pNtCtx->u64RegR11);
1031 if (RT_SUCCESS(rc))
1032 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R12, &pNtCtx->u64RegR12);
1033 if (RT_SUCCESS(rc))
1034 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R13, &pNtCtx->u64RegR13);
1035 if (RT_SUCCESS(rc))
1036 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R14, &pNtCtx->u64RegR14);
1037 if (RT_SUCCESS(rc))
1038 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_R15, &pNtCtx->u64RegR15);
1039 if (RT_SUCCESS(rc))
1040 pNtCtx->fContext |= NTCONTEXT_F_INTEGER;
1041 }
1042
1043 if ( RT_SUCCESS(rc)
1044 && fCtxFlags & NTCONTEXT_F_SEGMENTS)
1045 {
1046 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_DS, &pNtCtx->u16SegDs);
1047 if (RT_SUCCESS(rc))
1048 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_ES, &pNtCtx->u16SegEs);
1049 if (RT_SUCCESS(rc))
1050 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_FS, &pNtCtx->u16SegFs);
1051 if (RT_SUCCESS(rc))
1052 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_GS, &pNtCtx->u16SegGs);
1053 if (RT_SUCCESS(rc))
1054 pNtCtx->fContext |= NTCONTEXT_F_SEGMENTS;
1055 }
1056
1057 if ( RT_SUCCESS(rc)
1058 && fCtxFlags & NTCONTEXT_F_FLOATING_POINT)
1059 {
1060 /** @todo. */
1061 }
1062
1063 if ( RT_SUCCESS(rc)
1064 && fCtxFlags & NTCONTEXT_F_DEBUG)
1065 {
1066 /** @todo. */
1067 }
1068
1069 return rc;
1070}
1071
1072
1073/**
1074 * Fills in the given 64bit NT kernel context structure with the requested values.
1075 *
1076 * @returns VBox status code.
1077 * @param pThis The KD context.
1078 * @param idCpu The CPU to query the context for.
1079 * @param pNtCtx The NT context structure to fill in.
1080 * @param pCtxFlags Combination of NTCONTEXT_F_XXX determining what to fill in.
1081 */
1082static int dbgcKdCtxQueryNtKCtx64(PKDCTX pThis, VMCPUID idCpu, PNTKCONTEXT64 pKNtCtx, uint32_t fCtxFlags)
1083{
1084 RT_BZERO(pKNtCtx, sizeof(*pKNtCtx));
1085
1086 int rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR0, &pKNtCtx->u64RegCr0);
1087 if (RT_SUCCESS(rc))
1088 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR2, &pKNtCtx->u64RegCr2);
1089 if (RT_SUCCESS(rc))
1090 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR3, &pKNtCtx->u64RegCr3);
1091 if (RT_SUCCESS(rc))
1092 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR4, &pKNtCtx->u64RegCr4);
1093 if (RT_SUCCESS(rc))
1094 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR8, &pKNtCtx->u64RegCr8);
1095 if (RT_SUCCESS(rc))
1096 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_DR0, &pKNtCtx->u64RegDr0);
1097 if (RT_SUCCESS(rc))
1098 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_DR1, &pKNtCtx->u64RegDr1);
1099 if (RT_SUCCESS(rc))
1100 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_DR2, &pKNtCtx->u64RegDr2);
1101 if (RT_SUCCESS(rc))
1102 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_DR3, &pKNtCtx->u64RegDr3);
1103 if (RT_SUCCESS(rc))
1104 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_DR6, &pKNtCtx->u64RegDr6);
1105 if (RT_SUCCESS(rc))
1106 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_DR7, &pKNtCtx->u64RegDr7);
1107 if (RT_SUCCESS(rc))
1108 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_GDTR_LIMIT, &pKNtCtx->Gdtr.u16Limit);
1109 if (RT_SUCCESS(rc))
1110 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_GDTR_BASE, &pKNtCtx->Gdtr.u64PtrBase);
1111 if (RT_SUCCESS(rc))
1112 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_IDTR_LIMIT, &pKNtCtx->Idtr.u16Limit);
1113 if (RT_SUCCESS(rc))
1114 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_IDTR_BASE, &pKNtCtx->Idtr.u64PtrBase);
1115 if (RT_SUCCESS(rc))
1116 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_TR, &pKNtCtx->u16RegTr);
1117 if (RT_SUCCESS(rc))
1118 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_LDTR, &pKNtCtx->u16RegLdtr);
1119 if (RT_SUCCESS(rc))
1120 rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_MXCSR, &pKNtCtx->u32RegMxCsr);
1121 /** @todo Debug control and stuff. */
1122
1123 if (RT_SUCCESS(rc))
1124 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_GS_BASE, &pKNtCtx->u64MsrGsBase);
1125 if (RT_SUCCESS(rc))
1126 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_KERNEL_GS_BASE, &pKNtCtx->u64MsrKernelGsBase);
1127 if (RT_SUCCESS(rc))
1128 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K6_STAR, &pKNtCtx->u64MsrStar);
1129 if (RT_SUCCESS(rc))
1130 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_LSTAR, &pKNtCtx->u64MsrLstar);
1131 if (RT_SUCCESS(rc))
1132 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_CSTAR, &pKNtCtx->u64MsrCstar);
1133 if (RT_SUCCESS(rc))
1134 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_SF_MASK, &pKNtCtx->u64MsrSfMask);
1135 /** @todo XCR0 */
1136
1137 if (RT_SUCCESS(rc))
1138 rc = dbgcKdCtxQueryNtCtx64(pThis, idCpu, &pKNtCtx->Ctx, fCtxFlags);
1139
1140 return rc;
1141}
1142
1143
1144/**
1145 * Validates the given KD packet header.
1146 *
1147 * @returns Flag whether the packet header is valid, false if invalid.
1148 * @param pPktHdr The packet header to validate.
1149 */
1150static bool dbgcKdPktHdrValidate(PCKDPACKETHDR pPktHdr)
1151{
1152 if ( pPktHdr->u32Signature != KD_PACKET_HDR_SIGNATURE_DATA
1153 && pPktHdr->u32Signature != KD_PACKET_HDR_SIGNATURE_CONTROL
1154 && pPktHdr->u32Signature != KD_PACKET_HDR_SIGNATURE_BREAKIN)
1155 return false;
1156
1157 if (pPktHdr->u16SubType >= KD_PACKET_HDR_SUB_TYPE_MAX)
1158 return false;
1159
1160 uint32_t idPacket = pPktHdr->idPacket & UINT32_C(0xfffffffe);
1161 if ( idPacket != KD_PACKET_HDR_ID_INITIAL
1162 && idPacket != KD_PACKET_HDR_ID_RESET
1163 && idPacket != 0 /* Happens on the very first packet */)
1164 return false;
1165
1166 return true;
1167}
1168
1169
1170/**
1171 * Generates a checksum from the given buffer.
1172 *
1173 * @returns Generated checksum.
1174 * @param pv The data to generate a checksum from.
1175 * @param cb Number of bytes to checksum.
1176 */
1177static uint32_t dbgcKdPktChkSumGen(const void *pv, size_t cb)
1178{
1179 const uint8_t *pb = (const uint8_t *)pv;
1180 uint32_t u32ChkSum = 0;
1181
1182 while (cb--)
1183 u32ChkSum += *pb++;
1184
1185 return u32ChkSum;
1186}
1187
1188
1189/**
1190 * Generates a checksum from the given segments.
1191 *
1192 * @returns Generated checksum.
1193 * @param paSegs Pointer to the array of segments containing the data.
1194 * @param cSegs Number of segments.
1195 * @param pcbChkSum Where to store the number of bytes checksummed, optional.
1196 */
1197static uint32_t dbgcKdPktChkSumGenSg(PCRTSGSEG paSegs, uint32_t cSegs, size_t *pcbChkSum)
1198{
1199 size_t cbChkSum = 0;
1200 uint32_t u32ChkSum = 0;
1201
1202 for (uint32_t i = 0; i < cSegs; i++)
1203 {
1204 u32ChkSum += dbgcKdPktChkSumGen(paSegs[i].pvSeg, paSegs[i].cbSeg);
1205 cbChkSum += paSegs[i].cbSeg;
1206 }
1207
1208 if (pcbChkSum)
1209 *pcbChkSum = cbChkSum;
1210
1211 return u32ChkSum;
1212}
1213
1214
1215/**
1216 * Waits for an acknowledgment.
1217 *
1218 * @returns VBox status code.
1219 * @param pThis The KD context.
1220 * @param msWait Maximum number of milliseconds to wait for an acknowledge.
1221 * @param pfResend Where to store the resend requested flag on success.
1222 */
1223static int dbgcKdCtxPktWaitForAck(PKDCTX pThis, RTMSINTERVAL msWait, bool *pfResend)
1224{
1225 KDPACKETHDR PktAck;
1226 uint8_t *pbCur = (uint8_t *)&PktAck;
1227 size_t cbLeft = sizeof(PktAck);
1228 uint64_t tsStartMs = RTTimeMilliTS();
1229 int rc = VINF_SUCCESS;
1230
1231 LogFlowFunc(("pThis=%p msWait=%u pfResend=%p\n", pThis, msWait, pfResend));
1232
1233 RT_ZERO(PktAck);
1234
1235 /* There might be breakin packets in the queue, read until we get something else. */
1236 while ( msWait
1237 && RT_SUCCESS(rc))
1238 {
1239 if (pThis->Dbgc.pBack->pfnInput(pThis->Dbgc.pBack, msWait))
1240 {
1241 size_t cbRead = 0;
1242 rc = pThis->Dbgc.pBack->pfnRead(pThis->Dbgc.pBack, pbCur, 1, &cbRead);
1243 if ( RT_SUCCESS(rc)
1244 && cbRead == 1)
1245 {
1246 uint64_t tsSpanMs = RTTimeMilliTS() - tsStartMs;
1247 msWait -= RT_MIN(msWait, tsSpanMs);
1248 tsStartMs = RTTimeMilliTS();
1249
1250 if (*pbCur == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
1251 pThis->fBreakinRecv = true;
1252 else
1253 {
1254 pbCur++;
1255 cbLeft--;
1256 break;
1257 }
1258 }
1259 }
1260 else
1261 rc = VERR_TIMEOUT;
1262 }
1263
1264 if ( RT_SUCCESS(rc)
1265 && !msWait)
1266 rc = VERR_TIMEOUT;
1267
1268 if (RT_SUCCESS(rc))
1269 {
1270 while ( msWait
1271 && RT_SUCCESS(rc)
1272 && cbLeft)
1273 {
1274 if (pThis->Dbgc.pBack->pfnInput(pThis->Dbgc.pBack, msWait))
1275 {
1276 size_t cbRead = 0;
1277 rc = pThis->Dbgc.pBack->pfnRead(pThis->Dbgc.pBack, pbCur, cbLeft, &cbRead);
1278 if (RT_SUCCESS(rc))
1279 {
1280 uint64_t tsSpanMs = RTTimeMilliTS() - tsStartMs;
1281 msWait -= RT_MIN(msWait, tsSpanMs);
1282 tsStartMs = RTTimeMilliTS();
1283
1284 cbLeft -= cbRead;
1285 pbCur += cbRead;
1286 }
1287 }
1288 else
1289 rc = VERR_TIMEOUT;
1290 }
1291
1292 if (RT_SUCCESS(rc))
1293 {
1294 if (PktAck.u32Signature == KD_PACKET_HDR_SIGNATURE_CONTROL)
1295 {
1296 if (PktAck.u16SubType == KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE)
1297 rc = VINF_SUCCESS;
1298 else if (PktAck.u16SubType == KD_PACKET_HDR_SUB_TYPE_RESEND)
1299 {
1300 *pfResend = true;
1301 rc = VINF_SUCCESS;
1302 }
1303 else
1304 rc = VERR_NET_PROTOCOL_ERROR;
1305 }
1306 else
1307 rc = VERR_NET_PROTOCOL_ERROR;
1308 }
1309 }
1310
1311 LogFlowFunc(("returns rc=%Rrc *pfResend=%RTbool\n", rc, *pfResend));
1312 return rc;
1313}
1314
1315
1316/**
1317 * Sends the given packet header and optional segmented body (the trailing byte is sent automatically).
1318 *
1319 * @returns VBox status code.
1320 * @param pThis The KD context.
1321 * @param u32Signature The signature to send.
1322 * @param u16SubType The sub type to send.
1323 * @param idPacket Packet ID to send.
1324 * @param paSegs Pointer to the array of segments to send in the body, optional.
1325 * @param cSegs Number of segments.
1326 * @param fAck Flag whether to wait for an acknowledge.
1327 */
1328static int dbgcKdCtxPktSendSg(PKDCTX pThis, uint32_t u32Signature, uint16_t u16SubType,
1329 PCRTSGSEG paSegs, uint32_t cSegs, bool fAck)
1330{
1331 int rc = VINF_SUCCESS;
1332 uint32_t cRetriesLeft = 3;
1333 uint8_t bTrailer = KD_PACKET_TRAILING_BYTE;
1334 KDPACKETHDR Hdr;
1335
1336 size_t cbChkSum = 0;
1337 uint32_t u32ChkSum = dbgcKdPktChkSumGenSg(paSegs, cSegs, &cbChkSum);
1338
1339 Hdr.u32Signature = u32Signature;
1340 Hdr.u16SubType = u16SubType;
1341 Hdr.cbBody = (uint16_t)cbChkSum;
1342 Hdr.idPacket = pThis->idPktNext;
1343 Hdr.u32ChkSum = u32ChkSum;
1344
1345 dbgcKdPktDump(&Hdr, paSegs, cSegs, false /*fRx*/);
1346
1347 while (cRetriesLeft--)
1348 {
1349 bool fResend = false;
1350
1351 rc = dbgcKdCtxWrite(pThis, &Hdr, sizeof(Hdr));
1352 if ( RT_SUCCESS(rc)
1353 && paSegs
1354 && cSegs)
1355 {
1356 for (uint32_t i = 0; i < cSegs && RT_SUCCESS(rc); i++)
1357 rc = dbgcKdCtxWrite(pThis, paSegs[i].pvSeg, paSegs[i].cbSeg);
1358
1359 if (RT_SUCCESS(rc))
1360 rc = dbgcKdCtxWrite(pThis, &bTrailer, sizeof(bTrailer));
1361 }
1362
1363 if (RT_SUCCESS(rc))
1364 {
1365 if (fAck)
1366 rc = dbgcKdCtxPktWaitForAck(pThis, 10 * 1000, &fResend);
1367
1368 if ( RT_SUCCESS(rc)
1369 && !fResend)
1370 break;
1371 }
1372 }
1373
1374 return rc;
1375}
1376
1377
1378/**
1379 * Sends the given packet header and optional body (the trailing byte is sent automatically).
1380 *
1381 * @returns VBox status code.
1382 * @param pThis The KD context.
1383 * @param u32Signature The signature to send.
1384 * @param u16SubType The sub type to send.
1385 * @param pvBody The body to send, optional.
1386 * @param cbBody Body size in bytes.
1387 * @param fAck Flag whether to wait for an acknowledge.
1388 */
1389DECLINLINE(int) dbgcKdCtxPktSend(PKDCTX pThis, uint32_t u32Signature, uint16_t u16SubType,
1390 const void *pvBody, size_t cbBody,
1391 bool fAck)
1392{
1393 RTSGSEG Seg;
1394
1395 Seg.pvSeg = (void *)pvBody;
1396 Seg.cbSeg = cbBody;
1397 return dbgcKdCtxPktSendSg(pThis, u32Signature, u16SubType, cbBody ? &Seg : NULL, cbBody ? 1 : 0, fAck);
1398}
1399
1400
1401/**
1402 * Sends a resend packet answer.
1403 *
1404 * @returns VBox status code.
1405 * @param pThis The KD context.
1406 */
1407DECLINLINE(int) dbgcKdCtxPktSendResend(PKDCTX pThis)
1408{
1409 return dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_CONTROL, KD_PACKET_HDR_SUB_TYPE_RESEND,
1410 NULL /*pvBody*/, 0 /*cbBody*/, false /*fAck*/);
1411}
1412
1413
1414/**
1415 * Sends a resend packet answer.
1416 *
1417 * @returns VBox status code.
1418 * @param pThis The KD context.
1419 */
1420DECLINLINE(int) dbgcKdCtxPktSendReset(PKDCTX pThis)
1421{
1422 pThis->idPktNext = KD_PACKET_HDR_ID_INITIAL;
1423 return dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_CONTROL, KD_PACKET_HDR_SUB_TYPE_RESET,
1424 NULL /*pvBody*/, 0 /*cbBody*/, false /*fAck*/);
1425}
1426
1427
1428/**
1429 * Sends an acknowledge packet answer.
1430 *
1431 * @returns VBox status code.
1432 * @param pThis The KD context.
1433 */
1434DECLINLINE(int) dbgcKdCtxPktSendAck(PKDCTX pThis)
1435{
1436 return dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_CONTROL, KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE,
1437 NULL /*pvBody*/, 0 /*cbBody*/, false /*fAck*/);
1438}
1439
1440
1441/**
1442 * Resets the packet receive state machine.
1443 *
1444 * @returns nothing.
1445 * @param pThis The KD context.
1446 */
1447static void dbgcKdCtxPktRecvReset(PKDCTX pThis)
1448{
1449 pThis->enmState = KDRECVSTATE_PACKET_HDR_FIRST_BYTE;
1450 pThis->pbRecv = &pThis->PktHdr.ab[0];
1451 pThis->cbRecvLeft = sizeof(pThis->PktHdr.ab[0]);
1452 pThis->msRecvTimeout = RT_INDEFINITE_WAIT;
1453 pThis->tsRecvLast = RTTimeMilliTS();
1454}
1455
1456
1457/**
1458 * Sends a state change event packet.
1459 *
1460 * @returns VBox status code.
1461 * @param pThis The KD context data.
1462 * @param enmType The event type.
1463 */
1464static int dbgcKdCtxStateChangeSend(PKDCTX pThis, DBGFEVENTTYPE enmType)
1465{
1466 LogFlowFunc(("pThis=%p enmType=%u\n", pThis, enmType));
1467
1468 /* Select the record to send based on the CPU mode. */
1469 int rc = VINF_SUCCESS;
1470 CPUMMODE enmMode = DBGCCmdHlpGetCpuMode(&pThis->Dbgc.CmdHlp);
1471 switch (enmMode)
1472 {
1473 case CPUMMODE_PROTECTED:
1474 {
1475 break;
1476 }
1477 case CPUMMODE_LONG:
1478 {
1479 KDPACKETSTATECHANGE64 StateChange64;
1480 RT_ZERO(StateChange64);
1481
1482 StateChange64.u32StateNew = KD_PACKET_STATE_CHANGE_EXCEPTION;
1483 StateChange64.u16CpuLvl = 0x6; /** @todo Figure this one out. */
1484 StateChange64.idCpu = pThis->Dbgc.idCpu;
1485 StateChange64.cCpus = (uint16_t)DBGFR3CpuGetCount(pThis->Dbgc.pUVM);
1486 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_RIP, &StateChange64.u64RipThread);
1487 if (RT_SUCCESS(rc))
1488 {
1489 /** @todo Properly fill in the exception record. */
1490 switch (enmType)
1491 {
1492 case DBGFEVENT_HALT_DONE:
1493 case DBGFEVENT_BREAKPOINT:
1494 case DBGFEVENT_BREAKPOINT_IO:
1495 case DBGFEVENT_BREAKPOINT_MMIO:
1496 case DBGFEVENT_BREAKPOINT_HYPER:
1497 StateChange64.u.Exception.ExcpRec.u32ExcpCode = KD_PACKET_EXCP_CODE_BKPT;
1498 break;
1499 case DBGFEVENT_STEPPED:
1500 case DBGFEVENT_STEPPED_HYPER:
1501 StateChange64.u.Exception.ExcpRec.u32ExcpCode = KD_PACKET_EXCP_CODE_SINGLE_STEP;
1502 break;
1503 default:
1504 AssertMsgFailed(("Invalid DBGF event type for state change %d!\n", enmType));
1505 }
1506
1507 StateChange64.u.Exception.ExcpRec.cExcpParms = 3;
1508 StateChange64.u.Exception.u32FirstChance = 0x1;
1509
1510 /** @todo Properly fill in the control report. */
1511 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_DR6, &StateChange64.uCtrlReport.Amd64.u64RegDr6);
1512 if (RT_SUCCESS(rc))
1513 rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_DR7, &StateChange64.uCtrlReport.Amd64.u64RegDr7);
1514 if (RT_SUCCESS(rc))
1515 rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_RFLAGS, &StateChange64.uCtrlReport.Amd64.u32RegEflags);
1516 if (RT_SUCCESS(rc))
1517 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_CS, &StateChange64.uCtrlReport.Amd64.u16SegCs);
1518 if (RT_SUCCESS(rc))
1519 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_DS, &StateChange64.uCtrlReport.Amd64.u16SegDs);
1520 if (RT_SUCCESS(rc))
1521 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_ES, &StateChange64.uCtrlReport.Amd64.u16SegEs);
1522 if (RT_SUCCESS(rc))
1523 rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_FS, &StateChange64.uCtrlReport.Amd64.u16SegFs);
1524
1525 /* Read instruction bytes. */
1526 StateChange64.uCtrlReport.Amd64.cbInsnStream = sizeof(StateChange64.uCtrlReport.Amd64.abInsn);
1527 DBGFADDRESS AddrRip;
1528 DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &AddrRip, StateChange64.u64RipThread);
1529 rc = DBGFR3MemRead(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, &AddrRip,
1530 &StateChange64.uCtrlReport.Amd64.abInsn[0], StateChange64.uCtrlReport.Amd64.cbInsnStream);
1531 if (RT_SUCCESS(rc))
1532 {
1533 pThis->idPktNext = KD_PACKET_HDR_ID_INITIAL;
1534 rc = dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE64,
1535 &StateChange64, sizeof(StateChange64), false /*fAck*/);
1536 }
1537 }
1538 break;
1539 }
1540 case CPUMMODE_REAL:
1541 default:
1542 return DBGCCmdHlpPrintf(&pThis->Dbgc.CmdHlp, "error: Invalid CPU mode %d.\n", enmMode);
1543 }
1544
1545 LogFlowFunc(("returns %Rrc\n", rc));
1546 return rc;
1547}
1548
1549
1550/**
1551 * Processes a get version 64 request.
1552 *
1553 * @returns VBox status code.
1554 * @param pThis The KD context.
1555 * @param pPktManip The manipulate packet request.
1556 */
1557static int dbgcKdCtxPktManipulate64GetVersion(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
1558{
1559 KDPACKETMANIPULATE64 Resp;
1560 RT_ZERO(Resp);
1561
1562 /* Fill in the generic part. */
1563 Resp.Hdr.idReq = KD_PACKET_MANIPULATE_REQ_GET_VERSION;
1564 Resp.Hdr.u16CpuLvl = pPktManip->Hdr.u16CpuLvl;
1565 Resp.Hdr.idCpu = pPktManip->Hdr.idCpu;
1566 Resp.Hdr.u32NtStatus = NTSTATUS_SUCCESS;
1567
1568#if 0
1569 /* Build our own response in case there is no Windows interface available. */
1570 if (pThis->pIfWinNt)
1571 {
1572 RTGCUINTPTR GCPtrKpcr = 0;
1573
1574 int rc = pThis->pIfWinNt->pfnQueryKpcrForVCpu(pThis->pIfWinNt, pThis->Dbgc.pUVM, Resp.Hdr.idCpu,
1575 &GCPtrKpcr, NULL /*pKpcrb*/);
1576 if (RT_SUCCESS(rc))
1577 {
1578 DBGFADDRESS AddrKdVersionBlock;
1579 DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &AddrKdVersionBlock, GCPtrKpcr + 0x108);
1580 rc = DBGFR3MemRead(pThis->Dbgc.pUVM, Resp.Hdr.idCpu, &AddrKdVersionBlock, &Resp.u.GetVersion, sizeof(Resp.u.GetVersion));
1581 }
1582 }
1583 else
1584#endif
1585 {
1586 /* Fill in the request specific part, the static parts are from an amd64 Windows 10 guest. */
1587 Resp.u.GetVersion.u16VersMaj = 0x0f;
1588 Resp.u.GetVersion.u16VersMin = 0x2800;
1589 Resp.u.GetVersion.u8VersProtocol = 0x6; /** From a Windows 10 guest. */
1590 Resp.u.GetVersion.u8VersKdSecondary = 0x2; /** From a Windows 10 guest. */
1591 Resp.u.GetVersion.fFlags = 0x5; /** 64bit pointer. */
1592 Resp.u.GetVersion.u16MachineType = IMAGE_FILE_MACHINE_AMD64;
1593 Resp.u.GetVersion.u8MaxPktType = KD_PACKET_HDR_SUB_TYPE_MAX;
1594 Resp.u.GetVersion.u8MaxStateChange = KD_PACKET_STATE_CHANGE_MAX - KD_PACKET_STATE_CHANGE_MIN;
1595 Resp.u.GetVersion.u8MaxManipulate = KD_PACKET_MANIPULATE_REQ_CLEAR_ALL_INTERNAL_BKPT - KD_PACKET_MANIPULATE_REQ_MIN;
1596 Resp.u.GetVersion.u64PtrDebuggerDataList = 0 ;//0xfffff800deadc0de;
1597 }
1598
1599 /* Try to fill in the rest using the OS digger interface if available. */
1600 int rc = VINF_SUCCESS;
1601 if (pThis->pIfWinNt)
1602 rc = pThis->pIfWinNt->pfnQueryKernelPtrs(pThis->pIfWinNt, pThis->Dbgc.pUVM, &Resp.u.GetVersion.u64PtrKernBase,
1603 &Resp.u.GetVersion.u64PtrPsLoadedModuleList);
1604 else
1605 {
1606 /** @todo */
1607 }
1608
1609 if (RT_SUCCESS(rc))
1610 rc = dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
1611 &Resp, sizeof(Resp), true /*fAck*/);
1612
1613 return rc;
1614}
1615
1616
1617/**
1618 * Processes a read virtual memory 64 request.
1619 *
1620 * @returns VBox status code.
1621 * @param pThis The KD context.
1622 * @param pPktManip The manipulate packet request.
1623 */
1624static int dbgcKdCtxPktManipulate64ReadMem(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
1625{
1626 KDPACKETMANIPULATEHDR RespHdr;
1627 KDPACKETMANIPULATE_XFERMEM64 XferMem64;
1628 uint8_t abMem[_4K];
1629 RT_ZERO(RespHdr); RT_ZERO(XferMem64);
1630
1631 DBGFADDRESS AddrRead;
1632 uint32_t cbRead = RT_MIN(sizeof(abMem), pPktManip->u.XferMem.cbXferReq);
1633 if (pPktManip->Hdr.idReq == KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM)
1634 DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &AddrRead, pPktManip->u.XferMem.u64PtrTarget);
1635 else
1636 DBGFR3AddrFromPhys(pThis->Dbgc.pUVM, &AddrRead, pPktManip->u.XferMem.u64PtrTarget);
1637
1638 RTSGSEG aRespSegs[3];
1639 uint32_t cSegs = 2; /* Gets incremented when read is successful. */
1640 RespHdr.idReq = pPktManip->Hdr.idReq;
1641 RespHdr.u16CpuLvl = pPktManip->Hdr.u16CpuLvl;
1642 RespHdr.idCpu = pPktManip->Hdr.idCpu;
1643 RespHdr.u32NtStatus = NTSTATUS_SUCCESS;
1644
1645 XferMem64.u64PtrTarget = pPktManip->u.XferMem.u64PtrTarget;
1646 XferMem64.cbXferReq = pPktManip->u.XferMem.cbXferReq;
1647 XferMem64.cbXfered = (uint32_t)cbRead;
1648
1649 aRespSegs[0].pvSeg = &RespHdr;
1650 aRespSegs[0].cbSeg = sizeof(RespHdr);
1651 aRespSegs[1].pvSeg = &XferMem64;
1652 aRespSegs[1].cbSeg = sizeof(XferMem64);
1653
1654 int rc = DBGFR3MemRead(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, &AddrRead, &abMem[0], cbRead);
1655 if (RT_SUCCESS(rc))
1656 {
1657 cSegs++;
1658 aRespSegs[2].pvSeg = &abMem[0];
1659 aRespSegs[2].cbSeg = cbRead;
1660 }
1661 else
1662 RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Convert to an appropriate NT status code. */
1663
1664 return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
1665 &aRespSegs[0], cSegs, true /*fAck*/);
1666}
1667
1668
1669/**
1670 * Processes a read control space 64 request.
1671 *
1672 * @returns VBox status code.
1673 * @param pThis The KD context.
1674 * @param pPktManip The manipulate packet request.
1675 */
1676static int dbgcKdCtxPktManipulate64ReadCtrlSpace(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
1677{
1678 KDPACKETMANIPULATEHDR RespHdr;
1679 KDPACKETMANIPULATE_XFERCTRLSPACE64 XferCtrlSpace64;
1680 uint8_t abResp[sizeof(NTKCONTEXT64)];
1681 size_t cbData = 0;
1682 RT_ZERO(RespHdr); RT_ZERO(XferCtrlSpace64);
1683 RT_ZERO(abResp);
1684
1685 RTSGSEG aRespSegs[3];
1686 uint32_t cSegs = 2; /* Gets incremented when read is successful. */
1687 RespHdr.idReq = KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE;
1688 RespHdr.u16CpuLvl = pPktManip->Hdr.u16CpuLvl;
1689 RespHdr.idCpu = pPktManip->Hdr.idCpu;
1690 RespHdr.u32NtStatus = NTSTATUS_SUCCESS;
1691
1692 XferCtrlSpace64.u64IdXfer = pPktManip->u.XferCtrlSpace.u64IdXfer;
1693 XferCtrlSpace64.cbXferReq = pPktManip->u.XferCtrlSpace.cbXferReq;
1694
1695 aRespSegs[0].pvSeg = &RespHdr;
1696 aRespSegs[0].cbSeg = sizeof(RespHdr);
1697 aRespSegs[1].pvSeg = &XferCtrlSpace64;
1698 aRespSegs[1].cbSeg = sizeof(XferCtrlSpace64);
1699
1700 int rc = VINF_SUCCESS;
1701 switch (pPktManip->u.XferCtrlSpace.u64IdXfer)
1702 {
1703 case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCR:
1704 {
1705 if (pThis->pIfWinNt)
1706 {
1707 RTGCUINTPTR GCPtrKpcr = 0;
1708
1709 rc = pThis->pIfWinNt->pfnQueryKpcrForVCpu(pThis->pIfWinNt, pThis->Dbgc.pUVM, RespHdr.idCpu,
1710 &GCPtrKpcr, NULL /*pKpcrb*/);
1711 if (RT_SUCCESS(rc))
1712 memcpy(&abResp[0], &GCPtrKpcr, sizeof(GCPtrKpcr));
1713 }
1714
1715 cbData = sizeof(RTGCUINTPTR);
1716 break;
1717 }
1718 case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCRB:
1719 {
1720 if (pThis->pIfWinNt)
1721 {
1722 RTGCUINTPTR GCPtrKpcrb = 0;
1723
1724 rc = pThis->pIfWinNt->pfnQueryKpcrForVCpu(pThis->pIfWinNt, pThis->Dbgc.pUVM, RespHdr.idCpu,
1725 NULL /*pKpcr*/, &GCPtrKpcrb);
1726 if (RT_SUCCESS(rc))
1727 memcpy(&abResp[0], &GCPtrKpcrb, sizeof(GCPtrKpcrb));
1728 }
1729
1730 cbData = sizeof(RTGCUINTPTR);
1731 break;
1732 }
1733 case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KCTX:
1734 {
1735 rc = dbgcKdCtxQueryNtKCtx64(pThis, RespHdr.idCpu, (PNTKCONTEXT64)&abResp[0], NTCONTEXT64_F_FULL);
1736 if (RT_SUCCESS(rc))
1737 cbData = sizeof(NTKCONTEXT64);
1738 break;
1739 }
1740 case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KTHRD:
1741 {
1742 if (pThis->pIfWinNt)
1743 {
1744 RTGCUINTPTR GCPtrCurThrd = 0;
1745
1746 rc = pThis->pIfWinNt->pfnQueryCurThrdForVCpu(pThis->pIfWinNt, pThis->Dbgc.pUVM, RespHdr.idCpu,
1747 &GCPtrCurThrd);
1748 if (RT_SUCCESS(rc))
1749 memcpy(&abResp[0], &GCPtrCurThrd, sizeof(GCPtrCurThrd));
1750 }
1751
1752 cbData = sizeof(RTGCUINTPTR);
1753 break;
1754 }
1755 default:
1756 rc = VERR_NOT_SUPPORTED;
1757 break;
1758 }
1759
1760 if ( RT_SUCCESS(rc)
1761 && cbData)
1762 {
1763 XferCtrlSpace64.cbXfered = RT_MIN(cbData, XferCtrlSpace64.cbXferReq);
1764
1765 cSegs++;
1766 aRespSegs[2].pvSeg = &abResp[0];
1767 aRespSegs[2].cbSeg = cbData;
1768 }
1769 else if (RT_FAILURE(rc))
1770 RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Convert to an appropriate NT status code. */
1771
1772 return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
1773 &aRespSegs[0], cSegs, true /*fAck*/);
1774}
1775
1776
1777/**
1778 * Processes a restore breakpoint 64 request.
1779 *
1780 * @returns VBox status code.
1781 * @param pThis The KD context.
1782 * @param pPktManip The manipulate packet request.
1783 */
1784static int dbgcKdCtxPktManipulate64RestoreBkpt(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
1785{
1786 KDPACKETMANIPULATEHDR RespHdr;
1787 KDPACKETMANIPULATE_RESTOREBKPT64 RestoreBkpt64;
1788 RT_ZERO(RespHdr); RT_ZERO(RestoreBkpt64);
1789
1790 RTSGSEG aRespSegs[2];
1791 RespHdr.idReq = KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT;
1792 RespHdr.u16CpuLvl = pPktManip->Hdr.u16CpuLvl;
1793 RespHdr.idCpu = pPktManip->Hdr.idCpu;
1794 RespHdr.u32NtStatus = NTSTATUS_SUCCESS;
1795
1796 RestoreBkpt64.u32HndBkpt = pPktManip->u.RestoreBkpt.u32HndBkpt;
1797
1798 aRespSegs[0].pvSeg = &RespHdr;
1799 aRespSegs[0].cbSeg = sizeof(RespHdr);
1800 aRespSegs[1].pvSeg = &RestoreBkpt64;
1801 aRespSegs[1].cbSeg = sizeof(RestoreBkpt64);
1802
1803 /** @todo */
1804
1805 return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
1806 &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
1807}
1808
1809
1810/**
1811 * Processes a get context extended 64 request.
1812 *
1813 * @returns VBox status code.
1814 * @param pThis The KD context.
1815 * @param pPktManip The manipulate packet request.
1816 */
1817static int dbgcKdCtxPktManipulate64GetContextEx(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
1818{
1819 KDPACKETMANIPULATEHDR RespHdr;
1820 KDPACKETMANIPULATE_CONTEXTEX ContextEx;
1821 NTCONTEXT64 NtCtx;
1822 RT_ZERO(RespHdr); RT_ZERO(ContextEx);
1823
1824 RTSGSEG aRespSegs[3];
1825 uint32_t cSegs = 2;
1826 RespHdr.idReq = KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX;
1827 RespHdr.u16CpuLvl = pPktManip->Hdr.u16CpuLvl;
1828 RespHdr.idCpu = pPktManip->Hdr.idCpu;
1829 RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL;
1830
1831 ContextEx.offStart = pPktManip->u.ContextEx.offStart;
1832 ContextEx.cbXfer = pPktManip->u.ContextEx.cbXfer;
1833 ContextEx.cbXfered = 0;
1834
1835 aRespSegs[0].pvSeg = &RespHdr;
1836 aRespSegs[0].cbSeg = sizeof(RespHdr);
1837 aRespSegs[1].pvSeg = &ContextEx;
1838 aRespSegs[1].cbSeg = sizeof(ContextEx);
1839
1840 int rc = dbgcKdCtxQueryNtCtx64(pThis, pPktManip->Hdr.idCpu, &NtCtx, NTCONTEXT64_F_FULL);
1841 if ( RT_SUCCESS(rc)
1842 && pPktManip->u.ContextEx.offStart < sizeof(NtCtx))
1843 {
1844 RespHdr.u32NtStatus = NTSTATUS_SUCCESS;
1845 ContextEx.cbXfered = RT_MIN(sizeof(NtCtx) - ContextEx.offStart, ContextEx.cbXfer);
1846
1847 aRespSegs[2].pvSeg = (uint8_t *)&NtCtx + ContextEx.offStart;
1848 aRespSegs[2].cbSeg = ContextEx.cbXfered;
1849 cSegs++;
1850 }
1851
1852 return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
1853 &aRespSegs[0], cSegs, true /*fAck*/);
1854}
1855
1856
1857/**
1858 * Processes a manipulate packet.
1859 *
1860 * @returns VBox status code.
1861 * @param pThis The KD context.
1862 */
1863static int dbgcKdCtxPktManipulate64Process(PKDCTX pThis)
1864{
1865 int rc = VINF_SUCCESS;
1866 PCKDPACKETMANIPULATE64 pPktManip = (PCKDPACKETMANIPULATE64)&pThis->abBody[0];
1867
1868 switch (pPktManip->Hdr.idReq)
1869 {
1870 case KD_PACKET_MANIPULATE_REQ_GET_VERSION:
1871 {
1872 rc = dbgcKdCtxPktManipulate64GetVersion(pThis, pPktManip);
1873 break;
1874 }
1875 case KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM:
1876 case KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM:
1877 {
1878 rc = dbgcKdCtxPktManipulate64ReadMem(pThis, pPktManip);
1879 break;
1880 }
1881 case KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE:
1882 {
1883 rc = dbgcKdCtxPktManipulate64ReadCtrlSpace(pThis, pPktManip);
1884 break;
1885 }
1886 case KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT:
1887 {
1888 rc = dbgcKdCtxPktManipulate64RestoreBkpt(pThis, pPktManip);
1889 break;
1890 }
1891 case KD_PACKET_MANIPULATE_REQ_CLEAR_ALL_INTERNAL_BKPT:
1892 /* WinDbg doesn't seem to expect an answer apart from the ACK here. */
1893 break;
1894 case KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX:
1895 {
1896 rc = dbgcKdCtxPktManipulate64GetContextEx(pThis, pPktManip);
1897 break;
1898 }
1899 default:
1900 KDPACKETMANIPULATEHDR RespHdr;
1901 RT_ZERO(RespHdr);
1902
1903 RespHdr.idReq = pPktManip->Hdr.idReq;
1904 RespHdr.u16CpuLvl = pPktManip->Hdr.u16CpuLvl;
1905 RespHdr.idCpu = pPktManip->Hdr.idCpu;
1906 RespHdr.u32NtStatus = NTSTATUS_NOT_IMPLEMENTED;
1907 rc = dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
1908 &RespHdr, sizeof(RespHdr), true /*fAck*/);
1909 break;
1910 }
1911
1912 return rc;
1913}
1914
1915
1916/**
1917 * Processes a fully received packet.
1918 *
1919 * @returns VBox status code.
1920 * @param pThis The KD context.
1921 */
1922static int dbgcKdCtxPktProcess(PKDCTX pThis)
1923{
1924 int rc = VINF_SUCCESS;
1925
1926 pThis->fBreakinRecv = false;
1927
1928 /* Verify checksum. */
1929 if (dbgcKdPktChkSumGen(&pThis->abBody[0], pThis->PktHdr.Fields.cbBody) == pThis->PktHdr.Fields.u32ChkSum)
1930 {
1931 /** @todo Check packet id. */
1932 if (pThis->PktHdr.Fields.u16SubType != KD_PACKET_HDR_SUB_TYPE_RESET)
1933 {
1934 pThis->idPktNext = pThis->PktHdr.Fields.idPacket;
1935 rc = dbgcKdCtxPktSendAck(pThis);
1936 }
1937 if (RT_SUCCESS(rc))
1938 {
1939#ifdef LOG_ENABLED
1940 RTSGSEG Seg;
1941 Seg.pvSeg = &pThis->abBody[0];
1942 Seg.cbSeg = pThis->PktHdr.Fields.cbBody;
1943 dbgcKdPktDump(&pThis->PktHdr.Fields, &Seg, 1 /*cSegs*/, true /*fRx*/);
1944#endif
1945
1946 switch (pThis->PktHdr.Fields.u16SubType)
1947 {
1948 case KD_PACKET_HDR_SUB_TYPE_RESET:
1949 {
1950 pThis->idPktNext = 0;
1951 rc = dbgcKdCtxPktSendReset(pThis);
1952 if (RT_SUCCESS(rc))
1953 {
1954 rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
1955 if (rc == VWRN_DBGF_ALREADY_HALTED)
1956 rc = dbgcKdCtxStateChangeSend(pThis, DBGFEVENT_HALT_DONE);
1957 }
1958 pThis->idPktNext = KD_PACKET_HDR_ID_RESET;
1959 break;
1960 }
1961 case KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE:
1962 {
1963 CPUMMODE enmMode = DBGCCmdHlpGetCpuMode(&pThis->Dbgc.CmdHlp);
1964 switch (enmMode)
1965 {
1966 case CPUMMODE_PROTECTED:
1967 {
1968 rc = VERR_NOT_IMPLEMENTED;
1969 break;
1970 }
1971 case CPUMMODE_LONG:
1972 {
1973 pThis->idPktNext = pThis->PktHdr.Fields.idPacket ^ 0x1;
1974 rc = dbgcKdCtxPktManipulate64Process(pThis);
1975 break;
1976 }
1977 case CPUMMODE_REAL:
1978 default:
1979 rc = VERR_NOT_SUPPORTED;
1980 break;
1981 }
1982 break;
1983 }
1984 case KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE:
1985 case KD_PACKET_HDR_SUB_TYPE_RESEND:
1986 {
1987 /* Don't do anything. */
1988 rc = VINF_SUCCESS;
1989 break;
1990 }
1991 default:
1992 rc = VERR_NOT_IMPLEMENTED;
1993 }
1994 }
1995 }
1996 else
1997 {
1998 pThis->idPktNext = pThis->PktHdr.Fields.idPacket;
1999 rc = dbgcKdCtxPktSendResend(pThis);
2000 }
2001
2002 if (pThis->fBreakinRecv)
2003 {
2004 pThis->fBreakinRecv = false;
2005 rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
2006 if (rc == VWRN_DBGF_ALREADY_HALTED)
2007 rc = dbgcKdCtxStateChangeSend(pThis, DBGFEVENT_HALT_DONE);
2008 }
2009
2010 /* Next packet. */
2011 dbgcKdCtxPktRecvReset(pThis);
2012 return rc;
2013}
2014
2015
2016/**
2017 * Processes the received data based on the current state.
2018 *
2019 * @returns VBox status code.
2020 * @param pThis The KD context.
2021 */
2022static int dbgcKdCtxRecvDataProcess(PKDCTX pThis)
2023{
2024 int rc = VINF_SUCCESS;
2025
2026 switch (pThis->enmState)
2027 {
2028 case KDRECVSTATE_PACKET_HDR_FIRST_BYTE:
2029 {
2030 /* Does it look like a valid packet start?. */
2031 if ( pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_DATA_BYTE
2032 || pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_CONTROL_BYTE)
2033 {
2034 pThis->pbRecv = &pThis->PktHdr.ab[1];
2035 pThis->cbRecvLeft = sizeof(pThis->PktHdr.ab[1]);
2036 pThis->enmState = KDRECVSTATE_PACKET_HDR_SECOND_BYTE;
2037 pThis->msRecvTimeout = DBGC_KD_RECV_TIMEOUT_MS;
2038 }
2039 else if (pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
2040 {
2041 rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
2042 if (rc == VWRN_DBGF_ALREADY_HALTED)
2043 rc = dbgcKdCtxStateChangeSend(pThis, DBGFEVENT_HALT_DONE);
2044 dbgcKdCtxPktRecvReset(pThis);
2045 }
2046 /* else: Ignore and continue. */
2047 break;
2048 }
2049 case KDRECVSTATE_PACKET_HDR_SECOND_BYTE:
2050 {
2051 /*
2052 * If the first and second byte differ there might be a single breakin
2053 * packet byte received and this is actually the start of a new packet.
2054 */
2055 if (pThis->PktHdr.ab[0] != pThis->PktHdr.ab[1])
2056 {
2057 if (pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
2058 {
2059 /* Halt the VM and rearrange the packet receiving state machine. */
2060 LogFlow(("DbgKd: Halting VM!\n"));
2061
2062 rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
2063 pThis->PktHdr.ab[0] = pThis->PktHdr.ab[1]; /* Overwrite the first byte with the new start. */
2064 pThis->pbRecv = &pThis->PktHdr.ab[1];
2065 pThis->cbRecvLeft = sizeof(pThis->PktHdr.ab[1]);
2066 }
2067 else
2068 rc = VERR_NET_PROTOCOL_ERROR; /* Refuse talking to the remote end any further. */
2069 }
2070 else
2071 {
2072 /* Normal packet receive continues with the rest of the header. */
2073 pThis->pbRecv = &pThis->PktHdr.ab[2];
2074 pThis->cbRecvLeft = sizeof(pThis->PktHdr.Fields) - 2;
2075 pThis->enmState = KDRECVSTATE_PACKET_HDR;
2076 }
2077 break;
2078 }
2079 case KDRECVSTATE_PACKET_HDR:
2080 {
2081 if ( dbgcKdPktHdrValidate(&pThis->PktHdr.Fields)
2082 && pThis->PktHdr.Fields.cbBody <= sizeof(pThis->abBody))
2083 {
2084 /* Start receiving the body. */
2085 if (pThis->PktHdr.Fields.cbBody)
2086 {
2087 pThis->pbRecv = &pThis->abBody[0];
2088 pThis->cbRecvLeft = pThis->PktHdr.Fields.cbBody;
2089 pThis->enmState = KDRECVSTATE_PACKET_BODY;
2090 }
2091 else /* No body means no trailer byte it looks like. */
2092 rc = dbgcKdCtxPktProcess(pThis);
2093 }
2094 else
2095 rc = VERR_NET_PROTOCOL_ERROR;
2096 break;
2097 }
2098 case KDRECVSTATE_PACKET_BODY:
2099 {
2100 pThis->enmState = KDRECVSTATE_PACKET_TRAILER;
2101 pThis->bTrailer = 0;
2102 pThis->pbRecv = &pThis->bTrailer;
2103 pThis->cbRecvLeft = sizeof(pThis->bTrailer);
2104 break;
2105 }
2106 case KDRECVSTATE_PACKET_TRAILER:
2107 {
2108 if (pThis->bTrailer == KD_PACKET_TRAILING_BYTE)
2109 rc = dbgcKdCtxPktProcess(pThis);
2110 else
2111 rc = VERR_NET_PROTOCOL_ERROR;
2112 break;
2113 }
2114 default:
2115 AssertMsgFailed(("Invalid receive state %d\n", pThis->enmState));
2116 }
2117
2118 return rc;
2119}
2120
2121
2122/**
2123 * Receive data and processes complete packets.
2124 *
2125 * @returns Status code.
2126 * @param pThis The KD context.
2127 */
2128static int dbgcKdCtxRecv(PKDCTX pThis)
2129{
2130 int rc = VINF_SUCCESS;
2131
2132 LogFlowFunc(("pThis=%p{.cbRecvLeft=%zu}\n", pThis, pThis->cbRecvLeft));
2133
2134 if (pThis->cbRecvLeft)
2135 {
2136 size_t cbRead = 0;
2137 rc = pThis->Dbgc.pBack->pfnRead(pThis->Dbgc.pBack, pThis->pbRecv, pThis->cbRecvLeft, &cbRead);
2138 if (RT_SUCCESS(rc))
2139 {
2140 pThis->tsRecvLast = RTTimeMilliTS();
2141 pThis->cbRecvLeft -= cbRead;
2142 pThis->pbRecv += cbRead;
2143 if (!pThis->cbRecvLeft)
2144 rc = dbgcKdCtxRecvDataProcess(pThis);
2145 }
2146 }
2147
2148 LogFlowFunc(("returns rc=%Rrc\n", rc));
2149 return rc;
2150}
2151
2152
2153/**
2154 * Processes debugger events.
2155 *
2156 * @returns VBox status code.
2157 * @param pThis The KD context data.
2158 * @param pEvent Pointer to event data.
2159 */
2160static int dbgcKdCtxProcessEvent(PKDCTX pThis, PCDBGFEVENT pEvent)
2161{
2162 /*
2163 * Process the event.
2164 */
2165 //PDBGC pDbgc = &pThis->Dbgc;
2166 pThis->Dbgc.pszScratch = &pThis->Dbgc.achInput[0];
2167 pThis->Dbgc.iArg = 0;
2168 int rc = VINF_SUCCESS;
2169 switch (pEvent->enmType)
2170 {
2171 /*
2172 * The first part is events we have initiated with commands.
2173 */
2174 case DBGFEVENT_HALT_DONE:
2175 {
2176 rc = dbgcKdCtxStateChangeSend(pThis, pEvent->enmType);
2177 break;
2178 }
2179
2180 /*
2181 * The second part is events which can occur at any time.
2182 */
2183#if 0
2184 case DBGFEVENT_FATAL_ERROR:
2185 {
2186 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbf event: Fatal error! (%s)\n",
2187 dbgcGetEventCtx(pEvent->enmCtx));
2188 if (RT_SUCCESS(rc))
2189 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
2190 break;
2191 }
2192#endif
2193
2194 case DBGFEVENT_BREAKPOINT:
2195 case DBGFEVENT_BREAKPOINT_IO:
2196 case DBGFEVENT_BREAKPOINT_MMIO:
2197 case DBGFEVENT_BREAKPOINT_HYPER:
2198 case DBGFEVENT_STEPPED:
2199 case DBGFEVENT_STEPPED_HYPER:
2200 {
2201 rc = dbgcKdCtxStateChangeSend(pThis, pEvent->enmType);
2202 break;
2203 }
2204
2205#if 0
2206 case DBGFEVENT_ASSERTION_HYPER:
2207 {
2208 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
2209 "\ndbgf event: Hypervisor Assertion! (%s)\n"
2210 "%s"
2211 "%s"
2212 "\n",
2213 dbgcGetEventCtx(pEvent->enmCtx),
2214 pEvent->u.Assert.pszMsg1,
2215 pEvent->u.Assert.pszMsg2);
2216 if (RT_SUCCESS(rc))
2217 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
2218 break;
2219 }
2220
2221 case DBGFEVENT_DEV_STOP:
2222 {
2223 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
2224 "\n"
2225 "dbgf event: DBGFSTOP (%s)\n"
2226 "File: %s\n"
2227 "Line: %d\n"
2228 "Function: %s\n",
2229 dbgcGetEventCtx(pEvent->enmCtx),
2230 pEvent->u.Src.pszFile,
2231 pEvent->u.Src.uLine,
2232 pEvent->u.Src.pszFunction);
2233 if (RT_SUCCESS(rc) && pEvent->u.Src.pszMessage && *pEvent->u.Src.pszMessage)
2234 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
2235 "Message: %s\n",
2236 pEvent->u.Src.pszMessage);
2237 if (RT_SUCCESS(rc))
2238 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
2239 break;
2240 }
2241
2242
2243 case DBGFEVENT_INVALID_COMMAND:
2244 {
2245 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Invalid command event!\n");
2246 break;
2247 }
2248#endif
2249
2250 case DBGFEVENT_POWERING_OFF:
2251 {
2252 pThis->Dbgc.fReady = false;
2253 pThis->Dbgc.pBack->pfnSetReady(pThis->Dbgc.pBack, false);
2254 rc = VERR_GENERAL_FAILURE;
2255 break;
2256 }
2257
2258#if 0
2259 default:
2260 {
2261 /*
2262 * Probably a generic event. Look it up to find its name.
2263 */
2264 PCDBGCSXEVT pEvtDesc = dbgcEventLookup(pEvent->enmType);
2265 if (pEvtDesc)
2266 {
2267 if (pEvtDesc->enmKind == kDbgcSxEventKind_Interrupt)
2268 {
2269 Assert(pEvtDesc->pszDesc);
2270 Assert(pEvent->u.Generic.cArgs == 1);
2271 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s no %#llx! (%s)\n",
2272 pEvtDesc->pszDesc, pEvent->u.Generic.auArgs[0], pEvtDesc->pszName);
2273 }
2274 else if (pEvtDesc->fFlags & DBGCSXEVT_F_BUGCHECK)
2275 {
2276 Assert(pEvent->u.Generic.cArgs >= 5);
2277 char szDetails[512];
2278 DBGFR3FormatBugCheck(pDbgc->pUVM, szDetails, sizeof(szDetails), pEvent->u.Generic.auArgs[0],
2279 pEvent->u.Generic.auArgs[1], pEvent->u.Generic.auArgs[2],
2280 pEvent->u.Generic.auArgs[3], pEvent->u.Generic.auArgs[4]);
2281 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s %s%s!\n%s", pEvtDesc->pszName,
2282 pEvtDesc->pszDesc ? "- " : "", pEvtDesc->pszDesc ? pEvtDesc->pszDesc : "",
2283 szDetails);
2284 }
2285 else if ( (pEvtDesc->fFlags & DBGCSXEVT_F_TAKE_ARG)
2286 || pEvent->u.Generic.cArgs > 1
2287 || ( pEvent->u.Generic.cArgs == 1
2288 && pEvent->u.Generic.auArgs[0] != 0))
2289 {
2290 if (pEvtDesc->pszDesc)
2291 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s - %s!",
2292 pEvtDesc->pszName, pEvtDesc->pszDesc);
2293 else
2294 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s!", pEvtDesc->pszName);
2295 if (pEvent->u.Generic.cArgs <= 1)
2296 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " arg=%#llx\n", pEvent->u.Generic.auArgs[0]);
2297 else
2298 {
2299 for (uint32_t i = 0; i < pEvent->u.Generic.cArgs; i++)
2300 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " args[%u]=%#llx", i, pEvent->u.Generic.auArgs[i]);
2301 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n");
2302 }
2303 }
2304 else
2305 {
2306 if (pEvtDesc->pszDesc)
2307 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s - %s!\n",
2308 pEvtDesc->pszName, pEvtDesc->pszDesc);
2309 else
2310 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s!\n", pEvtDesc->pszName);
2311 }
2312 }
2313 else
2314 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Unknown event %d!\n", pEvent->enmType);
2315 break;
2316 }
2317#else
2318 default:
2319 rc = VERR_NOT_IMPLEMENTED;
2320 break;
2321#endif
2322 }
2323
2324 return rc;
2325}
2326
2327
2328/**
2329 * Handle a receive timeout.
2330 *
2331 * @returns VBox status code.
2332 * @param pThis Pointer to the KD context.
2333 */
2334static int dbgcKdCtxRecvTimeout(PKDCTX pThis)
2335{
2336 int rc = VINF_SUCCESS;
2337
2338 LogFlowFunc(("pThis=%p\n", pThis));
2339
2340 /*
2341 * If a single breakin packet byte was received but the header is otherwise incomplete
2342 * the VM is halted and a state change will be sent in the event processing loop.
2343 */
2344 if ( pThis->enmState == KDRECVSTATE_PACKET_HDR_SECOND_BYTE
2345 && pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
2346 {
2347 LogFlow(("DbgKd: Halting VM!\n"));
2348 rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
2349 }
2350 else /* Send a reset packet (@todo Figure out the semantics in this case exactly). */
2351 rc = dbgcKdCtxPktSendReset(pThis);
2352
2353 dbgcKdCtxPktRecvReset(pThis);
2354
2355 LogFlowFunc(("rc=%Rrc\n", rc));
2356 return rc;
2357}
2358
2359
2360/**
2361 * Run the debugger console.
2362 *
2363 * @returns VBox status code.
2364 * @param pThis Pointer to the KD context.
2365 */
2366int dbgcKdRun(PKDCTX pThis)
2367{
2368 /*
2369 * We're ready for commands now.
2370 */
2371 pThis->Dbgc.fReady = true;
2372 pThis->Dbgc.pBack->pfnSetReady(pThis->Dbgc.pBack, true);
2373
2374 /*
2375 * Main Debugger Loop.
2376 *
2377 * This loop will either block on waiting for input or on waiting on
2378 * debug events. If we're forwarding the log we cannot wait for long
2379 * before we must flush the log.
2380 */
2381 int rc;
2382 for (;;)
2383 {
2384 rc = VERR_SEM_OUT_OF_TURN;
2385 if (pThis->Dbgc.pUVM)
2386 rc = DBGFR3QueryWaitable(pThis->Dbgc.pUVM);
2387
2388 if (RT_SUCCESS(rc))
2389 {
2390 /*
2391 * Wait for a debug event.
2392 */
2393 DBGFEVENT Evt;
2394 rc = DBGFR3EventWait(pThis->Dbgc.pUVM, 32, &Evt);
2395 if (RT_SUCCESS(rc))
2396 {
2397 rc = dbgcKdCtxProcessEvent(pThis, &Evt);
2398 if (RT_FAILURE(rc))
2399 break;
2400 }
2401 else if (rc != VERR_TIMEOUT)
2402 break;
2403
2404 /*
2405 * Check for input.
2406 */
2407 if (pThis->Dbgc.pBack->pfnInput(pThis->Dbgc.pBack, 0))
2408 {
2409 rc = dbgcKdCtxRecv(pThis);
2410 if (RT_FAILURE(rc))
2411 break;
2412 }
2413 }
2414 else if (rc == VERR_SEM_OUT_OF_TURN)
2415 {
2416 /*
2417 * Wait for input.
2418 */
2419 if (pThis->Dbgc.pBack->pfnInput(pThis->Dbgc.pBack, 1000))
2420 {
2421 rc = dbgcKdCtxRecv(pThis);
2422 if (RT_FAILURE(rc))
2423 break;
2424 }
2425 else if ( pThis->msRecvTimeout != RT_INDEFINITE_WAIT
2426 && (RTTimeMilliTS() - pThis->tsRecvLast >= pThis->msRecvTimeout))
2427 rc = dbgcKdCtxRecvTimeout(pThis);
2428 }
2429 else
2430 break;
2431 }
2432
2433 return rc;
2434}
2435
2436
2437/**
2438 * Creates a KD context instance with the given backend.
2439 *
2440 * @returns VBox status code.
2441 * @param ppKdCtx Where to store the pointer to the KD stub context instance on success.
2442 * @param pBack The backend to use for I/O.
2443 * @param fFlags Flags controlling the behavior.
2444 */
2445static int dbgcKdCtxCreate(PPKDCTX ppKdCtx, PDBGCBACK pBack, unsigned fFlags)
2446{
2447 /*
2448 * Validate input.
2449 */
2450 AssertPtrReturn(pBack, VERR_INVALID_POINTER);
2451 AssertMsgReturn(!fFlags, ("%#x", fFlags), VERR_INVALID_PARAMETER);
2452
2453 /*
2454 * Allocate and initialize.
2455 */
2456 PKDCTX pThis = (PKDCTX)RTMemAllocZ(sizeof(*pThis));
2457 if (!pThis)
2458 return VERR_NO_MEMORY;
2459
2460 dbgcInitCmdHlp(&pThis->Dbgc);
2461 /*
2462 * This is compied from the native debug console (will be used for monitor commands)
2463 * in DBGCConsole.cpp. Try to keep both functions in sync.
2464 */
2465 pThis->Dbgc.pBack = pBack;
2466 /*pThis->Dbgc.pfnOutput = dbgcOutputGdb;*/
2467 pThis->Dbgc.pvOutputUser = pThis;
2468 pThis->Dbgc.pVM = NULL;
2469 pThis->Dbgc.pUVM = NULL;
2470 pThis->Dbgc.idCpu = 0;
2471 pThis->Dbgc.hDbgAs = DBGF_AS_GLOBAL;
2472 pThis->Dbgc.pszEmulation = "CodeView/WinDbg";
2473 pThis->Dbgc.paEmulationCmds = &g_aCmdsCodeView[0];
2474 pThis->Dbgc.cEmulationCmds = g_cCmdsCodeView;
2475 pThis->Dbgc.paEmulationFuncs = &g_aFuncsCodeView[0];
2476 pThis->Dbgc.cEmulationFuncs = g_cFuncsCodeView;
2477 //pThis->Dbgc.fLog = false;
2478 pThis->Dbgc.fRegTerse = true;
2479 pThis->Dbgc.fStepTraceRegs = true;
2480 //pThis->Dbgc.cPagingHierarchyDumps = 0;
2481 //pThis->Dbgc.DisasmPos = {0};
2482 //pThis->Dbgc.SourcePos = {0};
2483 //pThis->Dbgc.DumpPos = {0};
2484 pThis->Dbgc.pLastPos = &pThis->Dbgc.DisasmPos;
2485 //pThis->Dbgc.cbDumpElement = 0;
2486 //pThis->Dbgc.cVars = 0;
2487 //pThis->Dbgc.paVars = NULL;
2488 //pThis->Dbgc.pPlugInHead = NULL;
2489 //pThis->Dbgc.pFirstBp = NULL;
2490 //pThis->Dbgc.abSearch = {0};
2491 //pThis->Dbgc.cbSearch = 0;
2492 pThis->Dbgc.cbSearchUnit = 1;
2493 pThis->Dbgc.cMaxSearchHits = 1;
2494 //pThis->Dbgc.SearchAddr = {0};
2495 //pThis->Dbgc.cbSearchRange = 0;
2496
2497 //pThis->Dbgc.uInputZero = 0;
2498 //pThis->Dbgc.iRead = 0;
2499 //pThis->Dbgc.iWrite = 0;
2500 //pThis->Dbgc.cInputLines = 0;
2501 //pThis->Dbgc.fInputOverflow = false;
2502 pThis->Dbgc.fReady = true;
2503 pThis->Dbgc.pszScratch = &pThis->Dbgc.achScratch[0];
2504 //pThis->Dbgc.iArg = 0;
2505 //pThis->Dbgc.rcOutput = 0;
2506 //pThis->Dbgc.rcCmd = 0;
2507
2508 //pThis->Dbgc.pszHistoryFile = NULL;
2509 //pThis->Dbgc.pszGlobalInitScript = NULL;
2510 //pThis->Dbgc.pszLocalInitScript = NULL;
2511
2512 dbgcEvalInit();
2513
2514 pThis->fBreakinRecv = false;
2515 pThis->idPktNext = KD_PACKET_HDR_ID_INITIAL;
2516 pThis->pIfWinNt = NULL;
2517 dbgcKdCtxPktRecvReset(pThis);
2518
2519 *ppKdCtx = pThis;
2520 return VINF_SUCCESS;
2521}
2522
2523
2524/**
2525 * Destroys the given KD context.
2526 *
2527 * @returns nothing.
2528 * @param pThis The KD context to destroy.
2529 */
2530static void dbgcKdCtxDestroy(PKDCTX pThis)
2531{
2532 AssertPtr(pThis);
2533
2534 pThis->pIfWinNt = NULL;
2535
2536 /* Detach from the VM. */
2537 if (pThis->Dbgc.pUVM)
2538 DBGFR3Detach(pThis->Dbgc.pUVM);
2539
2540 /* Free config strings. */
2541 RTStrFree(pThis->Dbgc.pszGlobalInitScript);
2542 pThis->Dbgc.pszGlobalInitScript = NULL;
2543 RTStrFree(pThis->Dbgc.pszLocalInitScript);
2544 pThis->Dbgc.pszLocalInitScript = NULL;
2545 RTStrFree(pThis->Dbgc.pszHistoryFile);
2546 pThis->Dbgc.pszHistoryFile = NULL;
2547
2548 /* Finally, free the instance memory. */
2549 RTMemFree(pThis);
2550}
2551
2552
2553DECLHIDDEN(int) dbgcKdStubCreate(PUVM pUVM, PDBGCBACK pBack, unsigned fFlags)
2554{
2555 /*
2556 * Validate input.
2557 */
2558 AssertPtrNullReturn(pUVM, VERR_INVALID_VM_HANDLE);
2559 PVM pVM = NULL;
2560 if (pUVM)
2561 {
2562 pVM = VMR3GetVM(pUVM);
2563 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
2564 }
2565
2566 /*
2567 * Allocate and initialize instance data
2568 */
2569 PKDCTX pThis;
2570 int rc = dbgcKdCtxCreate(&pThis, pBack, fFlags);
2571 if (RT_FAILURE(rc))
2572 return rc;
2573 if (!HMR3IsEnabled(pUVM) && !NEMR3IsEnabled(pUVM))
2574 pThis->Dbgc.hDbgAs = DBGF_AS_RC_AND_GC_GLOBAL;
2575
2576 /*
2577 * Attach to the specified VM.
2578 */
2579 if (RT_SUCCESS(rc) && pUVM)
2580 {
2581 rc = DBGFR3Attach(pUVM);
2582 if (RT_SUCCESS(rc))
2583 {
2584 pThis->Dbgc.pVM = pVM;
2585 pThis->Dbgc.pUVM = pUVM;
2586 pThis->Dbgc.idCpu = 0;
2587
2588 /* Try detecting a Windows NT guest. */
2589 char szName[64];
2590 rc = DBGFR3OSDetect(pUVM, szName, sizeof(szName));
2591 if (RT_SUCCESS(rc))
2592 {
2593 pThis->pIfWinNt = (PDBGFOSIWINNT)DBGFR3OSQueryInterface(pUVM, DBGFOSINTERFACE_WINNT);
2594 if (pThis->pIfWinNt)
2595 LogRel(("DBGC/Kd: Detected Windows NT guest OS (%s)\n", &szName[0]));
2596 else
2597 LogRel(("DBGC/Kd: Detected guest OS is not of the Windows NT kind (%s)\n", &szName[0]));
2598 }
2599 else
2600 {
2601 LogRel(("DBGC/Kd: Unable to detect any guest operating system type, rc=%Rrc\n", rc));
2602 rc = VINF_SUCCESS; /* Try to continue nevertheless. */
2603 }
2604 }
2605 else
2606 rc = pThis->Dbgc.CmdHlp.pfnVBoxError(&pThis->Dbgc.CmdHlp, rc, "When trying to attach to VM %p\n", pThis->Dbgc.pVM);
2607 }
2608
2609 /*
2610 * Load plugins.
2611 */
2612 if (RT_SUCCESS(rc))
2613 {
2614 if (pVM)
2615 DBGFR3PlugInLoadAll(pThis->Dbgc.pUVM);
2616 dbgcEventInit(&pThis->Dbgc);
2617
2618 /*
2619 * Run the debugger main loop.
2620 */
2621 rc = dbgcKdRun(pThis);
2622 dbgcEventTerm(&pThis->Dbgc);
2623 }
2624
2625 /*
2626 * Cleanup console debugger session.
2627 */
2628 dbgcKdCtxDestroy(pThis);
2629 return rc == VERR_DBGC_QUIT ? VINF_SUCCESS : rc;
2630}
2631
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette