Patchwork [v3,27/39] windbg: implement kd_api_get_context_ex and kd_api_set_context_ex

login
register
mail settings
Submitter Mihail Abakumov
Date Dec. 6, 2018, 12:01 p.m.
Message ID <154409766974.5432.4221263895070915455.stgit@Misha-PC.lan02.inno>
Download mbox | patch
Permalink /patch/673993/
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 |    2 +
 target/i386/windbgstub.c        |   90 ++++++++++++++++++++++++++++++++++++---
 windbgstub.c                    |    8 +++
 3 files changed, 93 insertions(+), 7 deletions(-)

Patch

diff --git a/include/exec/windbgstub-utils.h b/include/exec/windbgstub-utils.h
index a88e013de9..6936fd0ffb 100644
--- a/include/exec/windbgstub-utils.h
+++ b/include/exec/windbgstub-utils.h
@@ -71,6 +71,8 @@  void kd_api_read_virtual_memory(CPUState *cs, PacketData *pd);
 void kd_api_write_virtual_memory(CPUState *cs, PacketData *pd);
 void kd_api_get_context(CPUState *cs, PacketData *pd);
 void kd_api_set_context(CPUState *cs, PacketData *pd);
+void kd_api_get_context_ex(CPUState *cs, PacketData *pd);
+void kd_api_set_context_ex(CPUState *cs, PacketData *pd);
 void kd_api_unsupported(CPUState *cs, PacketData *pd);
 
 DBGKD_ANY_WAIT_STATE_CHANGE *kd_state_change_exc(CPUState *cs);
diff --git a/target/i386/windbgstub.c b/target/i386/windbgstub.c
index 900950495f..76cdc1d9a7 100644
--- a/target/i386/windbgstub.c
+++ b/target/i386/windbgstub.c
@@ -884,18 +884,57 @@  static int fun_name(CPUState *cs, uint8_t *buf, int buf_size,                  \
     return 0;                                                                  \
 }
 
-__attribute__ ((unused)) /* unused yet */
 GEN_WINDBG_CONTEXT_RW(windbg_read_context, false)
-
-__attribute__ ((unused)) /* unused yet */
 GEN_WINDBG_CONTEXT_RW(windbg_write_context, true)
-
-__attribute__ ((unused)) /* unused yet */
 GEN_WINDBG_KSPEC_REGS_RW(windbg_read_ks_regs, false)
-
-__attribute__ ((unused)) /* unused yet */
 GEN_WINDBG_KSPEC_REGS_RW(windbg_write_ks_regs, true)
 
+static int windbg_rw_context_ex(CPUState *cs, uint8_t *buf, int buf_size,
+                                int offset, int len, bool is_read)
+{
+    int context_len;
+    int ks_regs_len;
+    int err = -1;
+
+    if (offset < sizeof(CPU_KPROCESSOR_STATE)) {
+        len = MIN(len, sizeof(CPU_KPROCESSOR_STATE) - offset);
+
+        context_len = MAX(0, (int) (sizeof(CPU_CONTEXT) - offset));
+        ks_regs_len = len - context_len;
+
+        if (context_len > 0) {
+            if (is_read) {
+                err = windbg_read_context(cs, buf, context_len, offset,
+                                          context_len);
+            } else {
+                err = windbg_write_context(cs, buf, context_len, offset,
+                                           context_len);
+            }
+
+            if (err) {
+                return err;
+            }
+        }
+
+        if (ks_regs_len > 0) {
+            offset += context_len - sizeof(CPU_CONTEXT);
+            if (is_read) {
+                err = windbg_read_ks_regs(cs, buf + context_len, ks_regs_len,
+                                          offset, ks_regs_len);
+            } else {
+                err = windbg_write_ks_regs(cs, buf + context_len, ks_regs_len,
+                                           offset, ks_regs_len);
+            }
+
+            if (err) {
+                return err;
+            }
+        }
+    }
+
+    return err;
+}
+
 void kd_api_get_context(CPUState *cs, PacketData *pd)
 {
     int err;
@@ -923,6 +962,43 @@  void kd_api_set_context(CPUState *cs, PacketData *pd)
     }
 }
 
+void kd_api_get_context_ex(CPUState *cs, PacketData *pd)
+{
+    DBGKD_CONTEXT_EX *ctx = &pd->m64.u.ContextEx;
+    uint32_t offset = ldl_p(&ctx->Offset);
+    uint32_t len = MIN(ldl_p(&ctx->ByteCount),
+                       PACKET_MAX_SIZE - sizeof(DBGKD_MANIPULATE_STATE64));
+    int err;
+
+    err = windbg_rw_context_ex(cs, pd->extra, len, offset, len, true);
+
+    if (err) {
+        len = 0;
+        pd->m64.ReturnStatus = STATUS_UNSUCCESSFUL;
+    }
+
+    pd->extra_size = len;
+    stl_p(&ctx->BytesCopied, len);
+}
+
+void kd_api_set_context_ex(CPUState *cs, PacketData *pd)
+{
+    DBGKD_CONTEXT_EX *ctx = &pd->m64.u.ContextEx;
+    uint32_t offset = ldl_p(&ctx->Offset);
+    uint32_t len = MIN(ldl_p(&ctx->ByteCount), pd->extra_size);
+    int err;
+
+    err = windbg_rw_context_ex(cs, pd->extra, len, offset, len, false);
+
+    if (err) {
+        len = 0;
+        pd->m64.ReturnStatus = STATUS_UNSUCCESSFUL;
+    }
+
+    pd->extra_size = 0;
+    stl_p(&ctx->BytesCopied, len);
+}
+
 static bool find_KPCR(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
diff --git a/windbgstub.c b/windbgstub.c
index 70ebf3c0ad..e9d759cddf 100644
--- a/windbgstub.c
+++ b/windbgstub.c
@@ -165,6 +165,14 @@  static void windbg_process_manipulate_packet(WindbgState *state)
         kd_api_set_context(cs, data);
         break;
 
+    case DbgKdGetContextExApi:
+        kd_api_get_context_ex(cs, data);
+        break;
+
+    case DbgKdSetContextExApi:
+        kd_api_set_context_ex(cs, data);
+        break;
+
     default:
         kd_api_unsupported(cs, data);
         break;