Skip to contentSkip to conversation

dump: add kernel_gs_base to QEMU CPU state

PATCH Superseded Viktor Prutyanov 3 participants 6 messages Jul 10, 2018 – Jul 11, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dump: add kernel_gs_base to QEMU CPU state

This patch adds field with content of KERNEL_GS_BASE MSR to QEMU note in ELF dump.

On Windows, if all vCPUs are running usermode tasks at the time the dump is created, this can be helpful in the discovery of guest system structures during conversion ELF dump to MEMORY.DMP dump.

---
 target/i386/arch_dump.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/i386/arch_dump.c +4 -1
index 35b55fc..a702138 100644
@@ -237,7 +237,7 @@ int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
  * please count up QEMUCPUSTATE_VERSION if you have changed definition of
  * QEMUCPUState, and modify the tools using this information accordingly.
  */
-#define QEMUCPUSTATE_VERSION (1)
+#define QEMUCPUSTATE_VERSION (2)
 
 struct QEMUCPUSegment {
     uint32_t selector;
@@ -258,6 +258,7 @@ struct QEMUCPUState {
     QEMUCPUSegment cs, ds, es, fs, gs, ss;
     QEMUCPUSegment ldt, tr, gdt, idt;
     uint64_t cr[5];
+    uint64_t kernel_gs_base;
 };
 
 typedef struct QEMUCPUState QEMUCPUState;
@@ -315,6 +316,8 @@ static void qemu_get_cpustate(QEMUCPUState *s, CPUX86State *env)
     s->cr[2] = env->cr[2];
     s->cr[3] = env->cr[3];
     s->cr[4] = env->cr[4];
+
+    s->kernel_gs_base = env->kernelgsbase;
 }
 
 static inline int cpu_write_qemu_note(WriteCoreDumpFunction f,
Signature · 1 line
2.7.4
#1 lore raw
Quoted 17 lines · Viktor Prutyanov

This patch adds field with content of KERNEL_GS_BASE MSR to QEMU note in ELF dump.

On Windows, if all vCPUs are running usermode tasks at the time the dump is created, this can be helpful in the discovery of guest system structures during conversion ELF dump to MEMORY.DMP dump.

---
 target/i386/arch_dump.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/i386/arch_dump.c +0 -0
index 35b55fc..a702138 100644

target/i386/arch_dump.c int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, · line 238

Quoted 2 lines
target/i386/arch_dump.c +0 -0
@@ -237,7 +237,7 @@ int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
  * please count up QEMUCPUSTATE_VERSION if you have changed definition of
  * QEMUCPUState, and modify the tools using this information accordingly.

Where are the tools using this information, that need to be updated? Won't this break existing versions of those tools?

Is the dump format and pointers to available tools documented somewhere?

Quoted 26 lines
target/i386/arch_dump.c +4 -1
  */
-#define QEMUCPUSTATE_VERSION (1)
+#define QEMUCPUSTATE_VERSION (2)
 
 struct QEMUCPUSegment {
     uint32_t selector;
@@ -258,6 +258,7 @@ struct QEMUCPUState {
     QEMUCPUSegment cs, ds, es, fs, gs, ss;
     QEMUCPUSegment ldt, tr, gdt, idt;
     uint64_t cr[5];
+    uint64_t kernel_gs_base;
 };
 
 typedef struct QEMUCPUState QEMUCPUState;
@@ -315,6 +316,8 @@ static void qemu_get_cpustate(QEMUCPUState *s, CPUX86State *env)
     s->cr[2] = env->cr[2];
     s->cr[3] = env->cr[3];
     s->cr[4] = env->cr[4];
+
+    s->kernel_gs_base = env->kernelgsbase;
 }
 
 static inline int cpu_write_qemu_note(WriteCoreDumpFunction f,
Signature · 1 line
2.7.4
Signature · 1 line
Eduardo
#2 lore raw
Quoted 5 lines · Eduardo Habkost

target/i386/arch_dump.c int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, · line 238

Quoted 3 lines
target/i386/arch_dump.c +0 -0
@@ -237,7 +237,7 @@ int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
  * please count up QEMUCPUSTATE_VERSION if you have changed definition of
  * QEMUCPUState, and modify the tools using this information accordingly.

Where are the tools using this information, that need to be updated? Won't this break existing versions of those tools?

I think it's okay to _not_ change the version, since the format is backwards-compatible. Each QEMUCPUState struct is in a separate ELF note, and the presence of the new field is visible in both 1) the size of the note 2) the size field of the struct.

Another possibility is to stash kernel_gs_base in cr[1]. This approach doesn't scale, but the word is otherwise unused if we want to make it super safe. I don't recommend it.

Paolo

Quoted 4 lines

Is the dump format and pointers to available tools documented somewhere?

#3 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 5 lines · Eduardo Habkost

target/i386/arch_dump.c int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, · line 238

Quoted 3 lines
target/i386/arch_dump.c +0 -0
@@ -237,7 +237,7 @@ int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
  * please count up QEMUCPUSTATE_VERSION if you have changed definition of
  * QEMUCPUState, and modify the tools using this information accordingly.

Where are the tools using this information, that need to be updated? Won't this break existing versions of those tools?

I think it's okay to _not_ change the version, since the format is backwards-compatible. Each QEMUCPUState struct is in a separate ELF note, and the presence of the new field is visible in both 1) the size of the note 2) the size field of the struct.

If we do it, can we please make the documentation (or at least code comments, as documentation doesn't seem to exist) clearly state that the new field is optional in version 1?

Signature · 1 line
Eduardo
#4 lore raw
In reply to Eduardo Habkost
Quoted 27 lines · On Wed, 11 Jul 2018 13:00:25 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 19 lines · Viktor Prutyanov

This patch adds field with content of KERNEL_GS_BASE MSR to QEMU note in ELF dump.

On Windows, if all vCPUs are running usermode tasks at the time the dump is created, this can be helpful in the discovery of guest system structures during conversion ELF dump to MEMORY.DMP dump.

---
 target/i386/arch_dump.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/i386/arch_dump.c +0 -0
index 35b55fc..a702138 100644
@@ -237,7 +237,7 @@ int
 x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
  * please count up QEMUCPUSTATE_VERSION if you have changed
Quoted 3 lines

definition of
* QEMUCPUState, and modify the tools using this information
accordingly.

Where are the tools using this information, that need to be updated? Won't this break existing versions of those tools?

Quoted 2 lines

Is the dump format and pointers to available tools documented somewhere?

I hope that someone from community knows about those tools because I can't find such tools.

Quoted 28 lines
Quoted 27 lines

*/

target/i386/arch_dump.c +4 -1
-#define QEMUCPUSTATE_VERSION (1)
+#define QEMUCPUSTATE_VERSION (2)
 
 struct QEMUCPUSegment {
     uint32_t selector;
@@ -258,6 +258,7 @@ struct QEMUCPUState {
     QEMUCPUSegment cs, ds, es, fs, gs, ss;
     QEMUCPUSegment ldt, tr, gdt, idt;
     uint64_t cr[5];
+    uint64_t kernel_gs_base;
 };
 
 typedef struct QEMUCPUState QEMUCPUState;
@@ -315,6 +316,8 @@ static void qemu_get_cpustate(QEMUCPUState *s,
 CPUX86State *env) s->cr[2] = env->cr[2];
     s->cr[3] = env->cr[3];
     s->cr[4] = env->cr[4];
+
+    s->kernel_gs_base = env->kernelgsbase;
 }
 
 static inline int cpu_write_qemu_note(WriteCoreDumpFunction f,
Signature · 1 line
2.7.4
#5 lore raw
Quoted 8 lines · Viktor Prutyanov
Quoted 5 lines

Where are the tools using this information, that need to be updated? Won't this break existing versions of those tools?

Is the dump format and pointers to available tools documented somewhere?

I hope that someone from community knows about those tools because I can't find such tools.

I checked crash, and it doesn't have any issue with QEMUCPUState that is bigger than what is currently in QEMU. It doesn't use anything but CR3 and IDT base. It also doesn't at all check the version! :O

Paolo