2 * writing ELF notes for ppc{64,} arch
5 * Copyright IBM, Corp. 2013
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
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 uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
144 fpregset->fpr[i] = cpu_to_dump64(s, *fpr);
146 fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
149 static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
152 struct PPCElfVmxregset *vmxregset;
153 Note *note = &arg->note;
154 DumpState *s = arg->state;
156 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
157 vmxregset = ¬e->contents.vmxregset;
158 memset(vmxregset, 0, sizeof(*vmxregset));
160 for (i = 0; i < 32; i++) {
162 ppc_avr_t *avr = cpu_avr_ptr(&cpu->env, i);
165 needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
167 needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
170 if (needs_byteswap) {
171 vmxregset->avr[i].u64[0] = bswap64(avr->u64[1]);
172 vmxregset->avr[i].u64[1] = bswap64(avr->u64[0]);
174 vmxregset->avr[i].u64[0] = avr->u64[0];
175 vmxregset->avr[i].u64[1] = avr->u64[1];
178 vmxregset->vscr.u32[3] = cpu_to_dump32(s, ppc_get_vscr(&cpu->env));
181 static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
184 struct PPCElfVsxregset *vsxregset;
185 Note *note = &arg->note;
186 DumpState *s = arg->state;
188 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
189 vsxregset = ¬e->contents.vsxregset;
190 memset(vsxregset, 0, sizeof(*vsxregset));
192 for (i = 0; i < 32; i++) {
193 uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
194 vsxregset->vsr[i] = cpu_to_dump64(s, *vsrl);
198 static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
200 struct PPCElfSperegset *speregset;
201 Note *note = &arg->note;
202 DumpState *s = arg->state;
204 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
205 speregset = ¬e->contents.speregset;
206 memset(speregset, 0, sizeof(*speregset));
208 speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
209 speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
212 static const struct NoteFuncDescStruct {
214 void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
216 {sizeof_field(Note, contents.prstatus), ppc_write_elf_prstatus},
217 {sizeof_field(Note, contents.fpregset), ppc_write_elf_fpregset},
218 {sizeof_field(Note, contents.vmxregset), ppc_write_elf_vmxregset},
219 {sizeof_field(Note, contents.vsxregset), ppc_write_elf_vsxregset},
220 {sizeof_field(Note, contents.speregset), ppc_write_elf_speregset},
224 typedef struct NoteFuncDescStruct NoteFuncDesc;
226 int cpu_get_dump_info(ArchDumpInfo *info,
227 const struct GuestPhysBlockList *guest_phys_blocks)
231 if (first_cpu == NULL) {
235 cpu = POWERPC_CPU(first_cpu);
237 info->d_machine = PPC_ELF_MACHINE;
238 info->d_class = ELFCLASS;
240 if (ppc_interrupts_little_endian(cpu, cpu->env.has_hv_mode)) {
241 info->d_endian = ELFDATA2LSB;
243 info->d_endian = ELFDATA2MSB;
245 /* 64KB is the max page size for pseries kernel */
246 if (strncmp(object_get_typename(qdev_get_machine()),
247 "pseries-", 8) == 0) {
248 info->page_size = (1U << 16);
254 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
256 int name_size = 8; /* "CORE" or "QEMU" rounded */
257 size_t elf_note_size = 0;
259 const NoteFuncDesc *nf;
261 note_head_size = sizeof(Elf_Nhdr);
262 for (nf = note_func; nf->note_contents_func; nf++) {
263 elf_note_size = elf_note_size + note_head_size + name_size +
267 return (elf_note_size) * nr_cpus;
270 static int ppc_write_all_elf_notes(const char *note_name,
271 WriteCoreDumpFunction f,
272 PowerPCCPU *cpu, int id,
275 NoteFuncArg arg = { .state = s };
278 const NoteFuncDesc *nf;
280 for (nf = note_func; nf->note_contents_func; nf++) {
281 arg.note.hdr.n_namesz = cpu_to_dump32(s, sizeof(arg.note.name));
282 arg.note.hdr.n_descsz = cpu_to_dump32(s, nf->contents_size);
283 strncpy(arg.note.name, note_name, sizeof(arg.note.name));
285 (*nf->note_contents_func)(&arg, cpu);
288 sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
289 ret = f(&arg.note, note_size, s);
297 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
298 int cpuid, DumpState *s)
300 PowerPCCPU *cpu = POWERPC_CPU(cs);
301 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, s);
304 int ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
305 int cpuid, DumpState *s)
307 PowerPCCPU *cpu = POWERPC_CPU(cs);
308 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, s);