Merge remote-tracking branch 'origin/tizen' into glesv3
[sdk/emulator/qemu.git] / target-s390x / arch_dump.c
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 "cpu.h"
15 #include "elf.h"
16 #include "exec/cpu-all.h"
17 #include "sysemu/dump.h"
18 #include "sysemu/kvm.h"
19
20
21 struct S390xUserRegsStruct {
22     uint64_t psw[2];
23     uint64_t gprs[16];
24     uint32_t acrs[16];
25 } QEMU_PACKED;
26
27 typedef struct S390xUserRegsStruct S390xUserRegs;
28
29 struct S390xElfPrstatusStruct {
30     uint8_t pad1[32];
31     uint32_t pid;
32     uint8_t pad2[76];
33     S390xUserRegs regs;
34     uint8_t pad3[16];
35 } QEMU_PACKED;
36
37 typedef struct S390xElfPrstatusStruct S390xElfPrstatus;
38
39 struct S390xElfFpregsetStruct {
40     uint32_t fpc;
41     uint32_t pad;
42     uint64_t fprs[16];
43 } QEMU_PACKED;
44
45 typedef struct S390xElfFpregsetStruct S390xElfFpregset;
46
47 typedef struct noteStruct {
48     Elf64_Nhdr hdr;
49     char name[5];
50     char pad3[3];
51     union {
52         S390xElfPrstatus prstatus;
53         S390xElfFpregset fpregset;
54         uint32_t prefix;
55         uint64_t timer;
56         uint64_t todcmp;
57         uint32_t todpreg;
58         uint64_t ctrs[16];
59     } contents;
60 } QEMU_PACKED Note;
61
62 static void s390x_write_elf64_prstatus(Note *note, S390CPU *cpu)
63 {
64     int i;
65     S390xUserRegs *regs;
66
67     note->hdr.n_type = cpu_to_be32(NT_PRSTATUS);
68
69     regs = &(note->contents.prstatus.regs);
70     regs->psw[0] = cpu_to_be64(cpu->env.psw.mask);
71     regs->psw[1] = cpu_to_be64(cpu->env.psw.addr);
72     for (i = 0; i <= 15; i++) {
73         regs->acrs[i] = cpu_to_be32(cpu->env.aregs[i]);
74         regs->gprs[i] = cpu_to_be64(cpu->env.regs[i]);
75     }
76 }
77
78 static void s390x_write_elf64_fpregset(Note *note, S390CPU *cpu)
79 {
80     int i;
81
82     note->hdr.n_type = cpu_to_be32(NT_FPREGSET);
83     note->contents.fpregset.fpc = cpu_to_be32(cpu->env.fpc);
84     for (i = 0; i <= 15; i++) {
85         note->contents.fpregset.fprs[i] = cpu_to_be64(cpu->env.fregs[i].ll);
86     }
87 }
88
89
90 static void s390x_write_elf64_timer(Note *note, S390CPU *cpu)
91 {
92     note->hdr.n_type = cpu_to_be32(NT_S390_TIMER);
93     note->contents.timer = cpu_to_be64((uint64_t)(cpu->env.cputm));
94 }
95
96 static void s390x_write_elf64_todcmp(Note *note, S390CPU *cpu)
97 {
98     note->hdr.n_type = cpu_to_be32(NT_S390_TODCMP);
99     note->contents.todcmp = cpu_to_be64((uint64_t)(cpu->env.ckc));
100 }
101
102 static void s390x_write_elf64_todpreg(Note *note, S390CPU *cpu)
103 {
104     note->hdr.n_type = cpu_to_be32(NT_S390_TODPREG);
105     note->contents.todpreg = cpu_to_be32((uint32_t)(cpu->env.todpr));
106 }
107
108 static void s390x_write_elf64_ctrs(Note *note, S390CPU *cpu)
109 {
110     int i;
111
112     note->hdr.n_type = cpu_to_be32(NT_S390_CTRS);
113
114     for (i = 0; i <= 15; i++) {
115         note->contents.ctrs[i] = cpu_to_be64(cpu->env.cregs[i]);
116     }
117 }
118
119 static void s390x_write_elf64_prefix(Note *note, S390CPU *cpu)
120 {
121     note->hdr.n_type = cpu_to_be32(NT_S390_PREFIX);
122     note->contents.prefix = cpu_to_be32((uint32_t)(cpu->env.psa));
123 }
124
125
126 struct NoteFuncDescStruct {
127     int contents_size;
128     void (*note_contents_func)(Note *note, S390CPU *cpu);
129 } note_func[] = {
130     {sizeof(((Note *)0)->contents.prstatus), s390x_write_elf64_prstatus},
131     {sizeof(((Note *)0)->contents.prefix),   s390x_write_elf64_prefix},
132     {sizeof(((Note *)0)->contents.fpregset), s390x_write_elf64_fpregset},
133     {sizeof(((Note *)0)->contents.ctrs),     s390x_write_elf64_ctrs},
134     {sizeof(((Note *)0)->contents.timer),    s390x_write_elf64_timer},
135     {sizeof(((Note *)0)->contents.todcmp),   s390x_write_elf64_todcmp},
136     {sizeof(((Note *)0)->contents.todpreg),  s390x_write_elf64_todpreg},
137     { 0, NULL}
138 };
139
140 typedef struct NoteFuncDescStruct NoteFuncDesc;
141
142
143 static int s390x_write_all_elf64_notes(const char *note_name,
144                                        WriteCoreDumpFunction f,
145                                        S390CPU *cpu, int id,
146                                        void *opaque)
147 {
148     Note note;
149     NoteFuncDesc *nf;
150     int note_size;
151     int ret = -1;
152
153     for (nf = note_func; nf->note_contents_func; nf++) {
154         memset(&note, 0, sizeof(note));
155         note.hdr.n_namesz = cpu_to_be32(sizeof(note.name));
156         note.hdr.n_descsz = cpu_to_be32(nf->contents_size);
157         strncpy(note.name, note_name, sizeof(note.name));
158         (*nf->note_contents_func)(&note, cpu);
159
160         note_size = sizeof(note) - sizeof(note.contents) + nf->contents_size;
161         ret = f(&note, note_size, opaque);
162
163         if (ret < 0) {
164             return -1;
165         }
166
167     }
168
169     return 0;
170 }
171
172
173 int s390_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
174                               int cpuid, void *opaque)
175 {
176     S390CPU *cpu = S390_CPU(cs);
177     return s390x_write_all_elf64_notes("CORE", f, cpu, cpuid, opaque);
178 }
179
180 int cpu_get_dump_info(ArchDumpInfo *info,
181                       const struct GuestPhysBlockList *guest_phys_blocks)
182 {
183     info->d_machine = EM_S390;
184     info->d_endian = ELFDATA2MSB;
185     info->d_class = ELFCLASS64;
186
187     return 0;
188 }
189
190 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
191 {
192     int name_size = 8; /* "CORE" or "QEMU" rounded */
193     size_t elf_note_size = 0;
194     int note_head_size;
195     NoteFuncDesc *nf;
196
197     assert(class == ELFCLASS64);
198     assert(machine == EM_S390);
199
200     note_head_size = sizeof(Elf64_Nhdr);
201
202     for (nf = note_func; nf->note_contents_func; nf++) {
203         elf_note_size = elf_note_size + note_head_size + name_size +
204                         nf->contents_size;
205     }
206
207     return (elf_note_size) * nr_cpus;
208 }
209
210 int s390_cpu_write_elf64_qemunote(WriteCoreDumpFunction f,
211                                   CPUState *cpu, void *opaque)
212 {
213     return 0;
214 }