target-ppc: Add missing 'static' and 'const' attributes
[qemu.git] / 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
83 static void ppc64_write_elf64_prstatus(Note *note, PowerPCCPU *cpu)
84 {
85 int i;
86 uint64_t cr;
87 struct PPC64ElfPrstatus *prstatus;
88 struct PPC64UserRegStruct *reg;
89
90 note->hdr.n_type = cpu_to_be32(NT_PRSTATUS);
91
92 prstatus = &note->contents.prstatus;
93 memset(prstatus, 0, sizeof(*prstatus));
94 reg = &prstatus->pr_reg;
95
96 for (i = 0; i < 32; i++) {
97 reg->gpr[i] = cpu_to_be64(cpu->env.gpr[i]);
98 }
99 reg->nip = cpu_to_be64(cpu->env.nip);
100 reg->msr = cpu_to_be64(cpu->env.msr);
101 reg->ctr = cpu_to_be64(cpu->env.ctr);
102 reg->link = cpu_to_be64(cpu->env.lr);
103 reg->xer = cpu_to_be64(cpu_read_xer(&cpu->env));
104
105 cr = 0;
106 for (i = 0; i < 8; i++) {
107 cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
108 }
109 reg->ccr = cpu_to_be64(cr);
110 }
111
112 static void ppc64_write_elf64_fpregset(Note *note, PowerPCCPU *cpu)
113 {
114 int i;
115 struct PPC64ElfFpregset *fpregset;
116
117 note->hdr.n_type = cpu_to_be32(NT_PRFPREG);
118
119 fpregset = &note->contents.fpregset;
120 memset(fpregset, 0, sizeof(*fpregset));
121
122 for (i = 0; i < 32; i++) {
123 fpregset->fpr[i] = cpu_to_be64(cpu->env.fpr[i]);
124 }
125 fpregset->fpscr = cpu_to_be64(cpu->env.fpscr);
126 }
127
128 static void ppc64_write_elf64_vmxregset(Note *note, PowerPCCPU *cpu)
129 {
130 int i;
131 struct PPC64ElfVmxregset *vmxregset;
132
133 note->hdr.n_type = cpu_to_be32(NT_PPC_VMX);
134 vmxregset = &note->contents.vmxregset;
135 memset(vmxregset, 0, sizeof(*vmxregset));
136
137 for (i = 0; i < 32; i++) {
138 vmxregset->avr[i].u64[0] = cpu_to_be64(cpu->env.avr[i].u64[0]);
139 vmxregset->avr[i].u64[1] = cpu_to_be64(cpu->env.avr[i].u64[1]);
140 }
141 vmxregset->vscr.u32[3] = cpu_to_be32(cpu->env.vscr);
142 }
143 static void ppc64_write_elf64_vsxregset(Note *note, PowerPCCPU *cpu)
144 {
145 int i;
146 struct PPC64ElfVsxregset *vsxregset;
147
148 note->hdr.n_type = cpu_to_be32(NT_PPC_VSX);
149 vsxregset = &note->contents.vsxregset;
150 memset(vsxregset, 0, sizeof(*vsxregset));
151
152 for (i = 0; i < 32; i++) {
153 vsxregset->vsr[i] = cpu_to_be64(cpu->env.vsr[i]);
154 }
155 }
156 static void ppc64_write_elf64_speregset(Note *note, PowerPCCPU *cpu)
157 {
158 struct PPC64ElfSperegset *speregset;
159 note->hdr.n_type = cpu_to_be32(NT_PPC_SPE);
160 speregset = &note->contents.speregset;
161 memset(speregset, 0, sizeof(*speregset));
162
163 speregset->spe_acc = cpu_to_be64(cpu->env.spe_acc);
164 speregset->spe_fscr = cpu_to_be32(cpu->env.spe_fscr);
165 }
166
167 static const struct NoteFuncDescStruct {
168 int contents_size;
169 void (*note_contents_func)(Note *note, PowerPCCPU *cpu);
170 } note_func[] = {
171 {sizeof(((Note *)0)->contents.prstatus), ppc64_write_elf64_prstatus},
172 {sizeof(((Note *)0)->contents.fpregset), ppc64_write_elf64_fpregset},
173 {sizeof(((Note *)0)->contents.vmxregset), ppc64_write_elf64_vmxregset},
174 {sizeof(((Note *)0)->contents.vsxregset), ppc64_write_elf64_vsxregset},
175 {sizeof(((Note *)0)->contents.speregset), ppc64_write_elf64_speregset},
176 { 0, NULL}
177 };
178
179 typedef struct NoteFuncDescStruct NoteFuncDesc;
180
181 int cpu_get_dump_info(ArchDumpInfo *info,
182 const struct GuestPhysBlockList *guest_phys_blocks)
183 {
184 /*
185 * Currently only handling PPC64 big endian.
186 */
187 info->d_machine = EM_PPC64;
188 info->d_endian = ELFDATA2MSB;
189 info->d_class = ELFCLASS64;
190
191 return 0;
192 }
193
194 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
195 {
196 int name_size = 8; /* "CORE" or "QEMU" rounded */
197 size_t elf_note_size = 0;
198 int note_head_size;
199 const NoteFuncDesc *nf;
200
201 if (class != ELFCLASS64) {
202 return -1;
203 }
204 assert(machine == EM_PPC64);
205
206 note_head_size = sizeof(Elf64_Nhdr);
207
208 for (nf = note_func; nf->note_contents_func; nf++) {
209 elf_note_size = elf_note_size + note_head_size + name_size +
210 nf->contents_size;
211 }
212
213 return (elf_note_size) * nr_cpus;
214 }
215
216 static int ppc64_write_all_elf64_notes(const char *note_name,
217 WriteCoreDumpFunction f,
218 PowerPCCPU *cpu, int id,
219 void *opaque)
220 {
221 Note note;
222 int ret = -1;
223 int note_size;
224 const NoteFuncDesc *nf;
225
226 for (nf = note_func; nf->note_contents_func; nf++) {
227 note.hdr.n_namesz = cpu_to_be32(sizeof(note.name));
228 note.hdr.n_descsz = cpu_to_be32(nf->contents_size);
229 strncpy(note.name, note_name, sizeof(note.name));
230
231 (*nf->note_contents_func)(&note, cpu);
232
233 note_size = sizeof(note) - sizeof(note.contents) + nf->contents_size;
234 ret = f(&note, note_size, opaque);
235 if (ret < 0) {
236 return -1;
237 }
238 }
239 return 0;
240 }
241
242 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
243 int cpuid, void *opaque)
244 {
245 PowerPCCPU *cpu = POWERPC_CPU(cs);
246 return ppc64_write_all_elf64_notes("CORE", f, cpu, cpuid, opaque);
247 }
248
249 int ppc64_cpu_write_elf64_qemunote(WriteCoreDumpFunction f,
250 CPUState *cpu, void *opaque)
251 {
252 return 0;
253 }