]>
Commit | Line | Data |
---|---|---|
e62fbc54 AK |
1 | /* |
2 | * writing ELF notes for ppc64 arch | |
3 | * | |
4 | * | |
5 | * Copyright IBM, Corp. 2013 | |
6 | * | |
7 | * Authors: | |
8 | * Aneesh Kumar K.V <[email protected]> | |
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 "cpu.h" | |
16 | #include "elf.h" | |
17 | #include "exec/cpu-all.h" | |
18 | #include "sysemu/dump.h" | |
19 | #include "sysemu/kvm.h" | |
20 | ||
21 | struct PPC64UserRegStruct { | |
22 | uint64_t gpr[32]; | |
23 | uint64_t nip; | |
24 | uint64_t msr; | |
25 | uint64_t orig_gpr3; | |
26 | uint64_t ctr; | |
27 | uint64_t link; | |
28 | uint64_t xer; | |
29 | uint64_t ccr; | |
30 | uint64_t softe; | |
31 | uint64_t trap; | |
32 | uint64_t dar; | |
33 | uint64_t dsisr; | |
34 | uint64_t result; | |
35 | } QEMU_PACKED; | |
36 | ||
37 | struct PPC64ElfPrstatus { | |
38 | char pad1[112]; | |
39 | struct PPC64UserRegStruct pr_reg; | |
40 | uint64_t pad2[4]; | |
41 | } QEMU_PACKED; | |
42 | ||
43 | ||
44 | struct PPC64ElfFpregset { | |
45 | uint64_t fpr[32]; | |
46 | uint64_t fpscr; | |
47 | } QEMU_PACKED; | |
48 | ||
49 | ||
50 | struct PPC64ElfVmxregset { | |
51 | ppc_avr_t avr[32]; | |
52 | ppc_avr_t vscr; | |
53 | union { | |
54 | ppc_avr_t unused; | |
55 | uint32_t value; | |
56 | } vrsave; | |
57 | } QEMU_PACKED; | |
58 | ||
59 | struct PPC64ElfVsxregset { | |
60 | uint64_t vsr[32]; | |
61 | } QEMU_PACKED; | |
62 | ||
63 | struct PPC64ElfSperegset { | |
64 | uint32_t evr[32]; | |
65 | uint64_t spe_acc; | |
66 | uint32_t spe_fscr; | |
67 | } QEMU_PACKED; | |
68 | ||
69 | typedef struct noteStruct { | |
70 | Elf64_Nhdr hdr; | |
71 | char name[5]; | |
72 | char pad3[3]; | |
73 | union { | |
74 | struct PPC64ElfPrstatus prstatus; | |
75 | struct PPC64ElfFpregset fpregset; | |
76 | struct PPC64ElfVmxregset vmxregset; | |
77 | struct PPC64ElfVsxregset vsxregset; | |
78 | struct PPC64ElfSperegset speregset; | |
79 | } contents; | |
80 | } QEMU_PACKED Note; | |
81 | ||
0c967de9 BR |
82 | typedef struct NoteFuncArg { |
83 | Note note; | |
84 | DumpState *state; | |
85 | } NoteFuncArg; | |
e62fbc54 | 86 | |
0c967de9 | 87 | static void ppc64_write_elf64_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu) |
e62fbc54 AK |
88 | { |
89 | int i; | |
90 | uint64_t cr; | |
91 | struct PPC64ElfPrstatus *prstatus; | |
92 | struct PPC64UserRegStruct *reg; | |
0c967de9 BR |
93 | Note *note = &arg->note; |
94 | DumpState *s = arg->state; | |
e62fbc54 | 95 | |
0c967de9 | 96 | note->hdr.n_type = cpu_to_dump32(s, NT_PRSTATUS); |
e62fbc54 AK |
97 | |
98 | prstatus = ¬e->contents.prstatus; | |
99 | memset(prstatus, 0, sizeof(*prstatus)); | |
100 | reg = &prstatus->pr_reg; | |
101 | ||
102 | for (i = 0; i < 32; i++) { | |
0c967de9 | 103 | reg->gpr[i] = cpu_to_dump64(s, cpu->env.gpr[i]); |
e62fbc54 | 104 | } |
0c967de9 BR |
105 | reg->nip = cpu_to_dump64(s, cpu->env.nip); |
106 | reg->msr = cpu_to_dump64(s, cpu->env.msr); | |
107 | reg->ctr = cpu_to_dump64(s, cpu->env.ctr); | |
108 | reg->link = cpu_to_dump64(s, cpu->env.lr); | |
109 | reg->xer = cpu_to_dump64(s, cpu_read_xer(&cpu->env)); | |
e62fbc54 AK |
110 | |
111 | cr = 0; | |
112 | for (i = 0; i < 8; i++) { | |
113 | cr |= (cpu->env.crf[i] & 15) << (4 * (7 - i)); | |
114 | } | |
0c967de9 | 115 | reg->ccr = cpu_to_dump64(s, cr); |
e62fbc54 AK |
116 | } |
117 | ||
0c967de9 | 118 | static void ppc64_write_elf64_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu) |
e62fbc54 AK |
119 | { |
120 | int i; | |
121 | struct PPC64ElfFpregset *fpregset; | |
0c967de9 BR |
122 | Note *note = &arg->note; |
123 | DumpState *s = arg->state; | |
e62fbc54 | 124 | |
0c967de9 | 125 | note->hdr.n_type = cpu_to_dump32(s, NT_PRFPREG); |
e62fbc54 AK |
126 | |
127 | fpregset = ¬e->contents.fpregset; | |
128 | memset(fpregset, 0, sizeof(*fpregset)); | |
129 | ||
130 | for (i = 0; i < 32; i++) { | |
0c967de9 | 131 | fpregset->fpr[i] = cpu_to_dump64(s, cpu->env.fpr[i]); |
e62fbc54 | 132 | } |
0c967de9 | 133 | fpregset->fpscr = cpu_to_dump64(s, cpu->env.fpscr); |
e62fbc54 AK |
134 | } |
135 | ||
0c967de9 | 136 | static void ppc64_write_elf64_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu) |
e62fbc54 AK |
137 | { |
138 | int i; | |
139 | struct PPC64ElfVmxregset *vmxregset; | |
0c967de9 BR |
140 | Note *note = &arg->note; |
141 | DumpState *s = arg->state; | |
e62fbc54 | 142 | |
0c967de9 | 143 | note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VMX); |
e62fbc54 AK |
144 | vmxregset = ¬e->contents.vmxregset; |
145 | memset(vmxregset, 0, sizeof(*vmxregset)); | |
146 | ||
147 | for (i = 0; i < 32; i++) { | |
0c967de9 BR |
148 | bool needs_byteswap; |
149 | ||
150 | #ifdef HOST_WORDS_BIGENDIAN | |
151 | needs_byteswap = s->dump_info.d_endian == ELFDATA2LSB; | |
152 | #else | |
153 | needs_byteswap = s->dump_info.d_endian == ELFDATA2MSB; | |
154 | #endif | |
155 | ||
156 | if (needs_byteswap) { | |
157 | vmxregset->avr[i].u64[0] = bswap64(cpu->env.avr[i].u64[1]); | |
158 | vmxregset->avr[i].u64[1] = bswap64(cpu->env.avr[i].u64[0]); | |
159 | } else { | |
160 | vmxregset->avr[i].u64[0] = cpu->env.avr[i].u64[0]; | |
161 | vmxregset->avr[i].u64[1] = cpu->env.avr[i].u64[1]; | |
162 | } | |
e62fbc54 | 163 | } |
0c967de9 | 164 | vmxregset->vscr.u32[3] = cpu_to_dump32(s, cpu->env.vscr); |
e62fbc54 | 165 | } |
0c967de9 | 166 | static void ppc64_write_elf64_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu) |
e62fbc54 AK |
167 | { |
168 | int i; | |
169 | struct PPC64ElfVsxregset *vsxregset; | |
0c967de9 BR |
170 | Note *note = &arg->note; |
171 | DumpState *s = arg->state; | |
e62fbc54 | 172 | |
0c967de9 | 173 | note->hdr.n_type = cpu_to_dump32(s, NT_PPC_VSX); |
e62fbc54 AK |
174 | vsxregset = ¬e->contents.vsxregset; |
175 | memset(vsxregset, 0, sizeof(*vsxregset)); | |
176 | ||
177 | for (i = 0; i < 32; i++) { | |
0c967de9 | 178 | vsxregset->vsr[i] = cpu_to_dump64(s, cpu->env.vsr[i]); |
e62fbc54 AK |
179 | } |
180 | } | |
0c967de9 | 181 | static void ppc64_write_elf64_speregset(NoteFuncArg *arg, PowerPCCPU *cpu) |
e62fbc54 AK |
182 | { |
183 | struct PPC64ElfSperegset *speregset; | |
0c967de9 BR |
184 | Note *note = &arg->note; |
185 | DumpState *s = arg->state; | |
186 | ||
187 | note->hdr.n_type = cpu_to_dump32(s, NT_PPC_SPE); | |
e62fbc54 AK |
188 | speregset = ¬e->contents.speregset; |
189 | memset(speregset, 0, sizeof(*speregset)); | |
190 | ||
0c967de9 BR |
191 | speregset->spe_acc = cpu_to_dump64(s, cpu->env.spe_acc); |
192 | speregset->spe_fscr = cpu_to_dump32(s, cpu->env.spe_fscr); | |
e62fbc54 AK |
193 | } |
194 | ||
cfd54a04 | 195 | static const struct NoteFuncDescStruct { |
e62fbc54 | 196 | int contents_size; |
0c967de9 | 197 | void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu); |
e62fbc54 AK |
198 | } note_func[] = { |
199 | {sizeof(((Note *)0)->contents.prstatus), ppc64_write_elf64_prstatus}, | |
200 | {sizeof(((Note *)0)->contents.fpregset), ppc64_write_elf64_fpregset}, | |
201 | {sizeof(((Note *)0)->contents.vmxregset), ppc64_write_elf64_vmxregset}, | |
202 | {sizeof(((Note *)0)->contents.vsxregset), ppc64_write_elf64_vsxregset}, | |
203 | {sizeof(((Note *)0)->contents.speregset), ppc64_write_elf64_speregset}, | |
204 | { 0, NULL} | |
205 | }; | |
206 | ||
207 | typedef struct NoteFuncDescStruct NoteFuncDesc; | |
208 | ||
209 | int cpu_get_dump_info(ArchDumpInfo *info, | |
210 | const struct GuestPhysBlockList *guest_phys_blocks) | |
211 | { | |
1e6ed54e BR |
212 | PowerPCCPU *cpu = POWERPC_CPU(first_cpu); |
213 | PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); | |
214 | ||
e62fbc54 | 215 | info->d_machine = EM_PPC64; |
e62fbc54 | 216 | info->d_class = ELFCLASS64; |
1e6ed54e BR |
217 | if ((*pcc->interrupts_big_endian)(cpu)) { |
218 | info->d_endian = ELFDATA2MSB; | |
219 | } else { | |
220 | info->d_endian = ELFDATA2LSB; | |
221 | } | |
e62fbc54 AK |
222 | |
223 | return 0; | |
224 | } | |
225 | ||
226 | ssize_t cpu_get_note_size(int class, int machine, int nr_cpus) | |
227 | { | |
228 | int name_size = 8; /* "CORE" or "QEMU" rounded */ | |
229 | size_t elf_note_size = 0; | |
230 | int note_head_size; | |
cfd54a04 | 231 | const NoteFuncDesc *nf; |
e62fbc54 AK |
232 | |
233 | if (class != ELFCLASS64) { | |
234 | return -1; | |
235 | } | |
236 | assert(machine == EM_PPC64); | |
237 | ||
238 | note_head_size = sizeof(Elf64_Nhdr); | |
239 | ||
240 | for (nf = note_func; nf->note_contents_func; nf++) { | |
241 | elf_note_size = elf_note_size + note_head_size + name_size + | |
242 | nf->contents_size; | |
243 | } | |
244 | ||
245 | return (elf_note_size) * nr_cpus; | |
246 | } | |
247 | ||
248 | static int ppc64_write_all_elf64_notes(const char *note_name, | |
249 | WriteCoreDumpFunction f, | |
250 | PowerPCCPU *cpu, int id, | |
251 | void *opaque) | |
252 | { | |
0c967de9 | 253 | NoteFuncArg arg = { .state = opaque }; |
e62fbc54 AK |
254 | int ret = -1; |
255 | int note_size; | |
cfd54a04 | 256 | const NoteFuncDesc *nf; |
e62fbc54 AK |
257 | |
258 | for (nf = note_func; nf->note_contents_func; nf++) { | |
0c967de9 BR |
259 | arg.note.hdr.n_namesz = cpu_to_dump32(opaque, sizeof(arg.note.name)); |
260 | arg.note.hdr.n_descsz = cpu_to_dump32(opaque, nf->contents_size); | |
261 | strncpy(arg.note.name, note_name, sizeof(arg.note.name)); | |
e62fbc54 | 262 | |
0c967de9 | 263 | (*nf->note_contents_func)(&arg, cpu); |
e62fbc54 | 264 | |
0c967de9 BR |
265 | note_size = |
266 | sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size; | |
267 | ret = f(&arg.note, note_size, opaque); | |
e62fbc54 AK |
268 | if (ret < 0) { |
269 | return -1; | |
270 | } | |
271 | } | |
272 | return 0; | |
273 | } | |
274 | ||
275 | int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs, | |
276 | int cpuid, void *opaque) | |
277 | { | |
278 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
279 | return ppc64_write_all_elf64_notes("CORE", f, cpu, cpuid, opaque); | |
280 | } | |
281 | ||
282 | int ppc64_cpu_write_elf64_qemunote(WriteCoreDumpFunction f, | |
283 | CPUState *cpu, void *opaque) | |
284 | { | |
285 | return 0; | |
286 | } |