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"
22 #define ELFCLASS ELFCLASS64
23 #define cpu_to_dump_reg cpu_to_dump64
24 typedef uint64_t reg_t;
25 typedef Elf64_Nhdr Elf_Nhdr;
27 #define ELFCLASS ELFCLASS32
28 #define cpu_to_dump_reg cpu_to_dump32
29 typedef uint32_t reg_t;
30 typedef Elf32_Nhdr Elf_Nhdr;
31 #endif /* TARGET_PPC64 */
33 struct PPCUserRegStruct {
49 struct PPCElfPrstatus {
51 struct PPCUserRegStruct pr_reg;
56 struct PPCElfFpregset {
62 struct PPCElfVmxregset {
71 struct PPCElfVsxregset {
75 struct PPCElfSperegset {
81 typedef struct noteStruct {
86 struct PPCElfPrstatus prstatus;
87 struct PPCElfFpregset fpregset;
88 struct PPCElfVmxregset vmxregset;
89 struct PPCElfVsxregset vsxregset;
90 struct PPCElfSperegset speregset;
94 typedef struct NoteFuncArg {
99 static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
103 struct PPCElfPrstatus *prstatus;
104 struct PPCUserRegStruct *reg;
105 Note *note = &arg->note;
106 DumpState *s = arg->state;
108 note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS);
110 prstatus = ¬e->contents.prstatus;
111 memset(prstatus, 0, sizeof(*prstatus));
112 reg = &prstatus->pr_reg;
114 for (i = 0; i < 32; i++) {
115 reg->gpr[i] = cpu_to_dump_reg(s, cpu->env.gpr[i]);
117 reg->nip = cpu_to_dump_reg(s, cpu->env.nip);
118 reg->msr = cpu_to_dump_reg(s, cpu->env.msr);
119 reg->ctr = cpu_to_dump_reg(s, cpu->env.ctr);
120 reg->link = cpu_to_dump_reg(s, cpu->env.lr);
121 reg->xer = cpu_to_dump_reg(s, cpu_read_xer(&cpu->env));
124 for (i = 0; i < 8; i++) {
125 cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
127 reg->ccr = cpu_to_dump_reg(s, cr);
130 static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
133 struct PPCElfFpregset *fpregset;
134 Note *note = &arg->note;
135 DumpState *s = arg->state;
137 note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG);
139 fpregset = ¬e->contents.fpregset;
140 memset(fpregset, 0, sizeof(*fpregset));
142 for (i = 0; i < 32; i++) {
143 fpregset->fpr[i] = cpu_to_dump64(s, cpu->env.fpr[i]);
145 fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
148 static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
151 struct PPCElfVmxregset *vmxregset;
152 Note *note = &arg->note;
153 DumpState *s = arg->state;
155 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
156 vmxregset = ¬e->contents.vmxregset;
157 memset(vmxregset, 0, sizeof(*vmxregset));
159 for (i = 0; i < 32; i++) {
162 #ifdef HOST_WORDS_BIGENDIAN
163 needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
165 needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
168 if (needs_byteswap) {
169 vmxregset->avr[i].u64[0] = bswap64(cpu->env.avr[i].u64[1]);
170 vmxregset->avr[i].u64[1] = bswap64(cpu->env.avr[i].u64[0]);
172 vmxregset->avr[i].u64[0] = cpu->env.avr[i].u64[0];
173 vmxregset->avr[i].u64[1] = cpu->env.avr[i].u64[1];
176 vmxregset->vscr.u32[3] = cpu_to_dump32(s, cpu->env.vscr);
179 static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
182 struct PPCElfVsxregset *vsxregset;
183 Note *note = &arg->note;
184 DumpState *s = arg->state;
186 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
187 vsxregset = ¬e->contents.vsxregset;
188 memset(vsxregset, 0, sizeof(*vsxregset));
190 for (i = 0; i < 32; i++) {
191 vsxregset->vsr[i] = cpu_to_dump64(s, cpu->env.vsr[i]);
195 static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
197 struct PPCElfSperegset *speregset;
198 Note *note = &arg->note;
199 DumpState *s = arg->state;
201 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
202 speregset = ¬e->contents.speregset;
203 memset(speregset, 0, sizeof(*speregset));
205 speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
206 speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
209 static const struct NoteFuncDescStruct {
211 void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
213 {sizeof(((Note *)0)->contents.prstatus), ppc_write_elf_prstatus},
214 {sizeof(((Note *)0)->contents.fpregset), ppc_write_elf_fpregset},
215 {sizeof(((Note *)0)->contents.vmxregset), ppc_write_elf_vmxregset},
216 {sizeof(((Note *)0)->contents.vsxregset), ppc_write_elf_vsxregset},
217 {sizeof(((Note *)0)->contents.speregset), ppc_write_elf_speregset},
221 typedef struct NoteFuncDescStruct NoteFuncDesc;
223 int cpu_get_dump_info(ArchDumpInfo *info,
224 const struct GuestPhysBlockList *guest_phys_blocks)
227 PowerPCCPUClass *pcc;
229 if (first_cpu == NULL) {
233 cpu = POWERPC_CPU(first_cpu);
234 pcc = POWERPC_CPU_GET_CLASS(cpu);
236 info->d_machine = PPC_ELF_MACHINE;
237 info->d_class = ELFCLASS;
239 if ((*pcc->interrupts_big_endian)(cpu)) {
240 info->d_endian = ELFDATA2MSB;
242 info->d_endian = ELFDATA2LSB;
244 /* 64KB is the max page size for pseries kernel */
245 if (strncmp(object_get_typename(qdev_get_machine()),
246 "pseries-", 8) == 0) {
247 info->page_size = (1U << 16);
253 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
255 int name_size = 8; /* "CORE" or "QEMU" rounded */
256 size_t elf_note_size = 0;
258 const NoteFuncDesc *nf;
260 note_head_size = sizeof(Elf_Nhdr);
261 for (nf = note_func; nf->note_contents_func; nf++) {
262 elf_note_size = elf_note_size + note_head_size + name_size +
266 return (elf_note_size) * nr_cpus;
269 static int ppc_write_all_elf_notes(const char *note_name,
270 WriteCoreDumpFunction f,
271 PowerPCCPU *cpu, int id,
274 NoteFuncArg arg = { .state = opaque };
277 const NoteFuncDesc *nf;
279 for (nf = note_func; nf->note_contents_func; nf++) {
280 arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name));
281 arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size);
282 strncpy(arg.note.name, note_name, sizeof(arg.note.name));
284 (*nf->note_contents_func)(&arg, cpu);
287 sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
288 ret = f(&arg.note, note_size, opaque);
296 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
297 int cpuid, void *opaque)
299 PowerPCCPU *cpu = POWERPC_CPU(cs);
300 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);
303 int ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
304 int cpuid, void *opaque)
306 PowerPCCPU *cpu = POWERPC_CPU(cs);
307 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);