1e62fbc54SAneesh Kumar K.V /*
2356bb70eSMike Nawrocki * writing ELF notes for ppc{64,} arch
3e62fbc54SAneesh Kumar K.V *
4e62fbc54SAneesh Kumar K.V *
5e62fbc54SAneesh Kumar K.V * Copyright IBM, Corp. 2013
6e62fbc54SAneesh Kumar K.V *
7e62fbc54SAneesh Kumar K.V * Authors:
8e62fbc54SAneesh Kumar K.V * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9e62fbc54SAneesh Kumar K.V *
10e62fbc54SAneesh Kumar K.V * This work is licensed under the terms of the GNU GPL, version 2. See
11e62fbc54SAneesh Kumar K.V * the COPYING file in the top-level directory.
12e62fbc54SAneesh Kumar K.V *
13e62fbc54SAneesh Kumar K.V */
14e62fbc54SAneesh Kumar K.V
150d75590dSPeter Maydell #include "qemu/osdep.h"
16e62fbc54SAneesh Kumar K.V #include "cpu.h"
17e62fbc54SAneesh Kumar K.V #include "elf.h"
18*32cad1ffSPhilippe Mathieu-Daudé #include "system/dump.h"
19*32cad1ffSPhilippe Mathieu-Daudé #include "system/kvm.h"
20e62fbc54SAneesh Kumar K.V
21356bb70eSMike Nawrocki #ifdef TARGET_PPC64
22356bb70eSMike Nawrocki #define ELFCLASS ELFCLASS64
23356bb70eSMike Nawrocki #define cpu_to_dump_reg cpu_to_dump64
24356bb70eSMike Nawrocki typedef uint64_t reg_t;
25356bb70eSMike Nawrocki typedef Elf64_Nhdr Elf_Nhdr;
26356bb70eSMike Nawrocki #else
27356bb70eSMike Nawrocki #define ELFCLASS ELFCLASS32
28356bb70eSMike Nawrocki #define cpu_to_dump_reg cpu_to_dump32
29356bb70eSMike Nawrocki typedef uint32_t reg_t;
30356bb70eSMike Nawrocki typedef Elf32_Nhdr Elf_Nhdr;
31356bb70eSMike Nawrocki #endif /* TARGET_PPC64 */
32356bb70eSMike Nawrocki
33356bb70eSMike Nawrocki struct PPCUserRegStruct {
34356bb70eSMike Nawrocki reg_t gpr[32];
35356bb70eSMike Nawrocki reg_t nip;
36356bb70eSMike Nawrocki reg_t msr;
37356bb70eSMike Nawrocki reg_t orig_gpr3;
38356bb70eSMike Nawrocki reg_t ctr;
39356bb70eSMike Nawrocki reg_t link;
40356bb70eSMike Nawrocki reg_t xer;
41356bb70eSMike Nawrocki reg_t ccr;
42356bb70eSMike Nawrocki reg_t softe;
43356bb70eSMike Nawrocki reg_t trap;
44356bb70eSMike Nawrocki reg_t dar;
45356bb70eSMike Nawrocki reg_t dsisr;
46356bb70eSMike Nawrocki reg_t result;
47e62fbc54SAneesh Kumar K.V } QEMU_PACKED;
48e62fbc54SAneesh Kumar K.V
49356bb70eSMike Nawrocki struct PPCElfPrstatus {
502587a57dSOmar Sandoval char pad1[32]; /* 32 == offsetof(struct elf_prstatus, pr_pid) */
512587a57dSOmar Sandoval uint32_t pid;
522587a57dSOmar Sandoval char pad2[76]; /* 76 == offsetof(struct elf_prstatus, pr_reg) -
532587a57dSOmar Sandoval offsetof(struct elf_prstatus, pr_ppid) */
54356bb70eSMike Nawrocki struct PPCUserRegStruct pr_reg;
552587a57dSOmar Sandoval char pad3[40]; /* 40 == sizeof(struct elf_prstatus) -
562587a57dSOmar Sandoval offsetof(struct elf_prstatus, pr_reg) -
572587a57dSOmar Sandoval sizeof(struct user_pt_regs) */
58e62fbc54SAneesh Kumar K.V } QEMU_PACKED;
59e62fbc54SAneesh Kumar K.V
60e62fbc54SAneesh Kumar K.V
61356bb70eSMike Nawrocki struct PPCElfFpregset {
62e62fbc54SAneesh Kumar K.V uint64_t fpr[32];
63356bb70eSMike Nawrocki reg_t fpscr;
64e62fbc54SAneesh Kumar K.V } QEMU_PACKED;
65e62fbc54SAneesh Kumar K.V
66e62fbc54SAneesh Kumar K.V
67356bb70eSMike Nawrocki struct PPCElfVmxregset {
68e62fbc54SAneesh Kumar K.V ppc_avr_t avr[32];
69e62fbc54SAneesh Kumar K.V ppc_avr_t vscr;
70e62fbc54SAneesh Kumar K.V union {
71e62fbc54SAneesh Kumar K.V ppc_avr_t unused;
72e62fbc54SAneesh Kumar K.V uint32_t value;
73e62fbc54SAneesh Kumar K.V } vrsave;
74e62fbc54SAneesh Kumar K.V } QEMU_PACKED;
75e62fbc54SAneesh Kumar K.V
76356bb70eSMike Nawrocki struct PPCElfVsxregset {
77e62fbc54SAneesh Kumar K.V uint64_t vsr[32];
78e62fbc54SAneesh Kumar K.V } QEMU_PACKED;
79e62fbc54SAneesh Kumar K.V
80356bb70eSMike Nawrocki struct PPCElfSperegset {
81e62fbc54SAneesh Kumar K.V uint32_t evr[32];
82e62fbc54SAneesh Kumar K.V uint64_t spe_acc;
83e62fbc54SAneesh Kumar K.V uint32_t spe_fscr;
84e62fbc54SAneesh Kumar K.V } QEMU_PACKED;
85e62fbc54SAneesh Kumar K.V
86e62fbc54SAneesh Kumar K.V typedef struct noteStruct {
87356bb70eSMike Nawrocki Elf_Nhdr hdr;
88e62fbc54SAneesh Kumar K.V char name[5];
89e62fbc54SAneesh Kumar K.V char pad3[3];
90e62fbc54SAneesh Kumar K.V union {
91356bb70eSMike Nawrocki struct PPCElfPrstatus prstatus;
92356bb70eSMike Nawrocki struct PPCElfFpregset fpregset;
93356bb70eSMike Nawrocki struct PPCElfVmxregset vmxregset;
94356bb70eSMike Nawrocki struct PPCElfVsxregset vsxregset;
95356bb70eSMike Nawrocki struct PPCElfSperegset speregset;
96e62fbc54SAneesh Kumar K.V } contents;
97e62fbc54SAneesh Kumar K.V } QEMU_PACKED Note;
98e62fbc54SAneesh Kumar K.V
990c967de9SBharata B Rao typedef struct NoteFuncArg {
1000c967de9SBharata B Rao Note note;
1010c967de9SBharata B Rao DumpState *state;
1020c967de9SBharata B Rao } NoteFuncArg;
103e62fbc54SAneesh Kumar K.V
ppc_write_elf_prstatus(NoteFuncArg * arg,PowerPCCPU * cpu,int id)1042587a57dSOmar Sandoval static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
105e62fbc54SAneesh Kumar K.V {
106e62fbc54SAneesh Kumar K.V int i;
107356bb70eSMike Nawrocki reg_t cr;
108356bb70eSMike Nawrocki struct PPCElfPrstatus *prstatus;
109356bb70eSMike Nawrocki struct PPCUserRegStruct *reg;
1100c967de9SBharata B Rao Note *note = &arg->note;
1110c967de9SBharata B Rao DumpState *s = arg->state;
112e62fbc54SAneesh Kumar K.V
1130c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS);
114e62fbc54SAneesh Kumar K.V
115e62fbc54SAneesh Kumar K.V prstatus = ¬e->contents.prstatus;
116e62fbc54SAneesh Kumar K.V memset(prstatus, 0, sizeof(*prstatus));
1172587a57dSOmar Sandoval prstatus->pid = cpu_to_dump32(s, id);
118e62fbc54SAneesh Kumar K.V reg = &prstatus->pr_reg;
119e62fbc54SAneesh Kumar K.V
120e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) {
121356bb70eSMike Nawrocki reg->gpr[i] = cpu_to_dump_reg(s, cpu->env.gpr[i]);
122e62fbc54SAneesh Kumar K.V }
123356bb70eSMike Nawrocki reg->nip = cpu_to_dump_reg(s, cpu->env.nip);
124356bb70eSMike Nawrocki reg->msr = cpu_to_dump_reg(s, cpu->env.msr);
125356bb70eSMike Nawrocki reg->ctr = cpu_to_dump_reg(s, cpu->env.ctr);
126356bb70eSMike Nawrocki reg->link = cpu_to_dump_reg(s, cpu->env.lr);
127356bb70eSMike Nawrocki reg->xer = cpu_to_dump_reg(s, cpu_read_xer(&cpu->env));
128e62fbc54SAneesh Kumar K.V
129e62fbc54SAneesh Kumar K.V cr = 0;
130e62fbc54SAneesh Kumar K.V for (i = 0; i < 8; i++) {
131e62fbc54SAneesh Kumar K.V cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
132e62fbc54SAneesh Kumar K.V }
133356bb70eSMike Nawrocki reg->ccr = cpu_to_dump_reg(s, cr);
134e62fbc54SAneesh Kumar K.V }
135e62fbc54SAneesh Kumar K.V
ppc_write_elf_fpregset(NoteFuncArg * arg,PowerPCCPU * cpu,int id)1362587a57dSOmar Sandoval static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
137e62fbc54SAneesh Kumar K.V {
138e62fbc54SAneesh Kumar K.V int i;
139356bb70eSMike Nawrocki struct PPCElfFpregset *fpregset;
1400c967de9SBharata B Rao Note *note = &arg->note;
1410c967de9SBharata B Rao DumpState *s = arg->state;
142e62fbc54SAneesh Kumar K.V
1430c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG);
144e62fbc54SAneesh Kumar K.V
145e62fbc54SAneesh Kumar K.V fpregset = ¬e->contents.fpregset;
146e62fbc54SAneesh Kumar K.V memset(fpregset, 0, sizeof(*fpregset));
147e62fbc54SAneesh Kumar K.V
148e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) {
149ef96e3aeSMark Cave-Ayland uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
150ef96e3aeSMark Cave-Ayland fpregset->fpr[i] = cpu_to_dump64(s, *fpr);
151e62fbc54SAneesh Kumar K.V }
152356bb70eSMike Nawrocki fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
153e62fbc54SAneesh Kumar K.V }
154e62fbc54SAneesh Kumar K.V
ppc_write_elf_vmxregset(NoteFuncArg * arg,PowerPCCPU * cpu,int id)1552587a57dSOmar Sandoval static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
156e62fbc54SAneesh Kumar K.V {
157e62fbc54SAneesh Kumar K.V int i;
158356bb70eSMike Nawrocki struct PPCElfVmxregset *vmxregset;
1590c967de9SBharata B Rao Note *note = &arg->note;
1600c967de9SBharata B Rao DumpState *s = arg->state;
161e62fbc54SAneesh Kumar K.V
1620c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
163e62fbc54SAneesh Kumar K.V vmxregset = ¬e->contents.vmxregset;
164e62fbc54SAneesh Kumar K.V memset(vmxregset, 0, sizeof(*vmxregset));
165e62fbc54SAneesh Kumar K.V
166e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) {
1670c967de9SBharata B Rao bool needs_byteswap;
168ef96e3aeSMark Cave-Ayland ppc_avr_t *avr = cpu_avr_ptr(&cpu->env, i);
1690c967de9SBharata B Rao
170e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1710c967de9SBharata B Rao needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
1720c967de9SBharata B Rao #else
1730c967de9SBharata B Rao needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
1740c967de9SBharata B Rao #endif
1750c967de9SBharata B Rao
1760c967de9SBharata B Rao if (needs_byteswap) {
177ef96e3aeSMark Cave-Ayland vmxregset->avr[i].u64[0] = bswap64(avr->u64[1]);
178ef96e3aeSMark Cave-Ayland vmxregset->avr[i].u64[1] = bswap64(avr->u64[0]);
1790c967de9SBharata B Rao } else {
180ef96e3aeSMark Cave-Ayland vmxregset->avr[i].u64[0] = avr->u64[0];
181ef96e3aeSMark Cave-Ayland vmxregset->avr[i].u64[1] = avr->u64[1];
182e62fbc54SAneesh Kumar K.V }
183e62fbc54SAneesh Kumar K.V }
184c19940dbSBruno Larsen (billionai) vmxregset->vscr.u32[3] = cpu_to_dump32(s, ppc_get_vscr(&cpu->env));
1850c967de9SBharata B Rao }
186356bb70eSMike Nawrocki
ppc_write_elf_vsxregset(NoteFuncArg * arg,PowerPCCPU * cpu,int id)1872587a57dSOmar Sandoval static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
188e62fbc54SAneesh Kumar K.V {
189e62fbc54SAneesh Kumar K.V int i;
190356bb70eSMike Nawrocki struct PPCElfVsxregset *vsxregset;
1910c967de9SBharata B Rao Note *note = &arg->note;
1920c967de9SBharata B Rao DumpState *s = arg->state;
193e62fbc54SAneesh Kumar K.V
1940c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
195e62fbc54SAneesh Kumar K.V vsxregset = ¬e->contents.vsxregset;
196e62fbc54SAneesh Kumar K.V memset(vsxregset, 0, sizeof(*vsxregset));
197e62fbc54SAneesh Kumar K.V
198e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) {
199ef96e3aeSMark Cave-Ayland uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
200ef96e3aeSMark Cave-Ayland vsxregset->vsr[i] = cpu_to_dump64(s, *vsrl);
201e62fbc54SAneesh Kumar K.V }
202e62fbc54SAneesh Kumar K.V }
203356bb70eSMike Nawrocki
ppc_write_elf_speregset(NoteFuncArg * arg,PowerPCCPU * cpu,int id)2042587a57dSOmar Sandoval static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
205e62fbc54SAneesh Kumar K.V {
206356bb70eSMike Nawrocki struct PPCElfSperegset *speregset;
2070c967de9SBharata B Rao Note *note = &arg->note;
2080c967de9SBharata B Rao DumpState *s = arg->state;
2090c967de9SBharata B Rao
2100c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
211e62fbc54SAneesh Kumar K.V speregset = ¬e->contents.speregset;
212e62fbc54SAneesh Kumar K.V memset(speregset, 0, sizeof(*speregset));
213e62fbc54SAneesh Kumar K.V
2140c967de9SBharata B Rao speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
2150c967de9SBharata B Rao speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
216e62fbc54SAneesh Kumar K.V }
217e62fbc54SAneesh Kumar K.V
218cfd54a04SStefan Weil static const struct NoteFuncDescStruct {
219e62fbc54SAneesh Kumar K.V int contents_size;
2202587a57dSOmar Sandoval void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu, int id);
221e62fbc54SAneesh Kumar K.V } note_func[] = {
222f18793b0SStefan Hajnoczi {sizeof_field(Note, contents.prstatus), ppc_write_elf_prstatus},
223f18793b0SStefan Hajnoczi {sizeof_field(Note, contents.fpregset), ppc_write_elf_fpregset},
224f18793b0SStefan Hajnoczi {sizeof_field(Note, contents.vmxregset), ppc_write_elf_vmxregset},
225f18793b0SStefan Hajnoczi {sizeof_field(Note, contents.vsxregset), ppc_write_elf_vsxregset},
226f18793b0SStefan Hajnoczi {sizeof_field(Note, contents.speregset), ppc_write_elf_speregset},
227e62fbc54SAneesh Kumar K.V { 0, NULL}
228e62fbc54SAneesh Kumar K.V };
229e62fbc54SAneesh Kumar K.V
230e62fbc54SAneesh Kumar K.V typedef struct NoteFuncDescStruct NoteFuncDesc;
231e62fbc54SAneesh Kumar K.V
cpu_get_dump_info(ArchDumpInfo * info,const struct GuestPhysBlockList * guest_phys_blocks)232e62fbc54SAneesh Kumar K.V int cpu_get_dump_info(ArchDumpInfo *info,
233e62fbc54SAneesh Kumar K.V const struct GuestPhysBlockList *guest_phys_blocks)
234e62fbc54SAneesh Kumar K.V {
235b1fde1efSLaurent Vivier PowerPCCPU *cpu;
236b1fde1efSLaurent Vivier
237b1fde1efSLaurent Vivier if (first_cpu == NULL) {
238b1fde1efSLaurent Vivier return -1;
239b1fde1efSLaurent Vivier }
240b1fde1efSLaurent Vivier
241b1fde1efSLaurent Vivier cpu = POWERPC_CPU(first_cpu);
2421e6ed54eSBharata B Rao
243356bb70eSMike Nawrocki info->d_machine = PPC_ELF_MACHINE;
244356bb70eSMike Nawrocki info->d_class = ELFCLASS;
245356bb70eSMike Nawrocki
24693c691a0SNarayana Murty N if (ppc_interrupts_little_endian(cpu, !!(cpu->env.msr_mask & MSR_HVB))) {
2471e6ed54eSBharata B Rao info->d_endian = ELFDATA2LSB;
248c11dc15dSGreg Kurz } else {
249c11dc15dSGreg Kurz info->d_endian = ELFDATA2MSB;
2501e6ed54eSBharata B Rao }
251760d88d1SLaurent Vivier /* 64KB is the max page size for pseries kernel */
252760d88d1SLaurent Vivier if (strncmp(object_get_typename(qdev_get_machine()),
253760d88d1SLaurent Vivier "pseries-", 8) == 0) {
254760d88d1SLaurent Vivier info->page_size = (1U << 16);
255760d88d1SLaurent Vivier }
256e62fbc54SAneesh Kumar K.V
257e62fbc54SAneesh Kumar K.V return 0;
258e62fbc54SAneesh Kumar K.V }
259e62fbc54SAneesh Kumar K.V
cpu_get_note_size(int class,int machine,int nr_cpus)260e62fbc54SAneesh Kumar K.V ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
261e62fbc54SAneesh Kumar K.V {
262e62fbc54SAneesh Kumar K.V int name_size = 8; /* "CORE" or "QEMU" rounded */
263e62fbc54SAneesh Kumar K.V size_t elf_note_size = 0;
264e62fbc54SAneesh Kumar K.V int note_head_size;
265cfd54a04SStefan Weil const NoteFuncDesc *nf;
266e62fbc54SAneesh Kumar K.V
267356bb70eSMike Nawrocki note_head_size = sizeof(Elf_Nhdr);
268e62fbc54SAneesh Kumar K.V for (nf = note_func; nf->note_contents_func; nf++) {
269e62fbc54SAneesh Kumar K.V elf_note_size = elf_note_size + note_head_size + name_size +
270e62fbc54SAneesh Kumar K.V nf->contents_size;
271e62fbc54SAneesh Kumar K.V }
272e62fbc54SAneesh Kumar K.V
273e62fbc54SAneesh Kumar K.V return (elf_note_size) * nr_cpus;
274e62fbc54SAneesh Kumar K.V }
275e62fbc54SAneesh Kumar K.V
ppc_write_all_elf_notes(const char * note_name,WriteCoreDumpFunction f,PowerPCCPU * cpu,int id,DumpState * s)276356bb70eSMike Nawrocki static int ppc_write_all_elf_notes(const char *note_name,
277e62fbc54SAneesh Kumar K.V WriteCoreDumpFunction f,
278e62fbc54SAneesh Kumar K.V PowerPCCPU *cpu, int id,
2791af0006aSJanosch Frank DumpState *s)
280e62fbc54SAneesh Kumar K.V {
2811af0006aSJanosch Frank NoteFuncArg arg = { .state = s };
282e62fbc54SAneesh Kumar K.V int ret = -1;
283e62fbc54SAneesh Kumar K.V int note_size;
284cfd54a04SStefan Weil const NoteFuncDesc *nf;
285e62fbc54SAneesh Kumar K.V
286e62fbc54SAneesh Kumar K.V for (nf = note_func; nf->note_contents_func; nf++) {
2871af0006aSJanosch Frank arg.note.hdr.n_namesz = cpu_to_dump32(s, sizeof(arg.note.name));
2881af0006aSJanosch Frank arg.note.hdr.n_descsz = cpu_to_dump32(s, nf->contents_size);
2890c967de9SBharata B Rao strncpy(arg.note.name, note_name, sizeof(arg.note.name));
290e62fbc54SAneesh Kumar K.V
2912587a57dSOmar Sandoval (*nf->note_contents_func)(&arg, cpu, id);
292e62fbc54SAneesh Kumar K.V
2930c967de9SBharata B Rao note_size =
2940c967de9SBharata B Rao sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
2951af0006aSJanosch Frank ret = f(&arg.note, note_size, s);
296e62fbc54SAneesh Kumar K.V if (ret < 0) {
297e62fbc54SAneesh Kumar K.V return -1;
298e62fbc54SAneesh Kumar K.V }
299e62fbc54SAneesh Kumar K.V }
300e62fbc54SAneesh Kumar K.V return 0;
301e62fbc54SAneesh Kumar K.V }
302e62fbc54SAneesh Kumar K.V
ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f,CPUState * cs,int cpuid,DumpState * s)303e62fbc54SAneesh Kumar K.V int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
3041af0006aSJanosch Frank int cpuid, DumpState *s)
305e62fbc54SAneesh Kumar K.V {
306e62fbc54SAneesh Kumar K.V PowerPCCPU *cpu = POWERPC_CPU(cs);
3071af0006aSJanosch Frank return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, s);
308356bb70eSMike Nawrocki }
309356bb70eSMike Nawrocki
ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f,CPUState * cs,int cpuid,DumpState * s)310356bb70eSMike Nawrocki int ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
3111af0006aSJanosch Frank int cpuid, DumpState *s)
312356bb70eSMike Nawrocki {
313356bb70eSMike Nawrocki PowerPCCPU *cpu = POWERPC_CPU(cs);
3141af0006aSJanosch Frank return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, s);
315e62fbc54SAneesh Kumar K.V }
316