2 * writing ELF notes for ppc64 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 "exec/cpu-all.h"
19 #include "sysemu/dump.h"
20 #include "sysemu/kvm.h"
22 struct PPC64UserRegStruct {
38 struct PPC64ElfPrstatus {
40 struct PPC64UserRegStruct pr_reg;
45 struct PPC64ElfFpregset {
51 struct PPC64ElfVmxregset {
60 struct PPC64ElfVsxregset {
64 struct PPC64ElfSperegset {
70 typedef struct noteStruct {
75 struct PPC64ElfPrstatus prstatus;
76 struct PPC64ElfFpregset fpregset;
77 struct PPC64ElfVmxregset vmxregset;
78 struct PPC64ElfVsxregset vsxregset;
79 struct PPC64ElfSperegset speregset;
83 typedef struct NoteFuncArg {
88 static void ppc64_write_elf64_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
92 struct PPC64ElfPrstatus *prstatus;
93 struct PPC64UserRegStruct *reg;
94 Note *note = &arg->note;
95 DumpState *s = arg->state;
97 note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS);
99 prstatus = ¬e->contents.prstatus;
100 memset(prstatus, 0, sizeof(*prstatus));
101 reg = &prstatus->pr_reg;
103 for (i = 0; i < 32; i++) {
104 reg->gpr[i] = cpu_to_dump64(s, cpu->env.gpr[i]);
106 reg->nip = cpu_to_dump64(s, cpu->env.nip);
107 reg->msr = cpu_to_dump64(s, cpu->env.msr);
108 reg->ctr = cpu_to_dump64(s, cpu->env.ctr);
109 reg->link = cpu_to_dump64(s, cpu->env.lr);
110 reg->xer = cpu_to_dump64(s, cpu_read_xer(&cpu->env));
113 for (i = 0; i < 8; i++) {
114 cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
116 reg->ccr = cpu_to_dump64(s, cr);
119 static void ppc64_write_elf64_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
122 struct PPC64ElfFpregset *fpregset;
123 Note *note = &arg->note;
124 DumpState *s = arg->state;
126 note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG);
128 fpregset = ¬e->contents.fpregset;
129 memset(fpregset, 0, sizeof(*fpregset));
131 for (i = 0; i < 32; i++) {
132 fpregset->fpr[i] = cpu_to_dump64(s, cpu->env.fpr[i]);
134 fpregset->fpscr = cpu_to_dump64(s, cpu->env.fpscr);
137 static void ppc64_write_elf64_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
140 struct PPC64ElfVmxregset *vmxregset;
141 Note *note = &arg->note;
142 DumpState *s = arg->state;
144 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
145 vmxregset = ¬e->contents.vmxregset;
146 memset(vmxregset, 0, sizeof(*vmxregset));
148 for (i = 0; i < 32; i++) {
151 #ifdef HOST_WORDS_BIGENDIAN
152 needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
154 needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
157 if (needs_byteswap) {
158 vmxregset->avr[i].u64[0] = bswap64(cpu->env.avr[i].u64[1]);
159 vmxregset->avr[i].u64[1] = bswap64(cpu->env.avr[i].u64[0]);
161 vmxregset->avr[i].u64[0] = cpu->env.avr[i].u64[0];
162 vmxregset->avr[i].u64[1] = cpu->env.avr[i].u64[1];
165 vmxregset->vscr.u32[3] = cpu_to_dump32(s, cpu->env.vscr);
167 static void ppc64_write_elf64_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
170 struct PPC64ElfVsxregset *vsxregset;
171 Note *note = &arg->note;
172 DumpState *s = arg->state;
174 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
175 vsxregset = ¬e->contents.vsxregset;
176 memset(vsxregset, 0, sizeof(*vsxregset));
178 for (i = 0; i < 32; i++) {
179 vsxregset->vsr[i] = cpu_to_dump64(s, cpu->env.vsr[i]);
182 static void ppc64_write_elf64_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
184 struct PPC64ElfSperegset *speregset;
185 Note *note = &arg->note;
186 DumpState *s = arg->state;
188 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
189 speregset = ¬e->contents.speregset;
190 memset(speregset, 0, sizeof(*speregset));
192 speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
193 speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
196 static const struct NoteFuncDescStruct {
198 void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
200 {sizeof(((Note *)0)->contents.prstatus), ppc64_write_elf64_prstatus},
201 {sizeof(((Note *)0)->contents.fpregset), ppc64_write_elf64_fpregset},
202 {sizeof(((Note *)0)->contents.vmxregset), ppc64_write_elf64_vmxregset},
203 {sizeof(((Note *)0)->contents.vsxregset), ppc64_write_elf64_vsxregset},
204 {sizeof(((Note *)0)->contents.speregset), ppc64_write_elf64_speregset},
208 typedef struct NoteFuncDescStruct NoteFuncDesc;
210 int cpu_get_dump_info(ArchDumpInfo *info,
211 const struct GuestPhysBlockList *guest_phys_blocks)
213 PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
214 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
216 info->d_machine = EM_PPC64;
217 info->d_class = ELFCLASS64;
218 if ((*pcc->interrupts_big_endian)(cpu)) {
219 info->d_endian = ELFDATA2MSB;
221 info->d_endian = ELFDATA2LSB;
223 /* 64KB is the max page size for pseries kernel */
224 if (strncmp(object_get_typename(qdev_get_machine()),
225 "pseries-", 8) == 0) {
226 info->page_size = (1U << 16);
232 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
234 int name_size = 8; /* "CORE" or "QEMU" rounded */
235 size_t elf_note_size = 0;
237 const NoteFuncDesc *nf;
239 if (class != ELFCLASS64) {
242 assert(machine == EM_PPC64);
244 note_head_size = sizeof(Elf64_Nhdr);
246 for (nf = note_func; nf->note_contents_func; nf++) {
247 elf_note_size = elf_note_size + note_head_size + name_size +
251 return (elf_note_size) * nr_cpus;
254 static int ppc64_write_all_elf64_notes(const char *note_name,
255 WriteCoreDumpFunction f,
256 PowerPCCPU *cpu, int id,
259 NoteFuncArg arg = { .state = opaque };
262 const NoteFuncDesc *nf;
264 for (nf = note_func; nf->note_contents_func; nf++) {
265 arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name));
266 arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size);
267 strncpy(arg.note.name, note_name, sizeof(arg.note.name));
269 (*nf->note_contents_func)(&arg, cpu);
272 sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
273 ret = f(&arg.note, note_size, opaque);
281 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
282 int cpuid, void *opaque)
284 PowerPCCPU *cpu = POWERPC_CPU(cs);
285 return ppc64_write_all_elf64_notes("CORE", f, cpu, cpuid, opaque);