xref: /openbmc/qemu/target/ppc/arch_dump.c (revision f101c9fe)
1 /*
2  * writing ELF notes for ppc{64,} 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 "qemu/osdep.h"
16 #include "cpu.h"
17 #include "elf.h"
18 #include "sysemu/dump.h"
19 #include "sysemu/kvm.h"
20 
21 #ifdef TARGET_PPC64
22 #define ELFCLASS ELFCLASS64
23 #define cpu_to_dump_reg cpu_to_dump64
24 typedef uint64_t reg_t;
25 typedef Elf64_Nhdr Elf_Nhdr;
26 #else
27 #define ELFCLASS ELFCLASS32
28 #define cpu_to_dump_reg cpu_to_dump32
29 typedef uint32_t reg_t;
30 typedef Elf32_Nhdr Elf_Nhdr;
31 #endif /* TARGET_PPC64 */
32 
33 struct 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;
47 } QEMU_PACKED;
48 
49 struct PPCElfPrstatus {
50     char pad1[112];
51     struct PPCUserRegStruct pr_reg;
52     char pad2[40];
53 } QEMU_PACKED;
54 
55 
56 struct PPCElfFpregset {
57     uint64_t fpr[32];
58     reg_t fpscr;
59 }  QEMU_PACKED;
60 
61 
62 struct PPCElfVmxregset {
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 
71 struct PPCElfVsxregset {
72     uint64_t vsr[32];
73 }  QEMU_PACKED;
74 
75 struct PPCElfSperegset {
76     uint32_t evr[32];
77     uint64_t spe_acc;
78     uint32_t spe_fscr;
79 }  QEMU_PACKED;
80 
81 typedef struct noteStruct {
82     Elf_Nhdr hdr;
83     char name[5];
84     char pad3[3];
85     union {
86         struct PPCElfPrstatus  prstatus;
87         struct PPCElfFpregset  fpregset;
88         struct PPCElfVmxregset vmxregset;
89         struct PPCElfVsxregset vsxregset;
90         struct PPCElfSperegset speregset;
91     } contents;
92 } QEMU_PACKED Note;
93 
94 typedef struct NoteFuncArg {
95     Note note;
96     DumpState *state;
97 } NoteFuncArg;
98 
99 static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
100 {
101     int i;
102     reg_t cr;
103     struct PPCElfPrstatus *prstatus;
104     struct PPCUserRegStruct *reg;
105     Note *note = &arg->note;
106     DumpState *s = arg->state;
107 
108     note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS);
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++) {
115         reg->gpr[i] = cpu_to_dump_reg(s, cpu->env.gpr[i]);
116     }
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));
122 
123     cr = 0;
124     for (i = 0; i < 8; i++) {
125         cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i));
126     }
127     reg->ccr = cpu_to_dump_reg(s, cr);
128 }
129 
130 static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
131 {
132     int i;
133     struct PPCElfFpregset  *fpregset;
134     Note *note = &arg->note;
135     DumpState *s = arg->state;
136 
137     note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG);
138 
139     fpregset = &note->contents.fpregset;
140     memset(fpregset, 0, sizeof(*fpregset));
141 
142     for (i = 0; i < 32; i++) {
143         uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
144         fpregset->fpr[i] = cpu_to_dump64(s, *fpr);
145     }
146     fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
147 }
148 
149 static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
150 {
151     int i;
152     struct PPCElfVmxregset *vmxregset;
153     Note *note = &arg->note;
154     DumpState *s = arg->state;
155 
156     note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX);
157     vmxregset = &note->contents.vmxregset;
158     memset(vmxregset, 0, sizeof(*vmxregset));
159 
160     for (i = 0; i < 32; i++) {
161         bool needs_byteswap;
162         ppc_avr_t *avr = cpu_avr_ptr(&cpu->env, i);
163 
164 #ifdef HOST_WORDS_BIGENDIAN
165         needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB;
166 #else
167         needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB;
168 #endif
169 
170         if (needs_byteswap) {
171             vmxregset->avr[i].u64[0] = bswap64(avr->u64[1]);
172             vmxregset->avr[i].u64[1] = bswap64(avr->u64[0]);
173         } else {
174             vmxregset->avr[i].u64[0] = avr->u64[0];
175             vmxregset->avr[i].u64[1] = avr->u64[1];
176         }
177     }
178     vmxregset->vscr.u32[3] = cpu_to_dump32(s, ppc_get_vscr(&cpu->env));
179 }
180 
181 static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
182 {
183     int i;
184     struct PPCElfVsxregset *vsxregset;
185     Note *note = &arg->note;
186     DumpState *s = arg->state;
187 
188     note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX);
189     vsxregset = &note->contents.vsxregset;
190     memset(vsxregset, 0, sizeof(*vsxregset));
191 
192     for (i = 0; i < 32; i++) {
193         uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
194         vsxregset->vsr[i] = cpu_to_dump64(s, *vsrl);
195     }
196 }
197 
198 static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
199 {
200     struct PPCElfSperegset *speregset;
201     Note *note = &arg->note;
202     DumpState *s = arg->state;
203 
204     note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE);
205     speregset = &note->contents.speregset;
206     memset(speregset, 0, sizeof(*speregset));
207 
208     speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc);
209     speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr);
210 }
211 
212 static const struct NoteFuncDescStruct {
213     int contents_size;
214     void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
215 } note_func[] = {
216     {sizeof_field(Note, contents.prstatus),  ppc_write_elf_prstatus},
217     {sizeof_field(Note, contents.fpregset),  ppc_write_elf_fpregset},
218     {sizeof_field(Note, contents.vmxregset), ppc_write_elf_vmxregset},
219     {sizeof_field(Note, contents.vsxregset), ppc_write_elf_vsxregset},
220     {sizeof_field(Note, contents.speregset), ppc_write_elf_speregset},
221     { 0, NULL}
222 };
223 
224 typedef struct NoteFuncDescStruct NoteFuncDesc;
225 
226 int cpu_get_dump_info(ArchDumpInfo *info,
227                       const struct GuestPhysBlockList *guest_phys_blocks)
228 {
229     PowerPCCPU *cpu;
230     PowerPCCPUClass *pcc;
231 
232     if (first_cpu == NULL) {
233         return -1;
234     }
235 
236     cpu = POWERPC_CPU(first_cpu);
237     pcc = POWERPC_CPU_GET_CLASS(cpu);
238 
239     info->d_machine = PPC_ELF_MACHINE;
240     info->d_class = ELFCLASS;
241 
242     if ((*pcc->interrupts_big_endian)(cpu)) {
243         info->d_endian = ELFDATA2MSB;
244     } else {
245         info->d_endian = ELFDATA2LSB;
246     }
247     /* 64KB is the max page size for pseries kernel */
248     if (strncmp(object_get_typename(qdev_get_machine()),
249                 "pseries-", 8) == 0) {
250         info->page_size = (1U << 16);
251     }
252 
253     return 0;
254 }
255 
256 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
257 {
258     int name_size = 8; /* "CORE" or "QEMU" rounded */
259     size_t elf_note_size = 0;
260     int note_head_size;
261     const NoteFuncDesc *nf;
262 
263     note_head_size = sizeof(Elf_Nhdr);
264     for (nf = note_func; nf->note_contents_func; nf++) {
265         elf_note_size = elf_note_size + note_head_size + name_size +
266             nf->contents_size;
267     }
268 
269     return (elf_note_size) * nr_cpus;
270 }
271 
272 static int ppc_write_all_elf_notes(const char *note_name,
273                                    WriteCoreDumpFunction f,
274                                    PowerPCCPU *cpu, int id,
275                                    void *opaque)
276 {
277     NoteFuncArg arg = { .state = opaque };
278     int ret = -1;
279     int note_size;
280     const NoteFuncDesc *nf;
281 
282     for (nf = note_func; nf->note_contents_func; nf++) {
283         arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name));
284         arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size);
285         strncpy(arg.note.name, note_name, sizeof(arg.note.name));
286 
287         (*nf->note_contents_func)(&arg, cpu);
288 
289         note_size =
290             sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
291         ret = f(&arg.note, note_size, opaque);
292         if (ret < 0) {
293             return -1;
294         }
295     }
296     return 0;
297 }
298 
299 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
300                                int cpuid, void *opaque)
301 {
302     PowerPCCPU *cpu = POWERPC_CPU(cs);
303     return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);
304 }
305 
306 int ppc32_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
307                                int cpuid, void *opaque)
308 {
309     PowerPCCPU *cpu = POWERPC_CPU(cs);
310     return ppc_write_all_elf_notes("CORE", f, cpu, cpuid, opaque);
311 }
312