]> git.proxmox.com Git - mirror_qemu.git/blame - target/ppc/arch_dump.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / target / ppc / arch_dump.c
CommitLineData
e62fbc54 1/*
356bb70e 2 * writing ELF notes for ppc{64,} arch
e62fbc54
AK
3 *
4 *
5 * Copyright IBM, Corp. 2013
6 *
7 * Authors:
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
12 *
13 */
14
0d75590d 15#include "qemu/osdep.h"
e62fbc54
AK
16#include "cpu.h"
17#include "elf.h"
e62fbc54
AK
18#include "sysemu/dump.h"
19#include "sysemu/kvm.h"
20
356bb70e
MN
21#ifdef TARGET_PPC64
22#define ELFCLASS ELFCLASS64
23#define cpu_to_dump_reg cpu_to_dump64
24typedef uint64_t reg_t;
25typedef Elf64_Nhdr Elf_Nhdr;
26#else
27#define ELFCLASS ELFCLASS32
28#define cpu_to_dump_reg cpu_to_dump32
29typedef uint32_t reg_t;
30typedef Elf32_Nhdr Elf_Nhdr;
31#endif /* TARGET_PPC64 */
32
33struct PPCUserRegStruct {
34 reg_t gpr[32];
35 reg_t nip;
36 reg_t msr;
37 reg_t orig_gpr3;
38 reg_t ctr;
39 reg_t link;
40 reg_t xer;
41 reg_t ccr;
42 reg_t softe;
43 reg_t trap;
44 reg_t dar;
45 reg_t dsisr;
46 reg_t result;
e62fbc54
AK
47} QEMU_PACKED;
48
356bb70e 49struct PPCElfPrstatus {
e62fbc54 50 char pad1[112];
356bb70e 51 struct PPCUserRegStruct pr_reg;
b88290cd 52 char pad2[40];
e62fbc54
AK
53} QEMU_PACKED;
54
55
356bb70e 56struct PPCElfFpregset {
e62fbc54 57 uint64_t fpr[32];
356bb70e 58 reg_t fpscr;
e62fbc54
AK
59} QEMU_PACKED;
60
61
356bb70e 62struct PPCElfVmxregset {
e62fbc54
AK
63 ppc_avr_t avr[32];
64 ppc_avr_t vscr;
65 union {
66 ppc_avr_t unused;
67 uint32_t value;
68 } vrsave;
69} QEMU_PACKED;
70
356bb70e 71struct PPCElfVsxregset {
e62fbc54
AK
72 uint64_t vsr[32];
73} QEMU_PACKED;
74
356bb70e 75struct PPCElfSperegset {
e62fbc54
AK
76 uint32_t evr[32];
77 uint64_t spe_acc;
78 uint32_t spe_fscr;
79} QEMU_PACKED;
80
81typedef struct noteStruct {
356bb70e 82 Elf_Nhdr hdr;
e62fbc54
AK
83 char name[5];
84 char pad3[3];
85 union {
356bb70e
MN
86 struct PPCElfPrstatus prstatus;
87 struct PPCElfFpregset fpregset;
88 struct PPCElfVmxregset vmxregset;
89 struct PPCElfVsxregset vsxregset;
90 struct PPCElfSperegset speregset;
e62fbc54
AK
91 } contents;
92} QEMU_PACKED Note;
93
0c967de9
BR
94typedef struct NoteFuncArg {
95 Note note;
96 DumpState *state;
97} NoteFuncArg;
e62fbc54 98
356bb70e 99static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
e62fbc54
AK
100{
101 int i;
356bb70e
MN
102 reg_t cr;
103 struct PPCElfPrstatus *prstatus;
104 struct PPCUserRegStruct *reg;
0c967de9
BR
105 Note *note = &arg->note;
106 DumpState *s = arg->state;
e62fbc54 107
0c967de9 108 note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS);
e62fbc54
AK
109
110 prstatus = &note->contents.prstatus;
111 memset(prstatus, 0, sizeof(*prstatus));
112 reg = &prstatus->pr_reg;
113
114 for (i = 0; i < 32; i++) {
356bb70e 115 reg->gpr[i] = cpu_to_dump_reg(s, cpu->env.gpr[i]);
e62fbc54 116 }
356bb70e
MN
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));
e62fbc54
AK
122
123 cr = 0;
124 for (i = 0; i < 8; i++) {
125 cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
126 }
356bb70e 127 reg->ccr = cpu_to_dump_reg(s, cr);
e62fbc54
AK
128}
129
356bb70e 130static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
e62fbc54
AK
131{
132 int i;
356bb70e 133 struct PPCElfFpregset *fpregset;
0c967de9
BR
134 Note *note = &arg->note;
135 DumpState *s = arg->state;
e62fbc54 136
0c967de9 137 note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG);
e62fbc54
AK
138
139 fpregset = &note->contents.fpregset;
140 memset(fpregset, 0, sizeof(*fpregset));
141
142 for (i = 0; i < 32; i++) {
0c967de9 143 fpregset->fpr[i] = cpu_to_dump64(s, cpu->env.fpr[i]);
e62fbc54 144 }
356bb70e 145 fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
e62fbc54
AK
146}
147
356bb70e 148static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
e62fbc54
AK
149{
150 int i;
356bb70e 151 struct PPCElfVmxregset *vmxregset;
0c967de9
BR
152 Note *note = &arg->note;
153 DumpState *s = arg->state;
e62fbc54 154
0c967de9 155 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
e62fbc54
AK
156 vmxregset = &note->contents.vmxregset;
157 memset(vmxregset, 0, sizeof(*vmxregset));
158
159 for (i = 0; i < 32; i++) {
0c967de9
BR
160 bool needs_byteswap;
161
162#ifdef HOST_WORDS_BIGENDIAN
163 needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
164#else
165 needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
166#endif
167
168 if (needs_byteswap) {
169 vmxregset->avr[i].u64[0] = bswap64(cpu->env.avr[i].u64[1]);
170 vmxregset->avr[i].u64[1] = bswap64(cpu->env.avr[i].u64[0]);
171 } else {
172 vmxregset->avr[i].u64[0] = cpu->env.avr[i].u64[0];
173 vmxregset->avr[i].u64[1] = cpu->env.avr[i].u64[1];
174 }
e62fbc54 175 }
0c967de9 176 vmxregset->vscr.u32[3] = cpu_to_dump32(s, cpu->env.vscr);
e62fbc54 177}
356bb70e
MN
178
179static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
e62fbc54
AK
180{
181 int i;
356bb70e 182 struct PPCElfVsxregset *vsxregset;
0c967de9
BR
183 Note *note = &arg->note;
184 DumpState *s = arg->state;
e62fbc54 185
0c967de9 186 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
e62fbc54
AK
187 vsxregset = &note->contents.vsxregset;
188 memset(vsxregset, 0, sizeof(*vsxregset));
189
190 for (i = 0; i < 32; i++) {
0c967de9 191 vsxregset->vsr[i] = cpu_to_dump64(s, cpu->env.vsr[i]);
e62fbc54
AK
192 }
193}
356bb70e
MN
194
195static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
e62fbc54 196{
356bb70e 197 struct PPCElfSperegset *speregset;
0c967de9
BR
198 Note *note = &arg->note;
199 DumpState *s = arg->state;
200
201 note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
e62fbc54
AK
202 speregset = &note->contents.speregset;
203 memset(speregset, 0, sizeof(*speregset));
204
0c967de9
BR
205 speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
206 speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
e62fbc54
AK
207}
208
cfd54a04 209static const struct NoteFuncDescStruct {
e62fbc54 210 int contents_size;
0c967de9 211 void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
e62fbc54 212} note_func[] = {
f18793b0
SH
213 {sizeof_field(Note, contents.prstatus), ppc_write_elf_prstatus},
214 {sizeof_field(Note, contents.fpregset), ppc_write_elf_fpregset},
215 {sizeof_field(Note, contents.vmxregset), ppc_write_elf_vmxregset},
216 {sizeof_field(Note, contents.vsxregset), ppc_write_elf_vsxregset},
217 {sizeof_field(Note, contents.speregset), ppc_write_elf_speregset},
e62fbc54
AK
218 { 0, NULL}
219};
220
221typedef struct NoteFuncDescStruct NoteFuncDesc;
222
223int cpu_get_dump_info(ArchDumpInfo *info,
224 const struct GuestPhysBlockList *guest_phys_blocks)
225{
b1fde1ef
LV
226 PowerPCCPU *cpu;
227 PowerPCCPUClass *pcc;
228
229 if (first_cpu == NULL) {
230 return -1;
231 }
232
233 cpu = POWERPC_CPU(first_cpu);
234 pcc = POWERPC_CPU_GET_CLASS(cpu);
1e6ed54e 235
356bb70e
MN
236 info->d_machine = PPC_ELF_MACHINE;
237 info->d_class = ELFCLASS;
238
1e6ed54e
BR
239 if ((*pcc->interrupts_big_endian)(cpu)) {
240 info->d_endian = ELFDATA2MSB;
241 } else {
242 info->d_endian = ELFDATA2LSB;
243 }
760d88d1
LV
244 /* 64KB is the max page size for pseries kernel */
245 if (strncmp(object_get_typename(qdev_get_machine()),
246 "pseries-", 8) == 0) {
247 info->page_size = (1U << 16);
248 }
e62fbc54
AK
249
250 return 0;
251}
252
253ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
254{
255 int name_size = 8; /* "CORE" or "QEMU" rounded */
256 size_t elf_note_size = 0;
257 int note_head_size;
cfd54a04 258 const NoteFuncDesc *nf;
e62fbc54 259
356bb70e 260 note_head_size = sizeof(Elf_Nhdr);
e62fbc54
AK
261 for (nf = note_func; nf->note_contents_func; nf++) {
262 elf_note_size = elf_note_size + note_head_size + name_size +
356bb70e 263 nf->contents_size;
e62fbc54
AK
264 }
265
266 return (elf_note_size) * nr_cpus;
267}
268
356bb70e
MN
269static int ppc_write_all_elf_notes(const char *note_name,
270 WriteCoreDumpFunction f,
271 PowerPCCPU *cpu, int id,
272 void *opaque)
e62fbc54 273{
0c967de9 274 NoteFuncArg arg = { .state = opaque };
e62fbc54
AK
275 int ret = -1;
276 int note_size;
cfd54a04 277 const NoteFuncDesc *nf;
e62fbc54
AK
278
279 for (nf = note_func; nf->note_contents_func; nf++) {
0c967de9
BR
280 arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name));
281 arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size);
282 strncpy(arg.note.name, note_name, sizeof(arg.note.name));
e62fbc54 283
0c967de9 284 (*nf->note_contents_func)(&arg, cpu);
e62fbc54 285
0c967de9
BR
286 note_size =
287 sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
288 ret = f(&arg.note, note_size, opaque);
e62fbc54
AK
289 if (ret < 0) {
290 return -1;
291 }
292 }
293 return 0;
294}
295
296int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
297 int cpuid, void *opaque)
298{
299 PowerPCCPU *cpu = POWERPC_CPU(cs);
356bb70e
MN
300 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);
301}
302
303int ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
304 int cpuid, void *opaque)
305{
306 PowerPCCPU *cpu = POWERPC_CPU(cs);
307 return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);
e62fbc54 308}