4 * Copyright Fujitsu, Corp. 2011, 2012
7 * Wen Congyang <wency@cn.fujitsu.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
15 #include "exec/cpu-all.h"
16 #include "sysemu/dump.h"
18 #include "sysemu/memory_mapping.h"
22 target_ulong r15
, r14
, r13
, r12
, rbp
, rbx
, r11
, r10
;
23 target_ulong r9
, r8
, rax
, rcx
, rdx
, rsi
, rdi
, orig_rax
;
24 target_ulong rip
, cs
, eflags
;
26 target_ulong fs_base
, gs_base
;
27 target_ulong ds
, es
, fs
, gs
;
28 } x86_64_user_regs_struct
;
34 x86_64_user_regs_struct regs
;
36 } x86_64_elf_prstatus
;
38 static int x86_64_write_elf64_note(WriteCoreDumpFunction f
,
39 CPUX86State
*env
, int id
,
42 x86_64_user_regs_struct regs
;
45 int descsz
, note_size
, name_size
= 5;
46 const char *name
= "CORE";
49 regs
.r15
= env
->regs
[15];
50 regs
.r14
= env
->regs
[14];
51 regs
.r13
= env
->regs
[13];
52 regs
.r12
= env
->regs
[12];
53 regs
.r11
= env
->regs
[11];
54 regs
.r10
= env
->regs
[10];
55 regs
.r9
= env
->regs
[9];
56 regs
.r8
= env
->regs
[8];
57 regs
.rbp
= env
->regs
[R_EBP
];
58 regs
.rsp
= env
->regs
[R_ESP
];
59 regs
.rdi
= env
->regs
[R_EDI
];
60 regs
.rsi
= env
->regs
[R_ESI
];
61 regs
.rdx
= env
->regs
[R_EDX
];
62 regs
.rcx
= env
->regs
[R_ECX
];
63 regs
.rbx
= env
->regs
[R_EBX
];
64 regs
.rax
= env
->regs
[R_EAX
];
66 regs
.eflags
= env
->eflags
;
68 regs
.orig_rax
= 0; /* FIXME */
69 regs
.cs
= env
->segs
[R_CS
].selector
;
70 regs
.ss
= env
->segs
[R_SS
].selector
;
71 regs
.fs_base
= env
->segs
[R_FS
].base
;
72 regs
.gs_base
= env
->segs
[R_GS
].base
;
73 regs
.ds
= env
->segs
[R_DS
].selector
;
74 regs
.es
= env
->segs
[R_ES
].selector
;
75 regs
.fs
= env
->segs
[R_FS
].selector
;
76 regs
.gs
= env
->segs
[R_GS
].selector
;
78 descsz
= sizeof(x86_64_elf_prstatus
);
79 note_size
= ((sizeof(Elf64_Nhdr
) + 3) / 4 + (name_size
+ 3) / 4 +
80 (descsz
+ 3) / 4) * 4;
81 note
= g_malloc(note_size
);
83 memset(note
, 0, note_size
);
84 note
->n_namesz
= cpu_to_le32(name_size
);
85 note
->n_descsz
= cpu_to_le32(descsz
);
86 note
->n_type
= cpu_to_le32(NT_PRSTATUS
);
88 buf
+= ((sizeof(Elf64_Nhdr
) + 3) / 4) * 4;
89 memcpy(buf
, name
, name_size
);
90 buf
+= ((name_size
+ 3) / 4) * 4;
91 memcpy(buf
+ 32, &id
, 4); /* pr_pid */
92 buf
+= descsz
- sizeof(x86_64_user_regs_struct
)-sizeof(target_ulong
);
93 memcpy(buf
, ®s
, sizeof(x86_64_user_regs_struct
));
95 ret
= f(note
, note_size
, opaque
);
106 uint32_t ebx
, ecx
, edx
, esi
, edi
, ebp
, eax
;
107 unsigned short ds
, __ds
, es
, __es
;
108 unsigned short fs
, __fs
, gs
, __gs
;
109 uint32_t orig_eax
, eip
;
110 unsigned short cs
, __cs
;
111 uint32_t eflags
, esp
;
112 unsigned short ss
, __ss
;
113 } x86_user_regs_struct
;
119 x86_user_regs_struct regs
;
123 static void x86_fill_elf_prstatus(x86_elf_prstatus
*prstatus
, CPUX86State
*env
,
126 memset(prstatus
, 0, sizeof(x86_elf_prstatus
));
127 prstatus
->regs
.ebp
= env
->regs
[R_EBP
] & 0xffffffff;
128 prstatus
->regs
.esp
= env
->regs
[R_ESP
] & 0xffffffff;
129 prstatus
->regs
.edi
= env
->regs
[R_EDI
] & 0xffffffff;
130 prstatus
->regs
.esi
= env
->regs
[R_ESI
] & 0xffffffff;
131 prstatus
->regs
.edx
= env
->regs
[R_EDX
] & 0xffffffff;
132 prstatus
->regs
.ecx
= env
->regs
[R_ECX
] & 0xffffffff;
133 prstatus
->regs
.ebx
= env
->regs
[R_EBX
] & 0xffffffff;
134 prstatus
->regs
.eax
= env
->regs
[R_EAX
] & 0xffffffff;
135 prstatus
->regs
.eip
= env
->eip
& 0xffffffff;
136 prstatus
->regs
.eflags
= env
->eflags
& 0xffffffff;
138 prstatus
->regs
.cs
= env
->segs
[R_CS
].selector
;
139 prstatus
->regs
.ss
= env
->segs
[R_SS
].selector
;
140 prstatus
->regs
.ds
= env
->segs
[R_DS
].selector
;
141 prstatus
->regs
.es
= env
->segs
[R_ES
].selector
;
142 prstatus
->regs
.fs
= env
->segs
[R_FS
].selector
;
143 prstatus
->regs
.gs
= env
->segs
[R_GS
].selector
;
148 static int x86_write_elf64_note(WriteCoreDumpFunction f
, CPUX86State
*env
,
149 int id
, void *opaque
)
151 x86_elf_prstatus prstatus
;
154 int descsz
, note_size
, name_size
= 5;
155 const char *name
= "CORE";
158 x86_fill_elf_prstatus(&prstatus
, env
, id
);
159 descsz
= sizeof(x86_elf_prstatus
);
160 note_size
= ((sizeof(Elf64_Nhdr
) + 3) / 4 + (name_size
+ 3) / 4 +
161 (descsz
+ 3) / 4) * 4;
162 note
= g_malloc(note_size
);
164 memset(note
, 0, note_size
);
165 note
->n_namesz
= cpu_to_le32(name_size
);
166 note
->n_descsz
= cpu_to_le32(descsz
);
167 note
->n_type
= cpu_to_le32(NT_PRSTATUS
);
169 buf
+= ((sizeof(Elf64_Nhdr
) + 3) / 4) * 4;
170 memcpy(buf
, name
, name_size
);
171 buf
+= ((name_size
+ 3) / 4) * 4;
172 memcpy(buf
, &prstatus
, sizeof(prstatus
));
174 ret
= f(note
, note_size
, opaque
);
183 int x86_cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cs
,
184 int cpuid
, void *opaque
)
186 X86CPU
*cpu
= X86_CPU(cs
);
189 X86CPU
*first_x86_cpu
= X86_CPU(first_cpu
);
190 bool lma
= !!(first_x86_cpu
->env
.hflags
& HF_LMA_MASK
);
193 ret
= x86_64_write_elf64_note(f
, &cpu
->env
, cpuid
, opaque
);
196 ret
= x86_write_elf64_note(f
, &cpu
->env
, cpuid
, opaque
);
204 int x86_cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cs
,
205 int cpuid
, void *opaque
)
207 X86CPU
*cpu
= X86_CPU(cs
);
208 x86_elf_prstatus prstatus
;
211 int descsz
, note_size
, name_size
= 5;
212 const char *name
= "CORE";
215 x86_fill_elf_prstatus(&prstatus
, &cpu
->env
, cpuid
);
216 descsz
= sizeof(x86_elf_prstatus
);
217 note_size
= ((sizeof(Elf32_Nhdr
) + 3) / 4 + (name_size
+ 3) / 4 +
218 (descsz
+ 3) / 4) * 4;
219 note
= g_malloc(note_size
);
221 memset(note
, 0, note_size
);
222 note
->n_namesz
= cpu_to_le32(name_size
);
223 note
->n_descsz
= cpu_to_le32(descsz
);
224 note
->n_type
= cpu_to_le32(NT_PRSTATUS
);
226 buf
+= ((sizeof(Elf32_Nhdr
) + 3) / 4) * 4;
227 memcpy(buf
, name
, name_size
);
228 buf
+= ((name_size
+ 3) / 4) * 4;
229 memcpy(buf
, &prstatus
, sizeof(prstatus
));
231 ret
= f(note
, note_size
, opaque
);
241 * please count up QEMUCPUSTATE_VERSION if you have changed definition of
242 * QEMUCPUState, and modify the tools using this information accordingly.
244 #define QEMUCPUSTATE_VERSION (1)
246 struct QEMUCPUSegment
{
254 typedef struct QEMUCPUSegment QEMUCPUSegment
;
256 struct QEMUCPUState
{
259 uint64_t rax
, rbx
, rcx
, rdx
, rsi
, rdi
, rsp
, rbp
;
260 uint64_t r8
, r9
, r10
, r11
, r12
, r13
, r14
, r15
;
261 uint64_t rip
, rflags
;
262 QEMUCPUSegment cs
, ds
, es
, fs
, gs
, ss
;
263 QEMUCPUSegment ldt
, tr
, gdt
, idt
;
267 typedef struct QEMUCPUState QEMUCPUState
;
269 static void copy_segment(QEMUCPUSegment
*d
, SegmentCache
*s
)
272 d
->selector
= s
->selector
;
278 static void qemu_get_cpustate(QEMUCPUState
*s
, CPUX86State
*env
)
280 memset(s
, 0, sizeof(QEMUCPUState
));
282 s
->version
= QEMUCPUSTATE_VERSION
;
283 s
->size
= sizeof(QEMUCPUState
);
285 s
->rax
= env
->regs
[R_EAX
];
286 s
->rbx
= env
->regs
[R_EBX
];
287 s
->rcx
= env
->regs
[R_ECX
];
288 s
->rdx
= env
->regs
[R_EDX
];
289 s
->rsi
= env
->regs
[R_ESI
];
290 s
->rdi
= env
->regs
[R_EDI
];
291 s
->rsp
= env
->regs
[R_ESP
];
292 s
->rbp
= env
->regs
[R_EBP
];
294 s
->r8
= env
->regs
[8];
295 s
->r9
= env
->regs
[9];
296 s
->r10
= env
->regs
[10];
297 s
->r11
= env
->regs
[11];
298 s
->r12
= env
->regs
[12];
299 s
->r13
= env
->regs
[13];
300 s
->r14
= env
->regs
[14];
301 s
->r15
= env
->regs
[15];
304 s
->rflags
= env
->eflags
;
306 copy_segment(&s
->cs
, &env
->segs
[R_CS
]);
307 copy_segment(&s
->ds
, &env
->segs
[R_DS
]);
308 copy_segment(&s
->es
, &env
->segs
[R_ES
]);
309 copy_segment(&s
->fs
, &env
->segs
[R_FS
]);
310 copy_segment(&s
->gs
, &env
->segs
[R_GS
]);
311 copy_segment(&s
->ss
, &env
->segs
[R_SS
]);
312 copy_segment(&s
->ldt
, &env
->ldt
);
313 copy_segment(&s
->tr
, &env
->tr
);
314 copy_segment(&s
->gdt
, &env
->gdt
);
315 copy_segment(&s
->idt
, &env
->idt
);
317 s
->cr
[0] = env
->cr
[0];
318 s
->cr
[1] = env
->cr
[1];
319 s
->cr
[2] = env
->cr
[2];
320 s
->cr
[3] = env
->cr
[3];
321 s
->cr
[4] = env
->cr
[4];
324 static inline int cpu_write_qemu_note(WriteCoreDumpFunction f
,
334 int descsz
, note_size
, name_size
= 5, note_head_size
;
335 const char *name
= "QEMU";
338 qemu_get_cpustate(&state
, env
);
340 descsz
= sizeof(state
);
342 note_head_size
= sizeof(Elf32_Nhdr
);
344 note_head_size
= sizeof(Elf64_Nhdr
);
346 note_size
= ((note_head_size
+ 3) / 4 + (name_size
+ 3) / 4 +
347 (descsz
+ 3) / 4) * 4;
348 note
= g_malloc(note_size
);
350 memset(note
, 0, note_size
);
353 note32
->n_namesz
= cpu_to_le32(name_size
);
354 note32
->n_descsz
= cpu_to_le32(descsz
);
358 note64
->n_namesz
= cpu_to_le32(name_size
);
359 note64
->n_descsz
= cpu_to_le32(descsz
);
363 buf
+= ((note_head_size
+ 3) / 4) * 4;
364 memcpy(buf
, name
, name_size
);
365 buf
+= ((name_size
+ 3) / 4) * 4;
366 memcpy(buf
, &state
, sizeof(state
));
368 ret
= f(note
, note_size
, opaque
);
377 int x86_cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cs
,
380 X86CPU
*cpu
= X86_CPU(cs
);
382 return cpu_write_qemu_note(f
, &cpu
->env
, opaque
, 1);
385 int x86_cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cs
,
388 X86CPU
*cpu
= X86_CPU(cs
);
390 return cpu_write_qemu_note(f
, &cpu
->env
, opaque
, 0);
393 int cpu_get_dump_info(ArchDumpInfo
*info
,
394 const GuestPhysBlockList
*guest_phys_blocks
)
397 GuestPhysBlock
*block
;
400 X86CPU
*first_x86_cpu
= X86_CPU(first_cpu
);
402 lma
= !!(first_x86_cpu
->env
.hflags
& HF_LMA_MASK
);
406 info
->d_machine
= EM_X86_64
;
408 info
->d_machine
= EM_386
;
410 info
->d_endian
= ELFDATA2LSB
;
413 info
->d_class
= ELFCLASS64
;
415 info
->d_class
= ELFCLASS32
;
417 QTAILQ_FOREACH(block
, &guest_phys_blocks
->head
, next
) {
418 if (block
->target_end
> UINT_MAX
) {
419 /* The memory size is greater than 4G */
420 info
->d_class
= ELFCLASS64
;
429 ssize_t
cpu_get_note_size(int class, int machine
, int nr_cpus
)
431 int name_size
= 5; /* "CORE" or "QEMU" */
432 size_t elf_note_size
= 0;
433 size_t qemu_note_size
= 0;
434 int elf_desc_size
= 0;
435 int qemu_desc_size
= 0;
438 if (class == ELFCLASS32
) {
439 note_head_size
= sizeof(Elf32_Nhdr
);
441 note_head_size
= sizeof(Elf64_Nhdr
);
444 if (machine
== EM_386
) {
445 elf_desc_size
= sizeof(x86_elf_prstatus
);
449 elf_desc_size
= sizeof(x86_64_elf_prstatus
);
452 qemu_desc_size
= sizeof(QEMUCPUState
);
454 elf_note_size
= ((note_head_size
+ 3) / 4 + (name_size
+ 3) / 4 +
455 (elf_desc_size
+ 3) / 4) * 4;
456 qemu_note_size
= ((note_head_size
+ 3) / 4 + (name_size
+ 3) / 4 +
457 (qemu_desc_size
+ 3) / 4) * 4;
459 return (elf_note_size
+ qemu_note_size
) * nr_cpus
;