1e62fbc54SAneesh Kumar K.V /* 2*356bb70eSMike 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" 18e62fbc54SAneesh Kumar K.V #include "exec/cpu-all.h" 19e62fbc54SAneesh Kumar K.V #include "sysemu/dump.h" 20e62fbc54SAneesh Kumar K.V #include "sysemu/kvm.h" 21e62fbc54SAneesh Kumar K.V 22*356bb70eSMike Nawrocki #ifdef TARGET_PPC64 23*356bb70eSMike Nawrocki #define ELFCLASS ELFCLASS64 24*356bb70eSMike Nawrocki #define cpu_to_dump_reg cpu_to_dump64 25*356bb70eSMike Nawrocki typedef uint64_t reg_t; 26*356bb70eSMike Nawrocki typedef Elf64_Nhdr Elf_Nhdr; 27*356bb70eSMike Nawrocki #else 28*356bb70eSMike Nawrocki #define ELFCLASS ELFCLASS32 29*356bb70eSMike Nawrocki #define cpu_to_dump_reg cpu_to_dump32 30*356bb70eSMike Nawrocki typedef uint32_t reg_t; 31*356bb70eSMike Nawrocki typedef Elf32_Nhdr Elf_Nhdr; 32*356bb70eSMike Nawrocki #endif /* TARGET_PPC64 */ 33*356bb70eSMike Nawrocki 34*356bb70eSMike Nawrocki struct PPCUserRegStruct { 35*356bb70eSMike Nawrocki reg_t gpr[32]; 36*356bb70eSMike Nawrocki reg_t nip; 37*356bb70eSMike Nawrocki reg_t msr; 38*356bb70eSMike Nawrocki reg_t orig_gpr3; 39*356bb70eSMike Nawrocki reg_t ctr; 40*356bb70eSMike Nawrocki reg_t link; 41*356bb70eSMike Nawrocki reg_t xer; 42*356bb70eSMike Nawrocki reg_t ccr; 43*356bb70eSMike Nawrocki reg_t softe; 44*356bb70eSMike Nawrocki reg_t trap; 45*356bb70eSMike Nawrocki reg_t dar; 46*356bb70eSMike Nawrocki reg_t dsisr; 47*356bb70eSMike Nawrocki reg_t result; 48e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 49e62fbc54SAneesh Kumar K.V 50*356bb70eSMike Nawrocki struct PPCElfPrstatus { 51e62fbc54SAneesh Kumar K.V char pad1[112]; 52*356bb70eSMike Nawrocki struct PPCUserRegStruct pr_reg; 53*356bb70eSMike Nawrocki reg_t pad2[4]; 54e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 55e62fbc54SAneesh Kumar K.V 56e62fbc54SAneesh Kumar K.V 57*356bb70eSMike Nawrocki struct PPCElfFpregset { 58e62fbc54SAneesh Kumar K.V uint64_t fpr[32]; 59*356bb70eSMike Nawrocki reg_t fpscr; 60e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 61e62fbc54SAneesh Kumar K.V 62e62fbc54SAneesh Kumar K.V 63*356bb70eSMike Nawrocki struct PPCElfVmxregset { 64e62fbc54SAneesh Kumar K.V ppc_avr_t avr[32]; 65e62fbc54SAneesh Kumar K.V ppc_avr_t vscr; 66e62fbc54SAneesh Kumar K.V union { 67e62fbc54SAneesh Kumar K.V ppc_avr_t unused; 68e62fbc54SAneesh Kumar K.V uint32_t value; 69e62fbc54SAneesh Kumar K.V } vrsave; 70e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 71e62fbc54SAneesh Kumar K.V 72*356bb70eSMike Nawrocki struct PPCElfVsxregset { 73e62fbc54SAneesh Kumar K.V uint64_t vsr[32]; 74e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 75e62fbc54SAneesh Kumar K.V 76*356bb70eSMike Nawrocki struct PPCElfSperegset { 77e62fbc54SAneesh Kumar K.V uint32_t evr[32]; 78e62fbc54SAneesh Kumar K.V uint64_t spe_acc; 79e62fbc54SAneesh Kumar K.V uint32_t spe_fscr; 80e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 81e62fbc54SAneesh Kumar K.V 82e62fbc54SAneesh Kumar K.V typedef struct noteStruct { 83*356bb70eSMike Nawrocki Elf_Nhdr hdr; 84e62fbc54SAneesh Kumar K.V char name[5]; 85e62fbc54SAneesh Kumar K.V char pad3[3]; 86e62fbc54SAneesh Kumar K.V union { 87*356bb70eSMike Nawrocki struct PPCElfPrstatus prstatus; 88*356bb70eSMike Nawrocki struct PPCElfFpregset fpregset; 89*356bb70eSMike Nawrocki struct PPCElfVmxregset vmxregset; 90*356bb70eSMike Nawrocki struct PPCElfVsxregset vsxregset; 91*356bb70eSMike Nawrocki struct PPCElfSperegset speregset; 92e62fbc54SAneesh Kumar K.V } contents; 93e62fbc54SAneesh Kumar K.V } QEMU_PACKED Note; 94e62fbc54SAneesh Kumar K.V 950c967de9SBharata B Rao typedef struct NoteFuncArg { 960c967de9SBharata B Rao Note note; 970c967de9SBharata B Rao DumpState *state; 980c967de9SBharata B Rao } NoteFuncArg; 99e62fbc54SAneesh Kumar K.V 100*356bb70eSMike Nawrocki static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu) 101e62fbc54SAneesh Kumar K.V { 102e62fbc54SAneesh Kumar K.V int i; 103*356bb70eSMike Nawrocki reg_t cr; 104*356bb70eSMike Nawrocki struct PPCElfPrstatus *prstatus; 105*356bb70eSMike Nawrocki struct PPCUserRegStruct *reg; 1060c967de9SBharata B Rao Note *note = &arg->note; 1070c967de9SBharata B Rao DumpState *s = arg->state; 108e62fbc54SAneesh Kumar K.V 1090c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS); 110e62fbc54SAneesh Kumar K.V 111e62fbc54SAneesh Kumar K.V prstatus = ¬e->contents.prstatus; 112e62fbc54SAneesh Kumar K.V memset(prstatus, 0, sizeof(*prstatus)); 113e62fbc54SAneesh Kumar K.V reg = &prstatus->pr_reg; 114e62fbc54SAneesh Kumar K.V 115e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 116*356bb70eSMike Nawrocki reg->gpr[i] = cpu_to_dump_reg(s, cpu->env.gpr[i]); 117e62fbc54SAneesh Kumar K.V } 118*356bb70eSMike Nawrocki reg->nip = cpu_to_dump_reg(s, cpu->env.nip); 119*356bb70eSMike Nawrocki reg->msr = cpu_to_dump_reg(s, cpu->env.msr); 120*356bb70eSMike Nawrocki reg->ctr = cpu_to_dump_reg(s, cpu->env.ctr); 121*356bb70eSMike Nawrocki reg->link = cpu_to_dump_reg(s, cpu->env.lr); 122*356bb70eSMike Nawrocki reg->xer = cpu_to_dump_reg(s, cpu_read_xer(&cpu->env)); 123e62fbc54SAneesh Kumar K.V 124e62fbc54SAneesh Kumar K.V cr = 0; 125e62fbc54SAneesh Kumar K.V for (i = 0; i < 8; i++) { 126e62fbc54SAneesh Kumar K.V cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i)); 127e62fbc54SAneesh Kumar K.V } 128*356bb70eSMike Nawrocki reg->ccr = cpu_to_dump_reg(s, cr); 129e62fbc54SAneesh Kumar K.V } 130e62fbc54SAneesh Kumar K.V 131*356bb70eSMike Nawrocki static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu) 132e62fbc54SAneesh Kumar K.V { 133e62fbc54SAneesh Kumar K.V int i; 134*356bb70eSMike Nawrocki struct PPCElfFpregset *fpregset; 1350c967de9SBharata B Rao Note *note = &arg->note; 1360c967de9SBharata B Rao DumpState *s = arg->state; 137e62fbc54SAneesh Kumar K.V 1380c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG); 139e62fbc54SAneesh Kumar K.V 140e62fbc54SAneesh Kumar K.V fpregset = ¬e->contents.fpregset; 141e62fbc54SAneesh Kumar K.V memset(fpregset, 0, sizeof(*fpregset)); 142e62fbc54SAneesh Kumar K.V 143e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 1440c967de9SBharata B Rao fpregset->fpr[i] = cpu_to_dump64(s, cpu->env.fpr[i]); 145e62fbc54SAneesh Kumar K.V } 146*356bb70eSMike Nawrocki fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr); 147e62fbc54SAneesh Kumar K.V } 148e62fbc54SAneesh Kumar K.V 149*356bb70eSMike Nawrocki static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu) 150e62fbc54SAneesh Kumar K.V { 151e62fbc54SAneesh Kumar K.V int i; 152*356bb70eSMike Nawrocki struct PPCElfVmxregset *vmxregset; 1530c967de9SBharata B Rao Note *note = &arg->note; 1540c967de9SBharata B Rao DumpState *s = arg->state; 155e62fbc54SAneesh Kumar K.V 1560c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX); 157e62fbc54SAneesh Kumar K.V vmxregset = ¬e->contents.vmxregset; 158e62fbc54SAneesh Kumar K.V memset(vmxregset, 0, sizeof(*vmxregset)); 159e62fbc54SAneesh Kumar K.V 160e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 1610c967de9SBharata B Rao bool needs_byteswap; 1620c967de9SBharata B Rao 1630c967de9SBharata B Rao #ifdef HOST_WORDS_BIGENDIAN 1640c967de9SBharata B Rao needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB; 1650c967de9SBharata B Rao #else 1660c967de9SBharata B Rao needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB; 1670c967de9SBharata B Rao #endif 1680c967de9SBharata B Rao 1690c967de9SBharata B Rao if (needs_byteswap) { 1700c967de9SBharata B Rao vmxregset->avr[i].u64[0] = bswap64(cpu->env.avr[i].u64[1]); 1710c967de9SBharata B Rao vmxregset->avr[i].u64[1] = bswap64(cpu->env.avr[i].u64[0]); 1720c967de9SBharata B Rao } else { 1730c967de9SBharata B Rao vmxregset->avr[i].u64[0] = cpu->env.avr[i].u64[0]; 1740c967de9SBharata B Rao vmxregset->avr[i].u64[1] = cpu->env.avr[i].u64[1]; 175e62fbc54SAneesh Kumar K.V } 176e62fbc54SAneesh Kumar K.V } 1770c967de9SBharata B Rao vmxregset->vscr.u32[3] = cpu_to_dump32(s, cpu->env.vscr); 1780c967de9SBharata B Rao } 179*356bb70eSMike Nawrocki 180*356bb70eSMike Nawrocki static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu) 181e62fbc54SAneesh Kumar K.V { 182e62fbc54SAneesh Kumar K.V int i; 183*356bb70eSMike Nawrocki struct PPCElfVsxregset *vsxregset; 1840c967de9SBharata B Rao Note *note = &arg->note; 1850c967de9SBharata B Rao DumpState *s = arg->state; 186e62fbc54SAneesh Kumar K.V 1870c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX); 188e62fbc54SAneesh Kumar K.V vsxregset = ¬e->contents.vsxregset; 189e62fbc54SAneesh Kumar K.V memset(vsxregset, 0, sizeof(*vsxregset)); 190e62fbc54SAneesh Kumar K.V 191e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 1920c967de9SBharata B Rao vsxregset->vsr[i] = cpu_to_dump64(s, cpu->env.vsr[i]); 193e62fbc54SAneesh Kumar K.V } 194e62fbc54SAneesh Kumar K.V } 195*356bb70eSMike Nawrocki 196*356bb70eSMike Nawrocki static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu) 197e62fbc54SAneesh Kumar K.V { 198*356bb70eSMike Nawrocki struct PPCElfSperegset *speregset; 1990c967de9SBharata B Rao Note *note = &arg->note; 2000c967de9SBharata B Rao DumpState *s = arg->state; 2010c967de9SBharata B Rao 2020c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE); 203e62fbc54SAneesh Kumar K.V speregset = ¬e->contents.speregset; 204e62fbc54SAneesh Kumar K.V memset(speregset, 0, sizeof(*speregset)); 205e62fbc54SAneesh Kumar K.V 2060c967de9SBharata B Rao speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc); 2070c967de9SBharata B Rao speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr); 208e62fbc54SAneesh Kumar K.V } 209e62fbc54SAneesh Kumar K.V 210cfd54a04SStefan Weil static const struct NoteFuncDescStruct { 211e62fbc54SAneesh Kumar K.V int contents_size; 2120c967de9SBharata B Rao void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu); 213e62fbc54SAneesh Kumar K.V } note_func[] = { 214*356bb70eSMike Nawrocki {sizeof(((Note *)0)->contents.prstatus), ppc_write_elf_prstatus}, 215*356bb70eSMike Nawrocki {sizeof(((Note *)0)->contents.fpregset), ppc_write_elf_fpregset}, 216*356bb70eSMike Nawrocki {sizeof(((Note *)0)->contents.vmxregset), ppc_write_elf_vmxregset}, 217*356bb70eSMike Nawrocki {sizeof(((Note *)0)->contents.vsxregset), ppc_write_elf_vsxregset}, 218*356bb70eSMike Nawrocki {sizeof(((Note *)0)->contents.speregset), ppc_write_elf_speregset}, 219e62fbc54SAneesh Kumar K.V { 0, NULL} 220e62fbc54SAneesh Kumar K.V }; 221e62fbc54SAneesh Kumar K.V 222e62fbc54SAneesh Kumar K.V typedef struct NoteFuncDescStruct NoteFuncDesc; 223e62fbc54SAneesh Kumar K.V 224e62fbc54SAneesh Kumar K.V int cpu_get_dump_info(ArchDumpInfo *info, 225e62fbc54SAneesh Kumar K.V const struct GuestPhysBlockList *guest_phys_blocks) 226e62fbc54SAneesh Kumar K.V { 2271e6ed54eSBharata B Rao PowerPCCPU *cpu = POWERPC_CPU(first_cpu); 2281e6ed54eSBharata B Rao PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 2291e6ed54eSBharata B Rao 230*356bb70eSMike Nawrocki info->d_machine = PPC_ELF_MACHINE; 231*356bb70eSMike Nawrocki info->d_class = ELFCLASS; 232*356bb70eSMike Nawrocki 2331e6ed54eSBharata B Rao if ((*pcc->interrupts_big_endian)(cpu)) { 2341e6ed54eSBharata B Rao info->d_endian = ELFDATA2MSB; 2351e6ed54eSBharata B Rao } else { 2361e6ed54eSBharata B Rao info->d_endian = ELFDATA2LSB; 2371e6ed54eSBharata B Rao } 238760d88d1SLaurent Vivier /* 64KB is the max page size for pseries kernel */ 239760d88d1SLaurent Vivier if (strncmp(object_get_typename(qdev_get_machine()), 240760d88d1SLaurent Vivier "pseries-", 8) == 0) { 241760d88d1SLaurent Vivier info->page_size = (1U << 16); 242760d88d1SLaurent Vivier } 243e62fbc54SAneesh Kumar K.V 244e62fbc54SAneesh Kumar K.V return 0; 245e62fbc54SAneesh Kumar K.V } 246e62fbc54SAneesh Kumar K.V 247e62fbc54SAneesh Kumar K.V ssize_t cpu_get_note_size(int class, int machine, int nr_cpus) 248e62fbc54SAneesh Kumar K.V { 249e62fbc54SAneesh Kumar K.V int name_size = 8; /* "CORE" or "QEMU" rounded */ 250e62fbc54SAneesh Kumar K.V size_t elf_note_size = 0; 251e62fbc54SAneesh Kumar K.V int note_head_size; 252cfd54a04SStefan Weil const NoteFuncDesc *nf; 253e62fbc54SAneesh Kumar K.V 254*356bb70eSMike Nawrocki note_head_size = sizeof(Elf_Nhdr); 255e62fbc54SAneesh Kumar K.V for (nf = note_func; nf->note_contents_func; nf++) { 256e62fbc54SAneesh Kumar K.V elf_note_size = elf_note_size + note_head_size + name_size + 257e62fbc54SAneesh Kumar K.V nf->contents_size; 258e62fbc54SAneesh Kumar K.V } 259e62fbc54SAneesh Kumar K.V 260e62fbc54SAneesh Kumar K.V return (elf_note_size) * nr_cpus; 261e62fbc54SAneesh Kumar K.V } 262e62fbc54SAneesh Kumar K.V 263*356bb70eSMike Nawrocki static int ppc_write_all_elf_notes(const char *note_name, 264e62fbc54SAneesh Kumar K.V WriteCoreDumpFunction f, 265e62fbc54SAneesh Kumar K.V PowerPCCPU *cpu, int id, 266e62fbc54SAneesh Kumar K.V void *opaque) 267e62fbc54SAneesh Kumar K.V { 2680c967de9SBharata B Rao NoteFuncArg arg = { .state = opaque }; 269e62fbc54SAneesh Kumar K.V int ret = -1; 270e62fbc54SAneesh Kumar K.V int note_size; 271cfd54a04SStefan Weil const NoteFuncDesc *nf; 272e62fbc54SAneesh Kumar K.V 273e62fbc54SAneesh Kumar K.V for (nf = note_func; nf->note_contents_func; nf++) { 2740c967de9SBharata B Rao arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name)); 2750c967de9SBharata B Rao arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size); 2760c967de9SBharata B Rao strncpy(arg.note.name, note_name, sizeof(arg.note.name)); 277e62fbc54SAneesh Kumar K.V 2780c967de9SBharata B Rao (*nf->note_contents_func)(&arg, cpu); 279e62fbc54SAneesh Kumar K.V 2800c967de9SBharata B Rao note_size = 2810c967de9SBharata B Rao sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size; 2820c967de9SBharata B Rao ret = f(&arg.note, note_size, opaque); 283e62fbc54SAneesh Kumar K.V if (ret < 0) { 284e62fbc54SAneesh Kumar K.V return -1; 285e62fbc54SAneesh Kumar K.V } 286e62fbc54SAneesh Kumar K.V } 287e62fbc54SAneesh Kumar K.V return 0; 288e62fbc54SAneesh Kumar K.V } 289e62fbc54SAneesh Kumar K.V 290e62fbc54SAneesh Kumar K.V int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs, 291e62fbc54SAneesh Kumar K.V int cpuid, void *opaque) 292e62fbc54SAneesh Kumar K.V { 293e62fbc54SAneesh Kumar K.V PowerPCCPU *cpu = POWERPC_CPU(cs); 294*356bb70eSMike Nawrocki return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque); 295*356bb70eSMike Nawrocki } 296*356bb70eSMike Nawrocki 297*356bb70eSMike Nawrocki int ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, 298*356bb70eSMike Nawrocki int cpuid, void *opaque) 299*356bb70eSMike Nawrocki { 300*356bb70eSMike Nawrocki PowerPCCPU *cpu = POWERPC_CPU(cs); 301*356bb70eSMike Nawrocki return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque); 302e62fbc54SAneesh Kumar K.V } 303