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