xref: /openbmc/qemu/target/s390x/arch_dump.c (revision 8779fccb)
1 /*
2  * writing ELF notes for s390x arch
3  *
4  *
5  * Copyright IBM Corp. 2012, 2013
6  *
7  *     Ekaterina Tumanova <tumanova@linux.vnet.ibm.com>
8  *
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.
11  *
12  */
13 
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"
20 
21 
22 struct S390xUserRegsStruct {
23     uint64_t psw[2];
24     uint64_t gprs[16];
25     uint32_t acrs[16];
26 } QEMU_PACKED;
27 
28 typedef struct S390xUserRegsStruct S390xUserRegs;
29 
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;
37 
38 typedef struct S390xElfPrstatusStruct S390xElfPrstatus;
39 
40 struct S390xElfFpregsetStruct {
41     uint32_t fpc;
42     uint32_t pad;
43     uint64_t fprs[16];
44 } QEMU_PACKED;
45 
46 typedef struct S390xElfFpregsetStruct S390xElfFpregset;
47 
48 struct S390xElfVregsLoStruct {
49     uint64_t vregs[16];
50 } QEMU_PACKED;
51 
52 typedef struct S390xElfVregsLoStruct S390xElfVregsLo;
53 
54 struct S390xElfVregsHiStruct {
55     uint64_t vregs[16][2];
56 } QEMU_PACKED;
57 
58 typedef struct S390xElfVregsHiStruct S390xElfVregsHi;
59 
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;
75 
76 static void s390x_write_elf64_prstatus(Note *note, S390CPU *cpu, int id)
77 {
78     int i;
79     S390xUserRegs *regs;
80 
81     note->hdr.n_type = cpu_to_be32(NT_PRSTATUS);
82 
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]);
89     }
90     note->contents.prstatus.pid = id;
91 }
92 
93 static void s390x_write_elf64_fpregset(Note *note, S390CPU *cpu, int id)
94 {
95     int i;
96     CPUS390XState *cs = &cpu->env;
97 
98     note->hdr.n_type = cpu_to_be32(NT_FPREGSET);
99     note->contents.fpregset.fpc = cpu_to_be32(cpu->env.fpc);
100     for (i = 0; i <= 15; i++) {
101         note->contents.fpregset.fprs[i] = cpu_to_be64(get_freg(cs, i)->ll);
102     }
103 }
104 
105 static void s390x_write_elf64_vregslo(Note *note, S390CPU *cpu,  int id)
106 {
107     int i;
108 
109     note->hdr.n_type = cpu_to_be32(NT_S390_VXRS_LOW);
110     for (i = 0; i <= 15; i++) {
111         note->contents.vregslo.vregs[i] = cpu_to_be64(cpu->env.vregs[i][1].ll);
112     }
113 }
114 
115 static void s390x_write_elf64_vregshi(Note *note, S390CPU *cpu, int id)
116 {
117     int i;
118     S390xElfVregsHi *temp_vregshi;
119 
120     temp_vregshi = &note->contents.vregshi;
121 
122     note->hdr.n_type = cpu_to_be32(NT_S390_VXRS_HIGH);
123     for (i = 0; i <= 15; i++) {
124         temp_vregshi->vregs[i][0] = cpu_to_be64(cpu->env.vregs[i + 16][0].ll);
125         temp_vregshi->vregs[i][1] = cpu_to_be64(cpu->env.vregs[i + 16][1].ll);
126     }
127 }
128 
129 static void s390x_write_elf64_timer(Note *note, S390CPU *cpu, int id)
130 {
131     note->hdr.n_type = cpu_to_be32(NT_S390_TIMER);
132     note->contents.timer = cpu_to_be64((uint64_t)(cpu->env.cputm));
133 }
134 
135 static void s390x_write_elf64_todcmp(Note *note, S390CPU *cpu, int id)
136 {
137     note->hdr.n_type = cpu_to_be32(NT_S390_TODCMP);
138     note->contents.todcmp = cpu_to_be64((uint64_t)(cpu->env.ckc));
139 }
140 
141 static void s390x_write_elf64_todpreg(Note *note, S390CPU *cpu, int id)
142 {
143     note->hdr.n_type = cpu_to_be32(NT_S390_TODPREG);
144     note->contents.todpreg = cpu_to_be32((uint32_t)(cpu->env.todpr));
145 }
146 
147 static void s390x_write_elf64_ctrs(Note *note, S390CPU *cpu, int id)
148 {
149     int i;
150 
151     note->hdr.n_type = cpu_to_be32(NT_S390_CTRS);
152 
153     for (i = 0; i <= 15; i++) {
154         note->contents.ctrs[i] = cpu_to_be64(cpu->env.cregs[i]);
155     }
156 }
157 
158 static void s390x_write_elf64_prefix(Note *note, S390CPU *cpu, int id)
159 {
160     note->hdr.n_type = cpu_to_be32(NT_S390_PREFIX);
161     note->contents.prefix = cpu_to_be32((uint32_t)(cpu->env.psa));
162 }
163 
164 
165 typedef struct NoteFuncDescStruct {
166     int contents_size;
167     void (*note_contents_func)(Note *note, S390CPU *cpu, int id);
168 } NoteFuncDesc;
169 
170 static const NoteFuncDesc note_core[] = {
171     {sizeof(((Note *)0)->contents.prstatus), s390x_write_elf64_prstatus},
172     {sizeof(((Note *)0)->contents.fpregset), s390x_write_elf64_fpregset},
173     { 0, NULL}
174 };
175 
176 static const NoteFuncDesc note_linux[] = {
177     {sizeof(((Note *)0)->contents.prefix),   s390x_write_elf64_prefix},
178     {sizeof(((Note *)0)->contents.ctrs),     s390x_write_elf64_ctrs},
179     {sizeof(((Note *)0)->contents.timer),    s390x_write_elf64_timer},
180     {sizeof(((Note *)0)->contents.todcmp),   s390x_write_elf64_todcmp},
181     {sizeof(((Note *)0)->contents.todpreg),  s390x_write_elf64_todpreg},
182     {sizeof(((Note *)0)->contents.vregslo),  s390x_write_elf64_vregslo},
183     {sizeof(((Note *)0)->contents.vregshi),  s390x_write_elf64_vregshi},
184     { 0, NULL}
185 };
186 
187 static int s390x_write_elf64_notes(const char *note_name,
188                                        WriteCoreDumpFunction f,
189                                        S390CPU *cpu, int id,
190                                        void *opaque,
191                                        const NoteFuncDesc *funcs)
192 {
193     Note note;
194     const NoteFuncDesc *nf;
195     int note_size;
196     int ret = -1;
197 
198     for (nf = funcs; nf->note_contents_func; nf++) {
199         memset(&note, 0, sizeof(note));
200         note.hdr.n_namesz = cpu_to_be32(strlen(note_name) + 1);
201         note.hdr.n_descsz = cpu_to_be32(nf->contents_size);
202         strncpy(note.name, note_name, sizeof(note.name));
203         (*nf->note_contents_func)(&note, cpu, id);
204 
205         note_size = sizeof(note) - sizeof(note.contents) + nf->contents_size;
206         ret = f(&note, note_size, opaque);
207 
208         if (ret < 0) {
209             return -1;
210         }
211 
212     }
213 
214     return 0;
215 }
216 
217 
218 int s390_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
219                               int cpuid, void *opaque)
220 {
221     S390CPU *cpu = S390_CPU(cs);
222     int r;
223 
224     r = s390x_write_elf64_notes("CORE", f, cpu, cpuid, opaque, note_core);
225     if (r) {
226         return r;
227     }
228     return s390x_write_elf64_notes("LINUX", f, cpu, cpuid, opaque, note_linux);
229 }
230 
231 int cpu_get_dump_info(ArchDumpInfo *info,
232                       const struct GuestPhysBlockList *guest_phys_blocks)
233 {
234     info->d_machine = EM_S390;
235     info->d_endian = ELFDATA2MSB;
236     info->d_class = ELFCLASS64;
237 
238     return 0;
239 }
240 
241 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
242 {
243     int name_size = 8; /* "LINUX" or "CORE" + pad */
244     size_t elf_note_size = 0;
245     int note_head_size;
246     const NoteFuncDesc *nf;
247 
248     assert(class == ELFCLASS64);
249     assert(machine == EM_S390);
250 
251     note_head_size = sizeof(Elf64_Nhdr);
252 
253     for (nf = note_core; nf->note_contents_func; nf++) {
254         elf_note_size = elf_note_size + note_head_size + name_size +
255                         nf->contents_size;
256     }
257     for (nf = note_linux; nf->note_contents_func; nf++) {
258         elf_note_size = elf_note_size + note_head_size + name_size +
259                         nf->contents_size;
260     }
261 
262     return (elf_note_size) * nr_cpus;
263 }
264