s390x/arch_dump: use proper note name and note size
[qemu.git] / target / s390x / arch_dump.c
blob887cae947e6bd9c1dc6d130fa8f4c55477bd7cb3
1 /*
2 * writing ELF notes for s390x arch
5 * Copyright IBM Corp. 2012, 2013
7 * Ekaterina Tumanova <tumanova@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "cpu.h"
16 #include "elf.h"
17 #include "exec/cpu-all.h"
18 #include "sysemu/dump.h"
19 #include "sysemu/kvm.h"
22 struct S390xUserRegsStruct {
23 uint64_t psw[2];
24 uint64_t gprs[16];
25 uint32_t acrs[16];
26 } QEMU_PACKED;
28 typedef struct S390xUserRegsStruct S390xUserRegs;
30 struct S390xElfPrstatusStruct {
31 uint8_t pad1[32];
32 uint32_t pid;
33 uint8_t pad2[76];
34 S390xUserRegs regs;
35 uint8_t pad3[16];
36 } QEMU_PACKED;
38 typedef struct S390xElfPrstatusStruct S390xElfPrstatus;
40 struct S390xElfFpregsetStruct {
41 uint32_t fpc;
42 uint32_t pad;
43 uint64_t fprs[16];
44 } QEMU_PACKED;
46 typedef struct S390xElfFpregsetStruct S390xElfFpregset;
48 struct S390xElfVregsLoStruct {
49 uint64_t vregs[16];
50 } QEMU_PACKED;
52 typedef struct S390xElfVregsLoStruct S390xElfVregsLo;
54 struct S390xElfVregsHiStruct {
55 uint64_t vregs[16][2];
56 } QEMU_PACKED;
58 typedef struct S390xElfVregsHiStruct S390xElfVregsHi;
60 typedef struct noteStruct {
61 Elf64_Nhdr hdr;
62 char name[8];
63 union {
64 S390xElfPrstatus prstatus;
65 S390xElfFpregset fpregset;
66 S390xElfVregsLo vregslo;
67 S390xElfVregsHi vregshi;
68 uint32_t prefix;
69 uint64_t timer;
70 uint64_t todcmp;
71 uint32_t todpreg;
72 uint64_t ctrs[16];
73 } contents;
74 } QEMU_PACKED Note;
76 static void s390x_write_elf64_prstatus(Note *note, S390CPU *cpu)
78 int i;
79 S390xUserRegs *regs;
81 note->hdr.n_type = cpu_to_be32(NT_PRSTATUS);
83 regs = &(note->contents.prstatus.regs);
84 regs->psw[0] = cpu_to_be64(cpu->env.psw.mask);
85 regs->psw[1] = cpu_to_be64(cpu->env.psw.addr);
86 for (i = 0; i <= 15; i++) {
87 regs->acrs[i] = cpu_to_be32(cpu->env.aregs[i]);
88 regs->gprs[i] = cpu_to_be64(cpu->env.regs[i]);
92 static void s390x_write_elf64_fpregset(Note *note, S390CPU *cpu)
94 int i;
95 CPUS390XState *cs = &cpu->env;
97 note->hdr.n_type = cpu_to_be32(NT_FPREGSET);
98 note->contents.fpregset.fpc = cpu_to_be32(cpu->env.fpc);
99 for (i = 0; i <= 15; i++) {
100 note->contents.fpregset.fprs[i] = cpu_to_be64(get_freg(cs, i)->ll);
104 static void s390x_write_elf64_vregslo(Note *note, S390CPU *cpu)
106 int i;
108 note->hdr.n_type = cpu_to_be32(NT_S390_VXRS_LOW);
109 for (i = 0; i <= 15; i++) {
110 note->contents.vregslo.vregs[i] = cpu_to_be64(cpu->env.vregs[i][1].ll);
114 static void s390x_write_elf64_vregshi(Note *note, S390CPU *cpu)
116 int i;
117 S390xElfVregsHi *temp_vregshi;
119 temp_vregshi = &note->contents.vregshi;
121 note->hdr.n_type = cpu_to_be32(NT_S390_VXRS_HIGH);
122 for (i = 0; i <= 15; i++) {
123 temp_vregshi->vregs[i][0] = cpu_to_be64(cpu->env.vregs[i + 16][0].ll);
124 temp_vregshi->vregs[i][1] = cpu_to_be64(cpu->env.vregs[i + 16][1].ll);
128 static void s390x_write_elf64_timer(Note *note, S390CPU *cpu)
130 note->hdr.n_type = cpu_to_be32(NT_S390_TIMER);
131 note->contents.timer = cpu_to_be64((uint64_t)(cpu->env.cputm));
134 static void s390x_write_elf64_todcmp(Note *note, S390CPU *cpu)
136 note->hdr.n_type = cpu_to_be32(NT_S390_TODCMP);
137 note->contents.todcmp = cpu_to_be64((uint64_t)(cpu->env.ckc));
140 static void s390x_write_elf64_todpreg(Note *note, S390CPU *cpu)
142 note->hdr.n_type = cpu_to_be32(NT_S390_TODPREG);
143 note->contents.todpreg = cpu_to_be32((uint32_t)(cpu->env.todpr));
146 static void s390x_write_elf64_ctrs(Note *note, S390CPU *cpu)
148 int i;
150 note->hdr.n_type = cpu_to_be32(NT_S390_CTRS);
152 for (i = 0; i <= 15; i++) {
153 note->contents.ctrs[i] = cpu_to_be64(cpu->env.cregs[i]);
157 static void s390x_write_elf64_prefix(Note *note, S390CPU *cpu)
159 note->hdr.n_type = cpu_to_be32(NT_S390_PREFIX);
160 note->contents.prefix = cpu_to_be32((uint32_t)(cpu->env.psa));
164 typedef struct NoteFuncDescStruct {
165 int contents_size;
166 void (*note_contents_func)(Note *note, S390CPU *cpu);
167 } NoteFuncDesc;
169 static const NoteFuncDesc note_core[] = {
170 {sizeof(((Note *)0)->contents.prstatus), s390x_write_elf64_prstatus},
171 {sizeof(((Note *)0)->contents.fpregset), s390x_write_elf64_fpregset},
172 { 0, NULL}
175 static const NoteFuncDesc note_linux[] = {
176 {sizeof(((Note *)0)->contents.prefix), s390x_write_elf64_prefix},
177 {sizeof(((Note *)0)->contents.ctrs), s390x_write_elf64_ctrs},
178 {sizeof(((Note *)0)->contents.timer), s390x_write_elf64_timer},
179 {sizeof(((Note *)0)->contents.todcmp), s390x_write_elf64_todcmp},
180 {sizeof(((Note *)0)->contents.todpreg), s390x_write_elf64_todpreg},
181 {sizeof(((Note *)0)->contents.vregslo), s390x_write_elf64_vregslo},
182 {sizeof(((Note *)0)->contents.vregshi), s390x_write_elf64_vregshi},
183 { 0, NULL}
186 static int s390x_write_elf64_notes(const char *note_name,
187 WriteCoreDumpFunction f,
188 S390CPU *cpu, int id,
189 void *opaque,
190 const NoteFuncDesc *funcs)
192 Note note;
193 const NoteFuncDesc *nf;
194 int note_size;
195 int ret = -1;
197 for (nf = funcs; nf->note_contents_func; nf++) {
198 memset(&note, 0, sizeof(note));
199 note.hdr.n_namesz = cpu_to_be32(strlen(note_name) + 1);
200 note.hdr.n_descsz = cpu_to_be32(nf->contents_size);
201 strncpy(note.name, note_name, sizeof(note.name));
202 (*nf->note_contents_func)(&note, cpu);
204 note_size = sizeof(note) - sizeof(note.contents) + nf->contents_size;
205 ret = f(&note, note_size, opaque);
207 if (ret < 0) {
208 return -1;
213 return 0;
217 int s390_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
218 int cpuid, void *opaque)
220 S390CPU *cpu = S390_CPU(cs);
221 int r;
223 r = s390x_write_elf64_notes("CORE", f, cpu, cpuid, opaque, note_core);
224 if (r) {
225 return r;
227 return s390x_write_elf64_notes("LINUX", f, cpu, cpuid, opaque, note_linux);
230 int cpu_get_dump_info(ArchDumpInfo *info,
231 const struct GuestPhysBlockList *guest_phys_blocks)
233 info->d_machine = EM_S390;
234 info->d_endian = ELFDATA2MSB;
235 info->d_class = ELFCLASS64;
237 return 0;
240 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
242 int name_size = 8; /* "LINUX" or "CORE" + pad */
243 size_t elf_note_size = 0;
244 int note_head_size;
245 const NoteFuncDesc *nf;
247 assert(class == ELFCLASS64);
248 assert(machine == EM_S390);
250 note_head_size = sizeof(Elf64_Nhdr);
252 for (nf = note_core; nf->note_contents_func; nf++) {
253 elf_note_size = elf_note_size + note_head_size + name_size +
254 nf->contents_size;
256 for (nf = note_linux; nf->note_contents_func; nf++) {
257 elf_note_size = elf_note_size + note_head_size + name_size +
258 nf->contents_size;
261 return (elf_note_size) * nr_cpus;