Patchwork [v3,09/39] windbg: implement find_KPCR

login
register
mail settings
Submitter Mihail Abakumov
Date Dec. 6, 2018, 11:59 a.m.
Message ID <154409756906.5432.13998918963086811049.stgit@Misha-PC.lan02.inno>
Download mbox | patch
Permalink /patch/673975/
State New
Headers show

Comments

Mihail Abakumov - Dec. 6, 2018, 11:59 a.m.
Signed-off-by: Mikhail Abakumov <mikhail.abakumov@ispras.ru>
Signed-off-by: Pavel Dovgalyuk <dovgaluk@ispras.ru>
---
 target/i386/windbgstub.c |   32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Patch

diff --git a/target/i386/windbgstub.c b/target/i386/windbgstub.c
index 1c1631d7c1..15a90e521c 100644
--- a/target/i386/windbgstub.c
+++ b/target/i386/windbgstub.c
@@ -12,6 +12,18 @@ 
 #include "qemu/osdep.h"
 #include "exec/windbgstub-utils.h"
 
+#ifdef TARGET_X86_64
+#define OFFSET_KPCR_SELF 0x18
+#else  /* TARGET_I386 */
+#define OFFSET_KPCR_SELF 0x1C
+#endif /* TARGET_I386 */
+
+#ifdef TARGET_X86_64
+#define TARGET_SAFE(i386_obj, x86_64_obj) x86_64_obj
+#else  /* TARGET_I386 */
+#define TARGET_SAFE(i386_obj, x86_64_obj) i386_obj
+#endif /* TARGET_I386 */
+
 static InitedAddr KPCR;
 #ifdef TARGET_X86_64
 static InitedAddr kdDebuggerDataBlock;
@@ -21,6 +33,26 @@  static InitedAddr kdVersion;
 
 static bool find_KPCR(CPUState *cs)
 {
+    X86CPU *cpu = X86_CPU(cs);
+    CPUX86State *env = &cpu->env;
+
+    if (!KPCR.is_init) {
+        KPCR.addr = env->segs[TARGET_SAFE(R_FS, R_GS)].base;
+
+        static target_ulong prev_KPCR;
+        if (!KPCR.addr || prev_KPCR == KPCR.addr) {
+            return false;
+        }
+        prev_KPCR = KPCR.addr;
+
+        if (KPCR.addr != VMEM_ADDR(cs, KPCR.addr + OFFSET_KPCR_SELF)) {
+            return false;
+        }
+        KPCR.is_init = true;
+
+        DPRINTF("find KPCR " FMT_ADDR "\n", KPCR.addr);
+    }
+
     return KPCR.is_init;
 }