Add qemu 2.4.0
[kvmfornfv.git] / qemu / target-ppc / arch_dump.c
1 /*
2  * writing ELF notes for ppc64 arch
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
15 #include "cpu.h"
16 #include "elf.h"
17 #include "exec/cpu-all.h"
18 #include "sysemu/dump.h"
19 #include "sysemu/kvm.h"
20
21 struct PPC64UserRegStruct {
22     uint64_t gpr[32];
23     uint64_t nip;
24     uint64_t msr;
25     uint64_t orig_gpr3;
26     uint64_t ctr;
27     uint64_t link;
28     uint64_t xer;
29     uint64_t ccr;
30     uint64_t softe;
31     uint64_t trap;
32     uint64_t dar;
33     uint64_t dsisr;
34     uint64_t result;
35 } QEMU_PACKED;
36
37 struct PPC64ElfPrstatus {
38     char pad1[112];
39     struct PPC64UserRegStruct pr_reg;
40     uint64_t pad2[4];
41 } QEMU_PACKED;
42
43
44 struct PPC64ElfFpregset {
45     uint64_t fpr[32];
46     uint64_t fpscr;
47 }  QEMU_PACKED;
48
49
50 struct PPC64ElfVmxregset {
51     ppc_avr_t avr[32];
52     ppc_avr_t vscr;
53     union {
54         ppc_avr_t unused;
55         uint32_t value;
56     } vrsave;
57 }  QEMU_PACKED;
58
59 struct PPC64ElfVsxregset {
60     uint64_t vsr[32];
61 }  QEMU_PACKED;
62
63 struct PPC64ElfSperegset {
64     uint32_t evr[32];
65     uint64_t spe_acc;
66     uint32_t spe_fscr;
67 }  QEMU_PACKED;
68
69 typedef struct noteStruct {
70     Elf64_Nhdr hdr;
71     char name[5];
72     char pad3[3];
73     union {
74         struct PPC64ElfPrstatus  prstatus;
75         struct PPC64ElfFpregset  fpregset;
76         struct PPC64ElfVmxregset vmxregset;
77         struct PPC64ElfVsxregset vsxregset;
78         struct PPC64ElfSperegset speregset;
79     } contents;
80 } QEMU_PACKED Note;
81
82 typedef struct NoteFuncArg {
83     Note note;
84     DumpState *state;
85 } NoteFuncArg;
86
87 static void ppc64_write_elf64_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
88 {
89     int i;
90     uint64_t cr;
91     struct PPC64ElfPrstatus *prstatus;
92     struct PPC64UserRegStruct *reg;
93     Note *note = &arg->note;
94     DumpState *s = arg->state;
95
96     note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS);
97
98     prstatus = &note->contents.prstatus;
99     memset(prstatus, 0, sizeof(*prstatus));
100     reg = &prstatus->pr_reg;
101
102     for (i = 0; i < 32; i++) {
103         reg->gpr[i] = cpu_to_dump64(s, cpu->env.gpr[i]);
104     }
105     reg->nip = cpu_to_dump64(s, cpu->env.nip);
106     reg->msr = cpu_to_dump64(s, cpu->env.msr);
107     reg->ctr = cpu_to_dump64(s, cpu->env.ctr);
108     reg->link = cpu_to_dump64(s, cpu->env.lr);
109     reg->xer = cpu_to_dump64(s, cpu_read_xer(&cpu->env));
110
111     cr = 0;
112     for (i = 0; i < 8; i++) {
113         cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
114     }
115     reg->ccr = cpu_to_dump64(s, cr);
116 }
117
118 static void ppc64_write_elf64_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
119 {
120     int i;
121     struct PPC64ElfFpregset  *fpregset;
122     Note *note = &arg->note;
123     DumpState *s = arg->state;
124
125     note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG);
126
127     fpregset = &note->contents.fpregset;
128     memset(fpregset, 0, sizeof(*fpregset));
129
130     for (i = 0; i < 32; i++) {
131         fpregset->fpr[i] = cpu_to_dump64(s, cpu->env.fpr[i]);
132     }
133     fpregset->fpscr = cpu_to_dump64(s, cpu->env.fpscr);
134 }
135
136 static void ppc64_write_elf64_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
137 {
138     int i;
139     struct PPC64ElfVmxregset *vmxregset;
140     Note *note = &arg->note;
141     DumpState *s = arg->state;
142
143     note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
144     vmxregset = &note->contents.vmxregset;
145     memset(vmxregset, 0, sizeof(*vmxregset));
146
147     for (i = 0; i < 32; i++) {
148         bool needs_byteswap;
149
150 #ifdef HOST_WORDS_BIGENDIAN
151         needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
152 #else
153         needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
154 #endif
155
156         if (needs_byteswap) {
157             vmxregset->avr[i].u64[0] = bswap64(cpu->env.avr[i].u64[1]);
158             vmxregset->avr[i].u64[1] = bswap64(cpu->env.avr[i].u64[0]);
159         } else {
160             vmxregset->avr[i].u64[0] = cpu->env.avr[i].u64[0];
161             vmxregset->avr[i].u64[1] = cpu->env.avr[i].u64[1];
162         }
163     }
164     vmxregset->vscr.u32[3] = cpu_to_dump32(s, cpu->env.vscr);
165 }
166 static void ppc64_write_elf64_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
167 {
168     int i;
169     struct PPC64ElfVsxregset *vsxregset;
170     Note *note = &arg->note;
171     DumpState *s = arg->state;
172
173     note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
174     vsxregset = &note->contents.vsxregset;
175     memset(vsxregset, 0, sizeof(*vsxregset));
176
177     for (i = 0; i < 32; i++) {
178         vsxregset->vsr[i] = cpu_to_dump64(s, cpu->env.vsr[i]);
179     }
180 }
181 static void ppc64_write_elf64_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
182 {
183     struct PPC64ElfSperegset *speregset;
184     Note *note = &arg->note;
185     DumpState *s = arg->state;
186
187     note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
188     speregset = &note->contents.speregset;
189     memset(speregset, 0, sizeof(*speregset));
190
191     speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
192     speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
193 }
194
195 static const struct NoteFuncDescStruct {
196     int contents_size;
197     void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
198 } note_func[] = {
199     {sizeof(((Note *)0)->contents.prstatus),  ppc64_write_elf64_prstatus},
200     {sizeof(((Note *)0)->contents.fpregset),  ppc64_write_elf64_fpregset},
201     {sizeof(((Note *)0)->contents.vmxregset), ppc64_write_elf64_vmxregset},
202     {sizeof(((Note *)0)->contents.vsxregset), ppc64_write_elf64_vsxregset},
203     {sizeof(((Note *)0)->contents.speregset), ppc64_write_elf64_speregset},
204     { 0, NULL}
205 };
206
207 typedef struct NoteFuncDescStruct NoteFuncDesc;
208
209 int cpu_get_dump_info(ArchDumpInfo *info,
210                       const struct GuestPhysBlockList *guest_phys_blocks)
211 {
212     PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
213     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
214
215     info->d_machine = EM_PPC64;
216     info->d_class = ELFCLASS64;
217     if ((*pcc->interrupts_big_endian)(cpu)) {
218         info->d_endian = ELFDATA2MSB;
219     } else {
220         info->d_endian = ELFDATA2LSB;
221     }
222
223     return 0;
224 }
225
226 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
227 {
228     int name_size = 8; /* "CORE" or "QEMU" rounded */
229     size_t elf_note_size = 0;
230     int note_head_size;
231     const NoteFuncDesc *nf;
232
233     if (class != ELFCLASS64) {
234         return -1;
235     }
236     assert(machine == EM_PPC64);
237
238     note_head_size = sizeof(Elf64_Nhdr);
239
240     for (nf = note_func; nf->note_contents_func; nf++) {
241         elf_note_size = elf_note_size + note_head_size + name_size +
242                         nf->contents_size;
243     }
244
245     return (elf_note_size) * nr_cpus;
246 }
247
248 static int ppc64_write_all_elf64_notes(const char *note_name,
249                                        WriteCoreDumpFunction f,
250                                        PowerPCCPU *cpu, int id,
251                                        void *opaque)
252 {
253     NoteFuncArg arg = { .state = opaque };
254     int ret = -1;
255     int note_size;
256     const NoteFuncDesc *nf;
257
258     for (nf = note_func; nf->note_contents_func; nf++) {
259         arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name));
260         arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size);
261         strncpy(arg.note.name, note_name, sizeof(arg.note.name));
262
263         (*nf->note_contents_func)(&arg, cpu);
264
265         note_size =
266             sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
267         ret = f(&arg.note, note_size, opaque);
268         if (ret < 0) {
269             return -1;
270         }
271     }
272     return 0;
273 }
274
275 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
276                                int cpuid, void *opaque)
277 {
278     PowerPCCPU *cpu = POWERPC_CPU(cs);
279     return ppc64_write_all_elf64_notes("CORE", f, cpu, cpuid, opaque);
280 }
281
282 int ppc64_cpu_write_elf64_qemunote(WriteCoreDumpFunction f,
283                                    CPUState *cpu, void *opaque)
284 {
285     return 0;
286 }