2 * writing ELF notes for ppc64 arch
5 * Copyright IBM, Corp. 2013
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
18 #include "exec/cpu-all.h"
19 #include "sysemu/dump.h"
20 #include "sysemu/kvm.h"
22 struct PPC64UserRegStruct
{
38 struct PPC64ElfPrstatus
{
40 struct PPC64UserRegStruct pr_reg
;
45 struct PPC64ElfFpregset
{
51 struct PPC64ElfVmxregset
{
60 struct PPC64ElfVsxregset
{
64 struct PPC64ElfSperegset
{
70 typedef struct noteStruct
{
75 struct PPC64ElfPrstatus prstatus
;
76 struct PPC64ElfFpregset fpregset
;
77 struct PPC64ElfVmxregset vmxregset
;
78 struct PPC64ElfVsxregset vsxregset
;
79 struct PPC64ElfSperegset speregset
;
83 typedef struct NoteFuncArg
{
88 static void ppc64_write_elf64_prstatus(NoteFuncArg
*arg
, PowerPCCPU
*cpu
)
92 struct PPC64ElfPrstatus
*prstatus
;
93 struct PPC64UserRegStruct
*reg
;
94 Note
*note
= &arg
->note
;
95 DumpState
*s
= arg
->state
;
97 note
->hdr
.n_type
= cpu_to_dump32(s
, NT_PRSTATUS
);
99 prstatus
= ¬e
->contents
.prstatus
;
100 memset(prstatus
, 0, sizeof(*prstatus
));
101 reg
= &prstatus
->pr_reg
;
103 for (i
= 0; i
< 32; i
++) {
104 reg
->gpr
[i
] = cpu_to_dump64(s
, cpu
->env
.gpr
[i
]);
106 reg
->nip
= cpu_to_dump64(s
, cpu
->env
.nip
);
107 reg
->msr
= cpu_to_dump64(s
, cpu
->env
.msr
);
108 reg
->ctr
= cpu_to_dump64(s
, cpu
->env
.ctr
);
109 reg
->link
= cpu_to_dump64(s
, cpu
->env
.lr
);
110 reg
->xer
= cpu_to_dump64(s
, cpu_read_xer(&cpu
->env
));
113 for (i
= 0; i
< 8; i
++) {
114 cr
|= (cpu
->env
.crf
[i
] & 15) << (4 * (7 - i
));
116 reg
->ccr
= cpu_to_dump64(s
, cr
);
119 static void ppc64_write_elf64_fpregset(NoteFuncArg
*arg
, PowerPCCPU
*cpu
)
122 struct PPC64ElfFpregset
*fpregset
;
123 Note
*note
= &arg
->note
;
124 DumpState
*s
= arg
->state
;
126 note
->hdr
.n_type
= cpu_to_dump32(s
, NT_PRFPREG
);
128 fpregset
= ¬e
->contents
.fpregset
;
129 memset(fpregset
, 0, sizeof(*fpregset
));
131 for (i
= 0; i
< 32; i
++) {
132 fpregset
->fpr
[i
] = cpu_to_dump64(s
, cpu
->env
.fpr
[i
]);
134 fpregset
->fpscr
= cpu_to_dump64(s
, cpu
->env
.fpscr
);
137 static void ppc64_write_elf64_vmxregset(NoteFuncArg
*arg
, PowerPCCPU
*cpu
)
140 struct PPC64ElfVmxregset
*vmxregset
;
141 Note
*note
= &arg
->note
;
142 DumpState
*s
= arg
->state
;
144 note
->hdr
.n_type
= cpu_to_dump32(s
, NT_PPC_VMX
);
145 vmxregset
= ¬e
->contents
.vmxregset
;
146 memset(vmxregset
, 0, sizeof(*vmxregset
));
148 for (i
= 0; i
< 32; i
++) {
151 #ifdef HOST_WORDS_BIGENDIAN
152 needs_byteswap
= s
->dump_info
.d_endian
== ELFDATA2LSB
;
154 needs_byteswap
= s
->dump_info
.d_endian
== ELFDATA2MSB
;
157 if (needs_byteswap
) {
158 vmxregset
->avr
[i
].u64
[0] = bswap64(cpu
->env
.avr
[i
].u64
[1]);
159 vmxregset
->avr
[i
].u64
[1] = bswap64(cpu
->env
.avr
[i
].u64
[0]);
161 vmxregset
->avr
[i
].u64
[0] = cpu
->env
.avr
[i
].u64
[0];
162 vmxregset
->avr
[i
].u64
[1] = cpu
->env
.avr
[i
].u64
[1];
165 vmxregset
->vscr
.u32
[3] = cpu_to_dump32(s
, cpu
->env
.vscr
);
167 static void ppc64_write_elf64_vsxregset(NoteFuncArg
*arg
, PowerPCCPU
*cpu
)
170 struct PPC64ElfVsxregset
*vsxregset
;
171 Note
*note
= &arg
->note
;
172 DumpState
*s
= arg
->state
;
174 note
->hdr
.n_type
= cpu_to_dump32(s
, NT_PPC_VSX
);
175 vsxregset
= ¬e
->contents
.vsxregset
;
176 memset(vsxregset
, 0, sizeof(*vsxregset
));
178 for (i
= 0; i
< 32; i
++) {
179 vsxregset
->vsr
[i
] = cpu_to_dump64(s
, cpu
->env
.vsr
[i
]);
182 static void ppc64_write_elf64_speregset(NoteFuncArg
*arg
, PowerPCCPU
*cpu
)
184 struct PPC64ElfSperegset
*speregset
;
185 Note
*note
= &arg
->note
;
186 DumpState
*s
= arg
->state
;
188 note
->hdr
.n_type
= cpu_to_dump32(s
, NT_PPC_SPE
);
189 speregset
= ¬e
->contents
.speregset
;
190 memset(speregset
, 0, sizeof(*speregset
));
192 speregset
->spe_acc
= cpu_to_dump64(s
, cpu
->env
.spe_acc
);
193 speregset
->spe_fscr
= cpu_to_dump32(s
, cpu
->env
.spe_fscr
);
196 static const struct NoteFuncDescStruct
{
198 void (*note_contents_func
)(NoteFuncArg
*arg
, PowerPCCPU
*cpu
);
200 {sizeof(((Note
*)0)->contents
.prstatus
), ppc64_write_elf64_prstatus
},
201 {sizeof(((Note
*)0)->contents
.fpregset
), ppc64_write_elf64_fpregset
},
202 {sizeof(((Note
*)0)->contents
.vmxregset
), ppc64_write_elf64_vmxregset
},
203 {sizeof(((Note
*)0)->contents
.vsxregset
), ppc64_write_elf64_vsxregset
},
204 {sizeof(((Note
*)0)->contents
.speregset
), ppc64_write_elf64_speregset
},
208 typedef struct NoteFuncDescStruct NoteFuncDesc
;
210 int cpu_get_dump_info(ArchDumpInfo
*info
,
211 const struct GuestPhysBlockList
*guest_phys_blocks
)
213 PowerPCCPU
*cpu
= POWERPC_CPU(first_cpu
);
214 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
216 info
->d_machine
= EM_PPC64
;
217 info
->d_class
= ELFCLASS64
;
218 if ((*pcc
->interrupts_big_endian
)(cpu
)) {
219 info
->d_endian
= ELFDATA2MSB
;
221 info
->d_endian
= ELFDATA2LSB
;
227 ssize_t
cpu_get_note_size(int class, int machine
, int nr_cpus
)
229 int name_size
= 8; /* "CORE" or "QEMU" rounded */
230 size_t elf_note_size
= 0;
232 const NoteFuncDesc
*nf
;
234 if (class != ELFCLASS64
) {
237 assert(machine
== EM_PPC64
);
239 note_head_size
= sizeof(Elf64_Nhdr
);
241 for (nf
= note_func
; nf
->note_contents_func
; nf
++) {
242 elf_note_size
= elf_note_size
+ note_head_size
+ name_size
+
246 return (elf_note_size
) * nr_cpus
;
249 static int ppc64_write_all_elf64_notes(const char *note_name
,
250 WriteCoreDumpFunction f
,
251 PowerPCCPU
*cpu
, int id
,
254 NoteFuncArg arg
= { .state
= opaque
};
257 const NoteFuncDesc
*nf
;
259 for (nf
= note_func
; nf
->note_contents_func
; nf
++) {
260 arg
.note
.hdr
.n_namesz
= cpu_to_dump32(opaque
, sizeof(arg
.note
.name
));
261 arg
.note
.hdr
.n_descsz
= cpu_to_dump32(opaque
, nf
->contents_size
);
262 strncpy(arg
.note
.name
, note_name
, sizeof(arg
.note
.name
));
264 (*nf
->note_contents_func
)(&arg
, cpu
);
267 sizeof(arg
.note
) - sizeof(arg
.note
.contents
) + nf
->contents_size
;
268 ret
= f(&arg
.note
, note_size
, opaque
);
276 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cs
,
277 int cpuid
, void *opaque
)
279 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
280 return ppc64_write_all_elf64_notes("CORE", f
, cpu
, cpuid
, opaque
);