2 * writing ELF notes for ppc{64,} arch
5 * Copyright IBM, Corp. 2013
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
18 #include "sysemu/dump.h"
19 #include "sysemu/kvm.h"
20 #include "exec/helper-proto.h"
23 #define ELFCLASS ELFCLASS64
24 #define cpu_to_dump_reg cpu_to_dump64
25 typedef uint64_t reg_t;
26 typedef Elf64_Nhdr Elf_Nhdr;
28 #define ELFCLASS ELFCLASS32
29 #define cpu_to_dump_reg cpu_to_dump32
30 typedef uint32_t reg_t;
31 typedef Elf32_Nhdr Elf_Nhdr;
32 #endif /* TARGET_PPC64 */
34 struct PPCUserRegStruct {
50 struct PPCElfPrstatus {
52 struct PPCUserRegStruct pr_reg;
57 struct PPCElfFpregset {
63 struct PPCElfVmxregset {
72 struct PPCElfVsxregset {
76 struct PPCElfSperegset {
82 typedef struct noteStruct {
87 struct PPCElfPrstatus prstatus;
88 struct PPCElfFpregset fpregset;
89 struct PPCElfVmxregset vmxregset;
90 struct PPCElfVsxregset vsxregset;
91 struct PPCElfSperegset speregset;
95 typedef struct NoteFuncArg {
100 static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
104 struct PPCElfPrstatus *prstatus;
105 struct PPCUserRegStruct *reg;
106 Note *note = &arg->note;
107 DumpState *s = arg->state;
109 note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS);
111 prstatus = ¬e->contents.prstatus;
112 memset(prstatus, 0, sizeof(*prstatus));
113 reg = &prstatus->pr_reg;
115 for (i = 0; i < 32; i++) {
116 reg->gpr[i] = cpu_to_dump_reg(s, cpu->env.gpr[i]);
118 reg->nip = cpu_to_dump_reg(s, cpu->env.nip);
119 reg->msr = cpu_to_dump_reg(s, cpu->env.msr);
120 reg->ctr = cpu_to_dump_reg(s, cpu->env.ctr);
121 reg->link = cpu_to_dump_reg(s, cpu->env.lr);
122 reg->xer = cpu_to_dump_reg(s, cpu_read_xer(&cpu->env));
125 for (i = 0; i < 8; i++) {
126 cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
128 reg->ccr = cpu_to_dump_reg(s, cr);
131 static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
134 struct PPCElfFpregset *fpregset;
135 Note *note = &arg->note;
136 DumpState *s = arg->state;
138 note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG);
140 fpregset = ¬e->contents.fpregset;
141 memset(fpregset, 0, sizeof(*fpregset));
143 for (i = 0; i < 32; i++) {
144 uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
145 fpregset->fpr[i] = cpu_to_dump64(s, *fpr);
147 fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
150 static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
153 struct PPCElfVmxregset *vmxregset;
154 Note *note = &arg->note;
155 DumpState *s = arg->state;
157 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
158 vmxregset = ¬e->contents.vmxregset;
159 memset(vmxregset, 0, sizeof(*vmxregset));
161 for (i = 0; i < 32; i++) {
163 ppc_avr_t *avr = cpu_avr_ptr(&cpu->env, i);
165 #ifdef HOST_WORDS_BIGENDIAN
166 needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
168 needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
171 if (needs_byteswap) {
172 vmxregset->avr[i].u64[0] = bswap64(avr->u64[1]);
173 vmxregset->avr[i].u64[1] = bswap64(avr->u64[0]);
175 vmxregset->avr[i].u64[0] = avr->u64[0];
176 vmxregset->avr[i].u64[1] = avr->u64[1];
179 vmxregset->vscr.u32[3] = cpu_to_dump32(s, helper_mfvscr(&cpu->env));
182 static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
185 struct PPCElfVsxregset *vsxregset;
186 Note *note = &arg->note;
187 DumpState *s = arg->state;
189 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
190 vsxregset = ¬e->contents.vsxregset;
191 memset(vsxregset, 0, sizeof(*vsxregset));
193 for (i = 0; i < 32; i++) {
194 uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
195 vsxregset->vsr[i] = cpu_to_dump64(s, *vsrl);
199 static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
201 struct PPCElfSperegset *speregset;
202 Note *note = &arg->note;
203 DumpState *s = arg->state;
205 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
206 speregset = ¬e->contents.speregset;
207 memset(speregset, 0, sizeof(*speregset));
209 speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
210 speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
213 static const struct NoteFuncDescStruct {
215 void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
217 {sizeof_field(Note, contents.prstatus), ppc_write_elf_prstatus},
218 {sizeof_field(Note, contents.fpregset), ppc_write_elf_fpregset},
219 {sizeof_field(Note, contents.vmxregset), ppc_write_elf_vmxregset},
220 {sizeof_field(Note, contents.vsxregset), ppc_write_elf_vsxregset},
221 {sizeof_field(Note, contents.speregset), ppc_write_elf_speregset},
225 typedef struct NoteFuncDescStruct NoteFuncDesc;
227 int cpu_get_dump_info(ArchDumpInfo *info,
228 const struct GuestPhysBlockList *guest_phys_blocks)
231 PowerPCCPUClass *pcc;
233 if (first_cpu == NULL) {
237 cpu = POWERPC_CPU(first_cpu);
238 pcc = POWERPC_CPU_GET_CLASS(cpu);
240 info->d_machine = PPC_ELF_MACHINE;
241 info->d_class = ELFCLASS;
243 if ((*pcc->interrupts_big_endian)(cpu)) {
244 info->d_endian = ELFDATA2MSB;
246 info->d_endian = ELFDATA2LSB;
248 /* 64KB is the max page size for pseries kernel */
249 if (strncmp(object_get_typename(qdev_get_machine()),
250 "pseries-", 8) == 0) {
251 info->page_size = (1U << 16);
257 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
259 int name_size = 8; /* "CORE" or "QEMU" rounded */
260 size_t elf_note_size = 0;
262 const NoteFuncDesc *nf;
264 note_head_size = sizeof(Elf_Nhdr);
265 for (nf = note_func; nf->note_contents_func; nf++) {
266 elf_note_size = elf_note_size + note_head_size + name_size +
270 return (elf_note_size) * nr_cpus;
273 static int ppc_write_all_elf_notes(const char *note_name,
274 WriteCoreDumpFunction f,
275 PowerPCCPU *cpu, int id,
278 NoteFuncArg arg = { .state = opaque };
281 const NoteFuncDesc *nf;
283 for (nf = note_func; nf->note_contents_func; nf++) {
284 arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name));
285 arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size);
286 strncpy(arg.note.name, note_name, sizeof(arg.note.name));
288 (*nf->note_contents_func)(&arg, cpu);
291 sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
292 ret = f(&arg.note, note_size, opaque);
300 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
301 int cpuid, void *opaque)
303 PowerPCCPU *cpu = POWERPC_CPU(cs);
304 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);
307 int ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
308 int cpuid, void *opaque)
310 PowerPCCPU *cpu = POWERPC_CPU(cs);
311 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);