1e62fbc54SAneesh Kumar K.V /* 2e62fbc54SAneesh Kumar K.V * writing ELF notes for ppc64 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 22e62fbc54SAneesh Kumar K.V struct PPC64UserRegStruct { 23e62fbc54SAneesh Kumar K.V uint64_t gpr[32]; 24e62fbc54SAneesh Kumar K.V uint64_t nip; 25e62fbc54SAneesh Kumar K.V uint64_t msr; 26e62fbc54SAneesh Kumar K.V uint64_t orig_gpr3; 27e62fbc54SAneesh Kumar K.V uint64_t ctr; 28e62fbc54SAneesh Kumar K.V uint64_t link; 29e62fbc54SAneesh Kumar K.V uint64_t xer; 30e62fbc54SAneesh Kumar K.V uint64_t ccr; 31e62fbc54SAneesh Kumar K.V uint64_t softe; 32e62fbc54SAneesh Kumar K.V uint64_t trap; 33e62fbc54SAneesh Kumar K.V uint64_t dar; 34e62fbc54SAneesh Kumar K.V uint64_t dsisr; 35e62fbc54SAneesh Kumar K.V uint64_t result; 36e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 37e62fbc54SAneesh Kumar K.V 38e62fbc54SAneesh Kumar K.V struct PPC64ElfPrstatus { 39e62fbc54SAneesh Kumar K.V char pad1[112]; 40e62fbc54SAneesh Kumar K.V struct PPC64UserRegStruct pr_reg; 41e62fbc54SAneesh Kumar K.V uint64_t pad2[4]; 42e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 43e62fbc54SAneesh Kumar K.V 44e62fbc54SAneesh Kumar K.V 45e62fbc54SAneesh Kumar K.V struct PPC64ElfFpregset { 46e62fbc54SAneesh Kumar K.V uint64_t fpr[32]; 47e62fbc54SAneesh Kumar K.V uint64_t fpscr; 48e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 49e62fbc54SAneesh Kumar K.V 50e62fbc54SAneesh Kumar K.V 51e62fbc54SAneesh Kumar K.V struct PPC64ElfVmxregset { 52e62fbc54SAneesh Kumar K.V ppc_avr_t avr[32]; 53e62fbc54SAneesh Kumar K.V ppc_avr_t vscr; 54e62fbc54SAneesh Kumar K.V union { 55e62fbc54SAneesh Kumar K.V ppc_avr_t unused; 56e62fbc54SAneesh Kumar K.V uint32_t value; 57e62fbc54SAneesh Kumar K.V } vrsave; 58e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 59e62fbc54SAneesh Kumar K.V 60e62fbc54SAneesh Kumar K.V struct PPC64ElfVsxregset { 61e62fbc54SAneesh Kumar K.V uint64_t vsr[32]; 62e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 63e62fbc54SAneesh Kumar K.V 64e62fbc54SAneesh Kumar K.V struct PPC64ElfSperegset { 65e62fbc54SAneesh Kumar K.V uint32_t evr[32]; 66e62fbc54SAneesh Kumar K.V uint64_t spe_acc; 67e62fbc54SAneesh Kumar K.V uint32_t spe_fscr; 68e62fbc54SAneesh Kumar K.V } QEMU_PACKED; 69e62fbc54SAneesh Kumar K.V 70e62fbc54SAneesh Kumar K.V typedef struct noteStruct { 71e62fbc54SAneesh Kumar K.V Elf64_Nhdr hdr; 72e62fbc54SAneesh Kumar K.V char name[5]; 73e62fbc54SAneesh Kumar K.V char pad3[3]; 74e62fbc54SAneesh Kumar K.V union { 75e62fbc54SAneesh Kumar K.V struct PPC64ElfPrstatus prstatus; 76e62fbc54SAneesh Kumar K.V struct PPC64ElfFpregset fpregset; 77e62fbc54SAneesh Kumar K.V struct PPC64ElfVmxregset vmxregset; 78e62fbc54SAneesh Kumar K.V struct PPC64ElfVsxregset vsxregset; 79e62fbc54SAneesh Kumar K.V struct PPC64ElfSperegset speregset; 80e62fbc54SAneesh Kumar K.V } contents; 81e62fbc54SAneesh Kumar K.V } QEMU_PACKED Note; 82e62fbc54SAneesh Kumar K.V 830c967de9SBharata B Rao typedef struct NoteFuncArg { 840c967de9SBharata B Rao Note note; 850c967de9SBharata B Rao DumpState *state; 860c967de9SBharata B Rao } NoteFuncArg; 87e62fbc54SAneesh Kumar K.V 880c967de9SBharata B Rao static void ppc64_write_elf64_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu) 89e62fbc54SAneesh Kumar K.V { 90e62fbc54SAneesh Kumar K.V int i; 91e62fbc54SAneesh Kumar K.V uint64_t cr; 92e62fbc54SAneesh Kumar K.V struct PPC64ElfPrstatus *prstatus; 93e62fbc54SAneesh Kumar K.V struct PPC64UserRegStruct *reg; 940c967de9SBharata B Rao Note *note = &arg->note; 950c967de9SBharata B Rao DumpState *s = arg->state; 96e62fbc54SAneesh Kumar K.V 970c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS); 98e62fbc54SAneesh Kumar K.V 99e62fbc54SAneesh Kumar K.V prstatus = ¬e->contents.prstatus; 100e62fbc54SAneesh Kumar K.V memset(prstatus, 0, sizeof(*prstatus)); 101e62fbc54SAneesh Kumar K.V reg = &prstatus->pr_reg; 102e62fbc54SAneesh Kumar K.V 103e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 1040c967de9SBharata B Rao reg->gpr[i] = cpu_to_dump64(s, cpu->env.gpr[i]); 105e62fbc54SAneesh Kumar K.V } 1060c967de9SBharata B Rao reg->nip = cpu_to_dump64(s, cpu->env.nip); 1070c967de9SBharata B Rao reg->msr = cpu_to_dump64(s, cpu->env.msr); 1080c967de9SBharata B Rao reg->ctr = cpu_to_dump64(s, cpu->env.ctr); 1090c967de9SBharata B Rao reg->link = cpu_to_dump64(s, cpu->env.lr); 1100c967de9SBharata B Rao reg->xer = cpu_to_dump64(s, cpu_read_xer(&cpu->env)); 111e62fbc54SAneesh Kumar K.V 112e62fbc54SAneesh Kumar K.V cr = 0; 113e62fbc54SAneesh Kumar K.V for (i = 0; i < 8; i++) { 114e62fbc54SAneesh Kumar K.V cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i)); 115e62fbc54SAneesh Kumar K.V } 1160c967de9SBharata B Rao reg->ccr = cpu_to_dump64(s, cr); 117e62fbc54SAneesh Kumar K.V } 118e62fbc54SAneesh Kumar K.V 1190c967de9SBharata B Rao static void ppc64_write_elf64_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu) 120e62fbc54SAneesh Kumar K.V { 121e62fbc54SAneesh Kumar K.V int i; 122e62fbc54SAneesh Kumar K.V struct PPC64ElfFpregset *fpregset; 1230c967de9SBharata B Rao Note *note = &arg->note; 1240c967de9SBharata B Rao DumpState *s = arg->state; 125e62fbc54SAneesh Kumar K.V 1260c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG); 127e62fbc54SAneesh Kumar K.V 128e62fbc54SAneesh Kumar K.V fpregset = ¬e->contents.fpregset; 129e62fbc54SAneesh Kumar K.V memset(fpregset, 0, sizeof(*fpregset)); 130e62fbc54SAneesh Kumar K.V 131e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 1320c967de9SBharata B Rao fpregset->fpr[i] = cpu_to_dump64(s, cpu->env.fpr[i]); 133e62fbc54SAneesh Kumar K.V } 1340c967de9SBharata B Rao fpregset->fpscr = cpu_to_dump64(s, cpu->env.fpscr); 135e62fbc54SAneesh Kumar K.V } 136e62fbc54SAneesh Kumar K.V 1370c967de9SBharata B Rao static void ppc64_write_elf64_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu) 138e62fbc54SAneesh Kumar K.V { 139e62fbc54SAneesh Kumar K.V int i; 140e62fbc54SAneesh Kumar K.V struct PPC64ElfVmxregset *vmxregset; 1410c967de9SBharata B Rao Note *note = &arg->note; 1420c967de9SBharata B Rao DumpState *s = arg->state; 143e62fbc54SAneesh Kumar K.V 1440c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX); 145e62fbc54SAneesh Kumar K.V vmxregset = ¬e->contents.vmxregset; 146e62fbc54SAneesh Kumar K.V memset(vmxregset, 0, sizeof(*vmxregset)); 147e62fbc54SAneesh Kumar K.V 148e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 1490c967de9SBharata B Rao bool needs_byteswap; 1500c967de9SBharata B Rao 1510c967de9SBharata B Rao #ifdef HOST_WORDS_BIGENDIAN 1520c967de9SBharata B Rao needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB; 1530c967de9SBharata B Rao #else 1540c967de9SBharata B Rao needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB; 1550c967de9SBharata B Rao #endif 1560c967de9SBharata B Rao 1570c967de9SBharata B Rao if (needs_byteswap) { 1580c967de9SBharata B Rao vmxregset->avr[i].u64[0] = bswap64(cpu->env.avr[i].u64[1]); 1590c967de9SBharata B Rao vmxregset->avr[i].u64[1] = bswap64(cpu->env.avr[i].u64[0]); 1600c967de9SBharata B Rao } else { 1610c967de9SBharata B Rao vmxregset->avr[i].u64[0] = cpu->env.avr[i].u64[0]; 1620c967de9SBharata B Rao vmxregset->avr[i].u64[1] = cpu->env.avr[i].u64[1]; 163e62fbc54SAneesh Kumar K.V } 164e62fbc54SAneesh Kumar K.V } 1650c967de9SBharata B Rao vmxregset->vscr.u32[3] = cpu_to_dump32(s, cpu->env.vscr); 1660c967de9SBharata B Rao } 1670c967de9SBharata B Rao static void ppc64_write_elf64_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu) 168e62fbc54SAneesh Kumar K.V { 169e62fbc54SAneesh Kumar K.V int i; 170e62fbc54SAneesh Kumar K.V struct PPC64ElfVsxregset *vsxregset; 1710c967de9SBharata B Rao Note *note = &arg->note; 1720c967de9SBharata B Rao DumpState *s = arg->state; 173e62fbc54SAneesh Kumar K.V 1740c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX); 175e62fbc54SAneesh Kumar K.V vsxregset = ¬e->contents.vsxregset; 176e62fbc54SAneesh Kumar K.V memset(vsxregset, 0, sizeof(*vsxregset)); 177e62fbc54SAneesh Kumar K.V 178e62fbc54SAneesh Kumar K.V for (i = 0; i < 32; i++) { 1790c967de9SBharata B Rao vsxregset->vsr[i] = cpu_to_dump64(s, cpu->env.vsr[i]); 180e62fbc54SAneesh Kumar K.V } 181e62fbc54SAneesh Kumar K.V } 1820c967de9SBharata B Rao static void ppc64_write_elf64_speregset(NoteFuncArg *arg, PowerPCCPU *cpu) 183e62fbc54SAneesh Kumar K.V { 184e62fbc54SAneesh Kumar K.V struct PPC64ElfSperegset *speregset; 1850c967de9SBharata B Rao Note *note = &arg->note; 1860c967de9SBharata B Rao DumpState *s = arg->state; 1870c967de9SBharata B Rao 1880c967de9SBharata B Rao note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE); 189e62fbc54SAneesh Kumar K.V speregset = ¬e->contents.speregset; 190e62fbc54SAneesh Kumar K.V memset(speregset, 0, sizeof(*speregset)); 191e62fbc54SAneesh Kumar K.V 1920c967de9SBharata B Rao speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc); 1930c967de9SBharata B Rao speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr); 194e62fbc54SAneesh Kumar K.V } 195e62fbc54SAneesh Kumar K.V 196cfd54a04SStefan Weil static const struct NoteFuncDescStruct { 197e62fbc54SAneesh Kumar K.V int contents_size; 1980c967de9SBharata B Rao void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu); 199e62fbc54SAneesh Kumar K.V } note_func[] = { 200e62fbc54SAneesh Kumar K.V {sizeof(((Note *)0)->contents.prstatus), ppc64_write_elf64_prstatus}, 201e62fbc54SAneesh Kumar K.V {sizeof(((Note *)0)->contents.fpregset), ppc64_write_elf64_fpregset}, 202e62fbc54SAneesh Kumar K.V {sizeof(((Note *)0)->contents.vmxregset), ppc64_write_elf64_vmxregset}, 203e62fbc54SAneesh Kumar K.V {sizeof(((Note *)0)->contents.vsxregset), ppc64_write_elf64_vsxregset}, 204e62fbc54SAneesh Kumar K.V {sizeof(((Note *)0)->contents.speregset), ppc64_write_elf64_speregset}, 205e62fbc54SAneesh Kumar K.V { 0, NULL} 206e62fbc54SAneesh Kumar K.V }; 207e62fbc54SAneesh Kumar K.V 208e62fbc54SAneesh Kumar K.V typedef struct NoteFuncDescStruct NoteFuncDesc; 209e62fbc54SAneesh Kumar K.V 210e62fbc54SAneesh Kumar K.V int cpu_get_dump_info(ArchDumpInfo *info, 211e62fbc54SAneesh Kumar K.V const struct GuestPhysBlockList *guest_phys_blocks) 212e62fbc54SAneesh Kumar K.V { 2131e6ed54eSBharata B Rao PowerPCCPU *cpu = POWERPC_CPU(first_cpu); 2141e6ed54eSBharata B Rao PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 2151e6ed54eSBharata B Rao 216e62fbc54SAneesh Kumar K.V info->d_machine = EM_PPC64; 217e62fbc54SAneesh Kumar K.V info->d_class = ELFCLASS64; 2181e6ed54eSBharata B Rao if ((*pcc->interrupts_big_endian)(cpu)) { 2191e6ed54eSBharata B Rao info->d_endian = ELFDATA2MSB; 2201e6ed54eSBharata B Rao } else { 2211e6ed54eSBharata B Rao info->d_endian = ELFDATA2LSB; 2221e6ed54eSBharata B Rao } 223760d88d1SLaurent Vivier /* 64KB is the max page size for pseries kernel */ 224760d88d1SLaurent Vivier if (strncmp(object_get_typename(qdev_get_machine()), 225760d88d1SLaurent Vivier "pseries-", 8) == 0) { 226760d88d1SLaurent Vivier info->page_size = (1U << 16); 227760d88d1SLaurent Vivier } 228e62fbc54SAneesh Kumar K.V 229e62fbc54SAneesh Kumar K.V return 0; 230e62fbc54SAneesh Kumar K.V } 231e62fbc54SAneesh Kumar K.V 232e62fbc54SAneesh Kumar K.V ssize_t cpu_get_note_size(int class, int machine, int nr_cpus) 233e62fbc54SAneesh Kumar K.V { 234e62fbc54SAneesh Kumar K.V int name_size = 8; /* "CORE" or "QEMU" rounded */ 235e62fbc54SAneesh Kumar K.V size_t elf_note_size = 0; 236e62fbc54SAneesh Kumar K.V int note_head_size; 237cfd54a04SStefan Weil const NoteFuncDesc *nf; 238e62fbc54SAneesh Kumar K.V 239e62fbc54SAneesh Kumar K.V if (class != ELFCLASS64) { 240e62fbc54SAneesh Kumar K.V return -1; 241e62fbc54SAneesh Kumar K.V } 242e62fbc54SAneesh Kumar K.V assert(machine == EM_PPC64); 243e62fbc54SAneesh Kumar K.V 244e62fbc54SAneesh Kumar K.V note_head_size = sizeof(Elf64_Nhdr); 245e62fbc54SAneesh Kumar K.V 246e62fbc54SAneesh Kumar K.V for (nf = note_func; nf->note_contents_func; nf++) { 247e62fbc54SAneesh Kumar K.V elf_note_size = elf_note_size + note_head_size + name_size + 248e62fbc54SAneesh Kumar K.V nf->contents_size; 249e62fbc54SAneesh Kumar K.V } 250e62fbc54SAneesh Kumar K.V 251e62fbc54SAneesh Kumar K.V return (elf_note_size) * nr_cpus; 252e62fbc54SAneesh Kumar K.V } 253e62fbc54SAneesh Kumar K.V 254e62fbc54SAneesh Kumar K.V static int ppc64_write_all_elf64_notes(const char *note_name, 255e62fbc54SAneesh Kumar K.V WriteCoreDumpFunction f, 256e62fbc54SAneesh Kumar K.V PowerPCCPU *cpu, int id, 257e62fbc54SAneesh Kumar K.V void *opaque) 258e62fbc54SAneesh Kumar K.V { 2590c967de9SBharata B Rao NoteFuncArg arg = { .state = opaque }; 260e62fbc54SAneesh Kumar K.V int ret = -1; 261e62fbc54SAneesh Kumar K.V int note_size; 262cfd54a04SStefan Weil const NoteFuncDesc *nf; 263e62fbc54SAneesh Kumar K.V 264e62fbc54SAneesh Kumar K.V for (nf = note_func; nf->note_contents_func; nf++) { 2650c967de9SBharata B Rao arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name)); 2660c967de9SBharata B Rao arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size); 2670c967de9SBharata B Rao strncpy(arg.note.name, note_name, sizeof(arg.note.name)); 268e62fbc54SAneesh Kumar K.V 2690c967de9SBharata B Rao (*nf->note_contents_func)(&arg, cpu); 270e62fbc54SAneesh Kumar K.V 2710c967de9SBharata B Rao note_size = 2720c967de9SBharata B Rao sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size; 2730c967de9SBharata B Rao ret = f(&arg.note, note_size, opaque); 274e62fbc54SAneesh Kumar K.V if (ret < 0) { 275e62fbc54SAneesh Kumar K.V return -1; 276e62fbc54SAneesh Kumar K.V } 277e62fbc54SAneesh Kumar K.V } 278e62fbc54SAneesh Kumar K.V return 0; 279e62fbc54SAneesh Kumar K.V } 280e62fbc54SAneesh Kumar K.V 281e62fbc54SAneesh Kumar K.V int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs, 282e62fbc54SAneesh Kumar K.V int cpuid, void *opaque) 283e62fbc54SAneesh Kumar K.V { 284e62fbc54SAneesh Kumar K.V PowerPCCPU *cpu = POWERPC_CPU(cs); 285e62fbc54SAneesh Kumar K.V return ppc64_write_all_elf64_notes("CORE", f, cpu, cpuid, opaque); 286e62fbc54SAneesh Kumar K.V } 287