Patchwork [v3,34/39] windbg: implement kd_api_get_version

login
register
mail settings
Submitter Mihail Abakumov
Date Dec. 6, 2018, 12:01 p.m.
Message ID <154409770882.5432.1469636471513777468.stgit@Misha-PC.lan02.inno>
Download mbox | patch
Permalink /patch/674011/
State New
Headers show

Comments

Mihail Abakumov - Dec. 6, 2018, 12:01 p.m.
Signed-off-by: Mikhail Abakumov <mikhail.abakumov@ispras.ru>
Signed-off-by: Pavel Dovgalyuk <dovgaluk@ispras.ru>
---
 include/exec/windbgstub-utils.h |    1 +
 target/i386/windbgstub.c        |   61 +++++++++++++++++++++++++++++++++++++++
 windbgstub.c                    |    4 +++
 3 files changed, 66 insertions(+)

Patch

diff --git a/include/exec/windbgstub-utils.h b/include/exec/windbgstub-utils.h
index fa67a7048d..adfc9936f3 100644
--- a/include/exec/windbgstub-utils.h
+++ b/include/exec/windbgstub-utils.h
@@ -80,6 +80,7 @@  void kd_api_read_io_space(CPUState *cs, PacketData *pd);
 void kd_api_write_io_space(CPUState *cs, PacketData *pd);
 void kd_api_read_physical_memory(CPUState *cs, PacketData *pd);
 void kd_api_write_physical_memory(CPUState *cs, PacketData *pd);
+void kd_api_get_version(CPUState *cs, PacketData *pd);
 void kd_api_clear_all_internal_breakpoints(CPUState *cs, PacketData *pd);
 void kd_api_get_context_ex(CPUState *cs, PacketData *pd);
 void kd_api_set_context_ex(CPUState *cs, PacketData *pd);
diff --git a/target/i386/windbgstub.c b/target/i386/windbgstub.c
index 6d2e7de746..9e84027f19 100644
--- a/target/i386/windbgstub.c
+++ b/target/i386/windbgstub.c
@@ -15,6 +15,9 @@ 
 #ifdef TARGET_X86_64
 #define OFFSET_KPCR_SELF 0x18
 #define OFFSET_KPCR_LOCK_ARRAY 0x28
+#define OFFSET_KDBG_LIST 0x0
+#define OFFSET_KDBG_KERNBASE 0x18
+#define OFFSET_KDBG_MODULELIST 0x48
 #define OFFSET_KPRCB 0x20
 #define OFFSET_KPRCB_CURRTHREAD 0x8
 #else  /* TARGET_I386 */
@@ -1080,6 +1083,64 @@  void kd_api_set_context_ex(CPUState *cs, PacketData *pd)
     stl_p(&ctx->BytesCopied, len);
 }
 
+void kd_api_get_version(CPUState *cs, PacketData *pd)
+{
+    DBGKD_GET_VERSION64 *kdver = (DBGKD_GET_VERSION64 *) (PTR(pd->m64) + 0x10);
+#ifdef TARGET_X86_64
+    target_ulong kdbg = kdDebuggerDataBlock.addr;
+    target_ulong dDataList = VMEM_ADDR(cs, kdbg + OFFSET_KDBG_LIST);
+    target_ulong kernbase = VMEM_ADDR(cs, kdbg + OFFSET_KDBG_KERNBASE);
+    target_ulong modules = VMEM_ADDR(cs, kdbg + OFFSET_KDBG_MODULELIST);
+
+    /* TODO: Fix this hardcoded value.
+     * Receives 0xF if the target's operating system is a free build,
+     * and 0xC if it is a checked build.
+     */
+    stw_p(&kdver->MajorVersion, 0xF);
+    /* TODO: Fix this hardcoded value. Needs NtBuildNumber. How to get it? */
+    stw_p(&kdver->MinorVersion, 0x1db1);
+    stb_p(&kdver->ProtocolVersion, DBGKD_64BIT_PROTOCOL_VERSION2);
+    /* TODO: Fix this hardcoded value. */
+    stb_p(&kdver->KdSecondaryVersion, 0);
+    stw_p(&kdver->Flags,
+          DBGKD_VERS_FLAG_MP | DBGKD_VERS_FLAG_DATA | DBGKD_VERS_FLAG_PTR64);
+    stw_p(&kdver->MachineType, IMAGE_FILE_MACHINE_AMD64);
+    stb_p(&kdver->MaxPacketType, PACKET_TYPE_MAX);
+    stb_p(&kdver->MaxStateChange,
+          DbgKdMaximumStateChange - DbgKdMinimumStateChange);
+    stb_p(&kdver->MaxManipulate,
+          DbgKdMaximumManipulate - DbgKdMinimumManipulate);
+    /* FIXME: Maybe DBGKD_SIMULATION_EXDI? */
+    stb_p(&kdver->Simulation, DBGKD_SIMULATION_NONE);
+    stw_p(&kdver->Unused[0], 0);
+    sttul_p(&kdver->KernBase, kernbase);
+    sttul_p(&kdver->PsLoadedModuleList, modules);
+    sttul_p(&kdver->DebuggerDataList, dDataList);
+#else /* TARGET_I386 */
+    int err = cpu_memory_rw_debug(cs, kdVersion.addr, (uint8_t *) kdver,
+                                  sizeof(DBGKD_MANIPULATE_STATE64) - 0x10, 0);
+    if (!err) {
+        stw_p(&kdver->MajorVersion, kdver->MajorVersion);
+        stw_p(&kdver->MinorVersion, kdver->MinorVersion);
+        stb_p(&kdver->ProtocolVersion, kdver->ProtocolVersion);
+        stb_p(&kdver->KdSecondaryVersion, kdver->KdSecondaryVersion);
+        stw_p(&kdver->Flags, kdver->Flags);
+        stw_p(&kdver->MachineType, kdver->MachineType);
+        stb_p(&kdver->MaxPacketType, kdver->MaxPacketType);
+        stb_p(&kdver->MaxStateChange, kdver->MaxStateChange);
+        stb_p(&kdver->MaxManipulate, kdver->MaxManipulate);
+        stb_p(&kdver->Simulation, kdver->Simulation);
+        stw_p(&kdver->Unused[0], kdver->Unused[0]);
+        sttul_p(&kdver->KernBase, kdver->KernBase);
+        sttul_p(&kdver->PsLoadedModuleList, kdver->PsLoadedModuleList);
+        sttul_p(&kdver->DebuggerDataList, kdver->DebuggerDataList);
+    } else {
+        pd->m64.ReturnStatus = STATUS_UNSUCCESSFUL;
+        WINDBG_ERROR("get_version: " FMT_ERR, err);
+    }
+#endif /* TARGET_I386 */
+}
+
 static bool find_KPCR(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
diff --git a/windbgstub.c b/windbgstub.c
index 25331f218f..366856bc81 100644
--- a/windbgstub.c
+++ b/windbgstub.c
@@ -212,6 +212,10 @@  static void windbg_process_manipulate_packet(WindbgState *state)
         kd_api_write_physical_memory(cs, data);
         break;
 
+    case DbgKdGetVersionApi:
+        kd_api_get_version(cs, data);
+        break;
+
     case DbgKdClearAllInternalBreakpointsApi:
         kd_api_clear_all_internal_breakpoints(cs, data);
         return;