1fcf5ef2aSThomas Huth /* Support for writing ELF notes for ARM architectures
2fcf5ef2aSThomas Huth *
3fcf5ef2aSThomas Huth * Copyright (C) 2015 Red Hat Inc.
4fcf5ef2aSThomas Huth *
5fcf5ef2aSThomas Huth * Author: Andrew Jones <drjones@redhat.com>
6fcf5ef2aSThomas Huth *
7fcf5ef2aSThomas Huth * This program is free software; you can redistribute it and/or modify
8fcf5ef2aSThomas Huth * it under the terms of the GNU General Public License as published by
9fcf5ef2aSThomas Huth * the Free Software Foundation; either version 2 of the License, or
10fcf5ef2aSThomas Huth * (at your option) any later version.
11fcf5ef2aSThomas Huth *
12fcf5ef2aSThomas Huth * This program is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15fcf5ef2aSThomas Huth * GNU General Public License for more details.
16fcf5ef2aSThomas Huth *
17fcf5ef2aSThomas Huth * You should have received a copy of the GNU General Public License along
18fcf5ef2aSThomas Huth * with this program; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth */
20fcf5ef2aSThomas Huth
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "elf.h"
24fcf5ef2aSThomas Huth #include "sysemu/dump.h"
25*5a534314SPeter Maydell #include "cpu-features.h"
26fcf5ef2aSThomas Huth
27fcf5ef2aSThomas Huth /* struct user_pt_regs from arch/arm64/include/uapi/asm/ptrace.h */
28fcf5ef2aSThomas Huth struct aarch64_user_regs {
29fcf5ef2aSThomas Huth uint64_t regs[31];
30fcf5ef2aSThomas Huth uint64_t sp;
31fcf5ef2aSThomas Huth uint64_t pc;
32fcf5ef2aSThomas Huth uint64_t pstate;
33fcf5ef2aSThomas Huth } QEMU_PACKED;
34fcf5ef2aSThomas Huth
35fcf5ef2aSThomas Huth QEMU_BUILD_BUG_ON(sizeof(struct aarch64_user_regs) != 272);
36fcf5ef2aSThomas Huth
37fcf5ef2aSThomas Huth /* struct elf_prstatus from include/uapi/linux/elfcore.h */
38fcf5ef2aSThomas Huth struct aarch64_elf_prstatus {
39fcf5ef2aSThomas Huth char pad1[32]; /* 32 == offsetof(struct elf_prstatus, pr_pid) */
40fcf5ef2aSThomas Huth uint32_t pr_pid;
41fcf5ef2aSThomas Huth char pad2[76]; /* 76 == offsetof(struct elf_prstatus, pr_reg) -
42fcf5ef2aSThomas Huth offsetof(struct elf_prstatus, pr_ppid) */
43fcf5ef2aSThomas Huth struct aarch64_user_regs pr_reg;
44fcf5ef2aSThomas Huth uint32_t pr_fpvalid;
45fcf5ef2aSThomas Huth char pad3[4];
46fcf5ef2aSThomas Huth } QEMU_PACKED;
47fcf5ef2aSThomas Huth
48fcf5ef2aSThomas Huth QEMU_BUILD_BUG_ON(sizeof(struct aarch64_elf_prstatus) != 392);
49fcf5ef2aSThomas Huth
50fcf5ef2aSThomas Huth /* struct user_fpsimd_state from arch/arm64/include/uapi/asm/ptrace.h
51fcf5ef2aSThomas Huth *
52fcf5ef2aSThomas Huth * While the vregs member of user_fpsimd_state is of type __uint128_t,
53fcf5ef2aSThomas Huth * QEMU uses an array of uint64_t, where the high half of the 128-bit
54fcf5ef2aSThomas Huth * value is always in the 2n+1'th index. Thus we also break the 128-
55fcf5ef2aSThomas Huth * bit values into two halves in this reproduction of user_fpsimd_state.
56fcf5ef2aSThomas Huth */
57fcf5ef2aSThomas Huth struct aarch64_user_vfp_state {
58fcf5ef2aSThomas Huth uint64_t vregs[64];
59fcf5ef2aSThomas Huth uint32_t fpsr;
60fcf5ef2aSThomas Huth uint32_t fpcr;
61fcf5ef2aSThomas Huth char pad[8];
62fcf5ef2aSThomas Huth } QEMU_PACKED;
63fcf5ef2aSThomas Huth
64fcf5ef2aSThomas Huth QEMU_BUILD_BUG_ON(sizeof(struct aarch64_user_vfp_state) != 528);
65fcf5ef2aSThomas Huth
66538baab2SAndrew Jones /* struct user_sve_header from arch/arm64/include/uapi/asm/ptrace.h */
67538baab2SAndrew Jones struct aarch64_user_sve_header {
68538baab2SAndrew Jones uint32_t size;
69538baab2SAndrew Jones uint32_t max_size;
70538baab2SAndrew Jones uint16_t vl;
71538baab2SAndrew Jones uint16_t max_vl;
72538baab2SAndrew Jones uint16_t flags;
73538baab2SAndrew Jones uint16_t reserved;
74538baab2SAndrew Jones } QEMU_PACKED;
75538baab2SAndrew Jones
76fcf5ef2aSThomas Huth struct aarch64_note {
77fcf5ef2aSThomas Huth Elf64_Nhdr hdr;
78fcf5ef2aSThomas Huth char name[8]; /* align_up(sizeof("CORE"), 4) */
79fcf5ef2aSThomas Huth union {
80fcf5ef2aSThomas Huth struct aarch64_elf_prstatus prstatus;
81fcf5ef2aSThomas Huth struct aarch64_user_vfp_state vfp;
82538baab2SAndrew Jones struct aarch64_user_sve_header sve;
83fcf5ef2aSThomas Huth };
84fcf5ef2aSThomas Huth } QEMU_PACKED;
85fcf5ef2aSThomas Huth
86fcf5ef2aSThomas Huth #define AARCH64_NOTE_HEADER_SIZE offsetof(struct aarch64_note, prstatus)
87fcf5ef2aSThomas Huth #define AARCH64_PRSTATUS_NOTE_SIZE \
88fcf5ef2aSThomas Huth (AARCH64_NOTE_HEADER_SIZE + sizeof(struct aarch64_elf_prstatus))
89fcf5ef2aSThomas Huth #define AARCH64_PRFPREG_NOTE_SIZE \
90fcf5ef2aSThomas Huth (AARCH64_NOTE_HEADER_SIZE + sizeof(struct aarch64_user_vfp_state))
91538baab2SAndrew Jones #define AARCH64_SVE_NOTE_SIZE(env) \
92538baab2SAndrew Jones (AARCH64_NOTE_HEADER_SIZE + sve_size(env))
93fcf5ef2aSThomas Huth
aarch64_note_init(struct aarch64_note * note,DumpState * s,const char * name,Elf64_Word namesz,Elf64_Word type,Elf64_Word descsz)94fcf5ef2aSThomas Huth static void aarch64_note_init(struct aarch64_note *note, DumpState *s,
95fcf5ef2aSThomas Huth const char *name, Elf64_Word namesz,
96fcf5ef2aSThomas Huth Elf64_Word type, Elf64_Word descsz)
97fcf5ef2aSThomas Huth {
98fcf5ef2aSThomas Huth memset(note, 0, sizeof(*note));
99fcf5ef2aSThomas Huth
100fcf5ef2aSThomas Huth note->hdr.n_namesz = cpu_to_dump32(s, namesz);
101fcf5ef2aSThomas Huth note->hdr.n_descsz = cpu_to_dump32(s, descsz);
102fcf5ef2aSThomas Huth note->hdr.n_type = cpu_to_dump32(s, type);
103fcf5ef2aSThomas Huth
104fcf5ef2aSThomas Huth memcpy(note->name, name, namesz);
105fcf5ef2aSThomas Huth }
106fcf5ef2aSThomas Huth
aarch64_write_elf64_prfpreg(WriteCoreDumpFunction f,CPUARMState * env,int cpuid,DumpState * s)107fcf5ef2aSThomas Huth static int aarch64_write_elf64_prfpreg(WriteCoreDumpFunction f,
108fcf5ef2aSThomas Huth CPUARMState *env, int cpuid,
109fcf5ef2aSThomas Huth DumpState *s)
110fcf5ef2aSThomas Huth {
111fcf5ef2aSThomas Huth struct aarch64_note note;
112fcf5ef2aSThomas Huth int ret, i;
113fcf5ef2aSThomas Huth
114fcf5ef2aSThomas Huth aarch64_note_init(¬e, s, "CORE", 5, NT_PRFPREG, sizeof(note.vfp));
115fcf5ef2aSThomas Huth
1169a2b5256SRichard Henderson for (i = 0; i < 32; ++i) {
1179a2b5256SRichard Henderson uint64_t *q = aa64_vfp_qreg(env, i);
1189a2b5256SRichard Henderson note.vfp.vregs[2 * i + 0] = cpu_to_dump64(s, q[0]);
1199a2b5256SRichard Henderson note.vfp.vregs[2 * i + 1] = cpu_to_dump64(s, q[1]);
120fcf5ef2aSThomas Huth }
121fcf5ef2aSThomas Huth
122fcf5ef2aSThomas Huth if (s->dump_info.d_endian == ELFDATA2MSB) {
123fcf5ef2aSThomas Huth /* For AArch64 we must always swap the vfp.regs's 2n and 2n+1
124fcf5ef2aSThomas Huth * entries when generating BE notes, because even big endian
125fcf5ef2aSThomas Huth * hosts use 2n+1 for the high half.
126fcf5ef2aSThomas Huth */
127fcf5ef2aSThomas Huth for (i = 0; i < 32; ++i) {
128fcf5ef2aSThomas Huth uint64_t tmp = note.vfp.vregs[2*i];
129fcf5ef2aSThomas Huth note.vfp.vregs[2 * i] = note.vfp.vregs[2 * i + 1];
130fcf5ef2aSThomas Huth note.vfp.vregs[2 * i + 1] = tmp;
131fcf5ef2aSThomas Huth }
132fcf5ef2aSThomas Huth }
133fcf5ef2aSThomas Huth
134fcf5ef2aSThomas Huth note.vfp.fpsr = cpu_to_dump32(s, vfp_get_fpsr(env));
135fcf5ef2aSThomas Huth note.vfp.fpcr = cpu_to_dump32(s, vfp_get_fpcr(env));
136fcf5ef2aSThomas Huth
137fcf5ef2aSThomas Huth ret = f(¬e, AARCH64_PRFPREG_NOTE_SIZE, s);
138fcf5ef2aSThomas Huth if (ret < 0) {
139fcf5ef2aSThomas Huth return -1;
140fcf5ef2aSThomas Huth }
141fcf5ef2aSThomas Huth
142fcf5ef2aSThomas Huth return 0;
143fcf5ef2aSThomas Huth }
144fcf5ef2aSThomas Huth
145538baab2SAndrew Jones #ifdef TARGET_AARCH64
sve_zreg_offset(uint32_t vq,int n)146538baab2SAndrew Jones static off_t sve_zreg_offset(uint32_t vq, int n)
147538baab2SAndrew Jones {
148538baab2SAndrew Jones off_t off = sizeof(struct aarch64_user_sve_header);
149538baab2SAndrew Jones return ROUND_UP(off, 16) + vq * 16 * n;
150538baab2SAndrew Jones }
151538baab2SAndrew Jones
sve_preg_offset(uint32_t vq,int n)152538baab2SAndrew Jones static off_t sve_preg_offset(uint32_t vq, int n)
153538baab2SAndrew Jones {
154538baab2SAndrew Jones return sve_zreg_offset(vq, 32) + vq * 16 / 8 * n;
155538baab2SAndrew Jones }
156538baab2SAndrew Jones
sve_fpsr_offset(uint32_t vq)157538baab2SAndrew Jones static off_t sve_fpsr_offset(uint32_t vq)
158538baab2SAndrew Jones {
159538baab2SAndrew Jones off_t off = sve_preg_offset(vq, 17);
160538baab2SAndrew Jones return ROUND_UP(off, 16);
161538baab2SAndrew Jones }
162538baab2SAndrew Jones
sve_fpcr_offset(uint32_t vq)163538baab2SAndrew Jones static off_t sve_fpcr_offset(uint32_t vq)
164538baab2SAndrew Jones {
165538baab2SAndrew Jones return sve_fpsr_offset(vq) + sizeof(uint32_t);
166538baab2SAndrew Jones }
167538baab2SAndrew Jones
sve_current_vq(CPUARMState * env)168538baab2SAndrew Jones static uint32_t sve_current_vq(CPUARMState *env)
169538baab2SAndrew Jones {
1705ef3cc56SRichard Henderson return sve_vqm1_for_el(env, arm_current_el(env)) + 1;
171538baab2SAndrew Jones }
172538baab2SAndrew Jones
sve_size_vq(uint32_t vq)173538baab2SAndrew Jones static size_t sve_size_vq(uint32_t vq)
174538baab2SAndrew Jones {
175538baab2SAndrew Jones off_t off = sve_fpcr_offset(vq) + sizeof(uint32_t);
176538baab2SAndrew Jones return ROUND_UP(off, 16);
177538baab2SAndrew Jones }
178538baab2SAndrew Jones
sve_size(CPUARMState * env)179538baab2SAndrew Jones static size_t sve_size(CPUARMState *env)
180538baab2SAndrew Jones {
181538baab2SAndrew Jones return sve_size_vq(sve_current_vq(env));
182538baab2SAndrew Jones }
183538baab2SAndrew Jones
aarch64_write_elf64_sve(WriteCoreDumpFunction f,CPUARMState * env,int cpuid,DumpState * s)184538baab2SAndrew Jones static int aarch64_write_elf64_sve(WriteCoreDumpFunction f,
185538baab2SAndrew Jones CPUARMState *env, int cpuid,
186538baab2SAndrew Jones DumpState *s)
187538baab2SAndrew Jones {
188538baab2SAndrew Jones struct aarch64_note *note;
189538baab2SAndrew Jones ARMCPU *cpu = env_archcpu(env);
190538baab2SAndrew Jones uint32_t vq = sve_current_vq(env);
191538baab2SAndrew Jones uint64_t tmp[ARM_MAX_VQ * 2], *r;
192538baab2SAndrew Jones uint32_t fpr;
193538baab2SAndrew Jones uint8_t *buf;
194538baab2SAndrew Jones int ret, i;
195538baab2SAndrew Jones
196538baab2SAndrew Jones note = g_malloc0(AARCH64_SVE_NOTE_SIZE(env));
197538baab2SAndrew Jones buf = (uint8_t *)¬e->sve;
198538baab2SAndrew Jones
199538baab2SAndrew Jones aarch64_note_init(note, s, "LINUX", 6, NT_ARM_SVE, sve_size_vq(vq));
200538baab2SAndrew Jones
201538baab2SAndrew Jones note->sve.size = cpu_to_dump32(s, sve_size_vq(vq));
202538baab2SAndrew Jones note->sve.max_size = cpu_to_dump32(s, sve_size_vq(cpu->sve_max_vq));
203538baab2SAndrew Jones note->sve.vl = cpu_to_dump16(s, vq * 16);
204538baab2SAndrew Jones note->sve.max_vl = cpu_to_dump16(s, cpu->sve_max_vq * 16);
205538baab2SAndrew Jones note->sve.flags = cpu_to_dump16(s, 1);
206538baab2SAndrew Jones
207538baab2SAndrew Jones for (i = 0; i < 32; ++i) {
208538baab2SAndrew Jones r = sve_bswap64(tmp, &env->vfp.zregs[i].d[0], vq * 2);
209538baab2SAndrew Jones memcpy(&buf[sve_zreg_offset(vq, i)], r, vq * 16);
210538baab2SAndrew Jones }
211538baab2SAndrew Jones
212538baab2SAndrew Jones for (i = 0; i < 17; ++i) {
213538baab2SAndrew Jones r = sve_bswap64(tmp, r = &env->vfp.pregs[i].p[0],
214538baab2SAndrew Jones DIV_ROUND_UP(vq * 2, 8));
215538baab2SAndrew Jones memcpy(&buf[sve_preg_offset(vq, i)], r, vq * 16 / 8);
216538baab2SAndrew Jones }
217538baab2SAndrew Jones
218538baab2SAndrew Jones fpr = cpu_to_dump32(s, vfp_get_fpsr(env));
219538baab2SAndrew Jones memcpy(&buf[sve_fpsr_offset(vq)], &fpr, sizeof(uint32_t));
220538baab2SAndrew Jones
221538baab2SAndrew Jones fpr = cpu_to_dump32(s, vfp_get_fpcr(env));
222538baab2SAndrew Jones memcpy(&buf[sve_fpcr_offset(vq)], &fpr, sizeof(uint32_t));
223538baab2SAndrew Jones
224538baab2SAndrew Jones ret = f(note, AARCH64_SVE_NOTE_SIZE(env), s);
225538baab2SAndrew Jones g_free(note);
226538baab2SAndrew Jones
227538baab2SAndrew Jones if (ret < 0) {
228538baab2SAndrew Jones return -1;
229538baab2SAndrew Jones }
230538baab2SAndrew Jones
231538baab2SAndrew Jones return 0;
232538baab2SAndrew Jones }
233538baab2SAndrew Jones #endif
234538baab2SAndrew Jones
arm_cpu_write_elf64_note(WriteCoreDumpFunction f,CPUState * cs,int cpuid,DumpState * s)235fcf5ef2aSThomas Huth int arm_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
2361af0006aSJanosch Frank int cpuid, DumpState *s)
237fcf5ef2aSThomas Huth {
238fcf5ef2aSThomas Huth struct aarch64_note note;
239538baab2SAndrew Jones ARMCPU *cpu = ARM_CPU(cs);
240538baab2SAndrew Jones CPUARMState *env = &cpu->env;
241fcf5ef2aSThomas Huth uint64_t pstate, sp;
242fcf5ef2aSThomas Huth int ret, i;
243fcf5ef2aSThomas Huth
244fcf5ef2aSThomas Huth aarch64_note_init(¬e, s, "CORE", 5, NT_PRSTATUS, sizeof(note.prstatus));
245fcf5ef2aSThomas Huth
246fcf5ef2aSThomas Huth note.prstatus.pr_pid = cpu_to_dump32(s, cpuid);
247fcf5ef2aSThomas Huth note.prstatus.pr_fpvalid = cpu_to_dump32(s, 1);
248fcf5ef2aSThomas Huth
249fcf5ef2aSThomas Huth if (!is_a64(env)) {
250fcf5ef2aSThomas Huth aarch64_sync_32_to_64(env);
251fcf5ef2aSThomas Huth pstate = cpsr_read(env);
252fcf5ef2aSThomas Huth sp = 0;
253fcf5ef2aSThomas Huth } else {
254fcf5ef2aSThomas Huth pstate = pstate_read(env);
255fcf5ef2aSThomas Huth sp = env->xregs[31];
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth
258fcf5ef2aSThomas Huth for (i = 0; i < 31; ++i) {
259fcf5ef2aSThomas Huth note.prstatus.pr_reg.regs[i] = cpu_to_dump64(s, env->xregs[i]);
260fcf5ef2aSThomas Huth }
261fcf5ef2aSThomas Huth note.prstatus.pr_reg.sp = cpu_to_dump64(s, sp);
262fcf5ef2aSThomas Huth note.prstatus.pr_reg.pc = cpu_to_dump64(s, env->pc);
263fcf5ef2aSThomas Huth note.prstatus.pr_reg.pstate = cpu_to_dump64(s, pstate);
264fcf5ef2aSThomas Huth
265fcf5ef2aSThomas Huth ret = f(¬e, AARCH64_PRSTATUS_NOTE_SIZE, s);
266fcf5ef2aSThomas Huth if (ret < 0) {
267fcf5ef2aSThomas Huth return -1;
268fcf5ef2aSThomas Huth }
269fcf5ef2aSThomas Huth
270538baab2SAndrew Jones ret = aarch64_write_elf64_prfpreg(f, env, cpuid, s);
271538baab2SAndrew Jones if (ret) {
272538baab2SAndrew Jones return ret;
273538baab2SAndrew Jones }
274538baab2SAndrew Jones
275538baab2SAndrew Jones #ifdef TARGET_AARCH64
276538baab2SAndrew Jones if (cpu_isar_feature(aa64_sve, cpu)) {
277538baab2SAndrew Jones ret = aarch64_write_elf64_sve(f, env, cpuid, s);
278538baab2SAndrew Jones }
279538baab2SAndrew Jones #endif
280538baab2SAndrew Jones
281538baab2SAndrew Jones return ret;
282fcf5ef2aSThomas Huth }
283fcf5ef2aSThomas Huth
284fcf5ef2aSThomas Huth /* struct pt_regs from arch/arm/include/asm/ptrace.h */
285fcf5ef2aSThomas Huth struct arm_user_regs {
286fcf5ef2aSThomas Huth uint32_t regs[17];
287fcf5ef2aSThomas Huth char pad[4];
288fcf5ef2aSThomas Huth } QEMU_PACKED;
289fcf5ef2aSThomas Huth
290fcf5ef2aSThomas Huth QEMU_BUILD_BUG_ON(sizeof(struct arm_user_regs) != 72);
291fcf5ef2aSThomas Huth
292fcf5ef2aSThomas Huth /* struct elf_prstatus from include/uapi/linux/elfcore.h */
293fcf5ef2aSThomas Huth struct arm_elf_prstatus {
294fcf5ef2aSThomas Huth char pad1[24]; /* 24 == offsetof(struct elf_prstatus, pr_pid) */
295fcf5ef2aSThomas Huth uint32_t pr_pid;
296fcf5ef2aSThomas Huth char pad2[44]; /* 44 == offsetof(struct elf_prstatus, pr_reg) -
297fcf5ef2aSThomas Huth offsetof(struct elf_prstatus, pr_ppid) */
298fcf5ef2aSThomas Huth struct arm_user_regs pr_reg;
299fcf5ef2aSThomas Huth uint32_t pr_fpvalid;
300fcf5ef2aSThomas Huth } QEMU_PACKED arm_elf_prstatus;
301fcf5ef2aSThomas Huth
302fcf5ef2aSThomas Huth QEMU_BUILD_BUG_ON(sizeof(struct arm_elf_prstatus) != 148);
303fcf5ef2aSThomas Huth
304fcf5ef2aSThomas Huth /* struct user_vfp from arch/arm/include/asm/user.h */
305fcf5ef2aSThomas Huth struct arm_user_vfp_state {
306fcf5ef2aSThomas Huth uint64_t vregs[32];
307fcf5ef2aSThomas Huth uint32_t fpscr;
308fcf5ef2aSThomas Huth } QEMU_PACKED;
309fcf5ef2aSThomas Huth
310fcf5ef2aSThomas Huth QEMU_BUILD_BUG_ON(sizeof(struct arm_user_vfp_state) != 260);
311fcf5ef2aSThomas Huth
312fcf5ef2aSThomas Huth struct arm_note {
313fcf5ef2aSThomas Huth Elf32_Nhdr hdr;
314fcf5ef2aSThomas Huth char name[8]; /* align_up(sizeof("LINUX"), 4) */
315fcf5ef2aSThomas Huth union {
316fcf5ef2aSThomas Huth struct arm_elf_prstatus prstatus;
317fcf5ef2aSThomas Huth struct arm_user_vfp_state vfp;
318fcf5ef2aSThomas Huth };
319fcf5ef2aSThomas Huth } QEMU_PACKED;
320fcf5ef2aSThomas Huth
321fcf5ef2aSThomas Huth #define ARM_NOTE_HEADER_SIZE offsetof(struct arm_note, prstatus)
322fcf5ef2aSThomas Huth #define ARM_PRSTATUS_NOTE_SIZE \
323fcf5ef2aSThomas Huth (ARM_NOTE_HEADER_SIZE + sizeof(struct arm_elf_prstatus))
324fcf5ef2aSThomas Huth #define ARM_VFP_NOTE_SIZE \
325fcf5ef2aSThomas Huth (ARM_NOTE_HEADER_SIZE + sizeof(struct arm_user_vfp_state))
326fcf5ef2aSThomas Huth
arm_note_init(struct arm_note * note,DumpState * s,const char * name,Elf32_Word namesz,Elf32_Word type,Elf32_Word descsz)327fcf5ef2aSThomas Huth static void arm_note_init(struct arm_note *note, DumpState *s,
328fcf5ef2aSThomas Huth const char *name, Elf32_Word namesz,
329fcf5ef2aSThomas Huth Elf32_Word type, Elf32_Word descsz)
330fcf5ef2aSThomas Huth {
331fcf5ef2aSThomas Huth memset(note, 0, sizeof(*note));
332fcf5ef2aSThomas Huth
333fcf5ef2aSThomas Huth note->hdr.n_namesz = cpu_to_dump32(s, namesz);
334fcf5ef2aSThomas Huth note->hdr.n_descsz = cpu_to_dump32(s, descsz);
335fcf5ef2aSThomas Huth note->hdr.n_type = cpu_to_dump32(s, type);
336fcf5ef2aSThomas Huth
337fcf5ef2aSThomas Huth memcpy(note->name, name, namesz);
338fcf5ef2aSThomas Huth }
339fcf5ef2aSThomas Huth
arm_write_elf32_vfp(WriteCoreDumpFunction f,CPUARMState * env,int cpuid,DumpState * s)340fcf5ef2aSThomas Huth static int arm_write_elf32_vfp(WriteCoreDumpFunction f, CPUARMState *env,
341fcf5ef2aSThomas Huth int cpuid, DumpState *s)
342fcf5ef2aSThomas Huth {
343fcf5ef2aSThomas Huth struct arm_note note;
344fcf5ef2aSThomas Huth int ret, i;
345fcf5ef2aSThomas Huth
346fcf5ef2aSThomas Huth arm_note_init(¬e, s, "LINUX", 6, NT_ARM_VFP, sizeof(note.vfp));
347fcf5ef2aSThomas Huth
348fcf5ef2aSThomas Huth for (i = 0; i < 32; ++i) {
3499a2b5256SRichard Henderson note.vfp.vregs[i] = cpu_to_dump64(s, *aa32_vfp_dreg(env, i));
350fcf5ef2aSThomas Huth }
351fcf5ef2aSThomas Huth
352fcf5ef2aSThomas Huth note.vfp.fpscr = cpu_to_dump32(s, vfp_get_fpscr(env));
353fcf5ef2aSThomas Huth
354fcf5ef2aSThomas Huth ret = f(¬e, ARM_VFP_NOTE_SIZE, s);
355fcf5ef2aSThomas Huth if (ret < 0) {
356fcf5ef2aSThomas Huth return -1;
357fcf5ef2aSThomas Huth }
358fcf5ef2aSThomas Huth
359fcf5ef2aSThomas Huth return 0;
360fcf5ef2aSThomas Huth }
361fcf5ef2aSThomas Huth
arm_cpu_write_elf32_note(WriteCoreDumpFunction f,CPUState * cs,int cpuid,DumpState * s)362fcf5ef2aSThomas Huth int arm_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
3631af0006aSJanosch Frank int cpuid, DumpState *s)
364fcf5ef2aSThomas Huth {
365fcf5ef2aSThomas Huth struct arm_note note;
3667fbc6a40SRichard Henderson ARMCPU *cpu = ARM_CPU(cs);
3677fbc6a40SRichard Henderson CPUARMState *env = &cpu->env;
3687fbc6a40SRichard Henderson int ret, i;
3697fbc6a40SRichard Henderson bool fpvalid = cpu_isar_feature(aa32_vfp_simd, cpu);
370fcf5ef2aSThomas Huth
371fcf5ef2aSThomas Huth arm_note_init(¬e, s, "CORE", 5, NT_PRSTATUS, sizeof(note.prstatus));
372fcf5ef2aSThomas Huth
373fcf5ef2aSThomas Huth note.prstatus.pr_pid = cpu_to_dump32(s, cpuid);
374fcf5ef2aSThomas Huth note.prstatus.pr_fpvalid = cpu_to_dump32(s, fpvalid);
375fcf5ef2aSThomas Huth
376fcf5ef2aSThomas Huth for (i = 0; i < 16; ++i) {
377fcf5ef2aSThomas Huth note.prstatus.pr_reg.regs[i] = cpu_to_dump32(s, env->regs[i]);
378fcf5ef2aSThomas Huth }
379fcf5ef2aSThomas Huth note.prstatus.pr_reg.regs[16] = cpu_to_dump32(s, cpsr_read(env));
380fcf5ef2aSThomas Huth
381fcf5ef2aSThomas Huth ret = f(¬e, ARM_PRSTATUS_NOTE_SIZE, s);
382fcf5ef2aSThomas Huth if (ret < 0) {
383fcf5ef2aSThomas Huth return -1;
384fcf5ef2aSThomas Huth } else if (fpvalid) {
385fcf5ef2aSThomas Huth return arm_write_elf32_vfp(f, env, cpuid, s);
386fcf5ef2aSThomas Huth }
387fcf5ef2aSThomas Huth
388fcf5ef2aSThomas Huth return 0;
389fcf5ef2aSThomas Huth }
390fcf5ef2aSThomas Huth
cpu_get_dump_info(ArchDumpInfo * info,const GuestPhysBlockList * guest_phys_blocks)391fcf5ef2aSThomas Huth int cpu_get_dump_info(ArchDumpInfo *info,
392fcf5ef2aSThomas Huth const GuestPhysBlockList *guest_phys_blocks)
393fcf5ef2aSThomas Huth {
3946dba6340SLaurent Vivier ARMCPU *cpu;
3956dba6340SLaurent Vivier CPUARMState *env;
396fcf5ef2aSThomas Huth GuestPhysBlock *block;
397fcf5ef2aSThomas Huth hwaddr lowest_addr = ULLONG_MAX;
398fcf5ef2aSThomas Huth
3996dba6340SLaurent Vivier if (first_cpu == NULL) {
4006dba6340SLaurent Vivier return -1;
4016dba6340SLaurent Vivier }
4026dba6340SLaurent Vivier
4036dba6340SLaurent Vivier cpu = ARM_CPU(first_cpu);
4046dba6340SLaurent Vivier env = &cpu->env;
4056dba6340SLaurent Vivier
406fcf5ef2aSThomas Huth /* Take a best guess at the phys_base. If we get it wrong then crash
407fcf5ef2aSThomas Huth * will need '--machdep phys_offset=<phys-offset>' added to its command
408fcf5ef2aSThomas Huth * line, which isn't any worse than assuming we can use zero, but being
409fcf5ef2aSThomas Huth * wrong. This is the same algorithm the crash utility uses when
410fcf5ef2aSThomas Huth * attempting to guess as it loads non-dumpfile formatted files.
411fcf5ef2aSThomas Huth */
412fcf5ef2aSThomas Huth QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
413fcf5ef2aSThomas Huth if (block->target_start < lowest_addr) {
414fcf5ef2aSThomas Huth lowest_addr = block->target_start;
415fcf5ef2aSThomas Huth }
416fcf5ef2aSThomas Huth }
417fcf5ef2aSThomas Huth
418fcf5ef2aSThomas Huth if (arm_feature(env, ARM_FEATURE_AARCH64)) {
419fcf5ef2aSThomas Huth info->d_machine = EM_AARCH64;
420fcf5ef2aSThomas Huth info->d_class = ELFCLASS64;
421fcf5ef2aSThomas Huth info->page_size = (1 << 16); /* aarch64 max pagesize */
422fcf5ef2aSThomas Huth if (lowest_addr != ULLONG_MAX) {
423fcf5ef2aSThomas Huth info->phys_base = lowest_addr;
424fcf5ef2aSThomas Huth }
425fcf5ef2aSThomas Huth } else {
426fcf5ef2aSThomas Huth info->d_machine = EM_ARM;
427fcf5ef2aSThomas Huth info->d_class = ELFCLASS32;
428fcf5ef2aSThomas Huth info->page_size = (1 << 12);
429fcf5ef2aSThomas Huth if (lowest_addr < UINT_MAX) {
430fcf5ef2aSThomas Huth info->phys_base = lowest_addr;
431fcf5ef2aSThomas Huth }
432fcf5ef2aSThomas Huth }
433fcf5ef2aSThomas Huth
434fcf5ef2aSThomas Huth /* We assume the relevant endianness is that of EL1; this is right
435fcf5ef2aSThomas Huth * for kernels, but might give the wrong answer if you're trying to
436fcf5ef2aSThomas Huth * dump a hypervisor that happens to be running an opposite-endian
437fcf5ef2aSThomas Huth * kernel.
438fcf5ef2aSThomas Huth */
439fcf5ef2aSThomas Huth info->d_endian = (env->cp15.sctlr_el[1] & SCTLR_EE) != 0
440fcf5ef2aSThomas Huth ? ELFDATA2MSB : ELFDATA2LSB;
441fcf5ef2aSThomas Huth
442fcf5ef2aSThomas Huth return 0;
443fcf5ef2aSThomas Huth }
444fcf5ef2aSThomas Huth
cpu_get_note_size(int class,int machine,int nr_cpus)445fcf5ef2aSThomas Huth ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
446fcf5ef2aSThomas Huth {
447fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(first_cpu);
448fcf5ef2aSThomas Huth size_t note_size;
449fcf5ef2aSThomas Huth
450fcf5ef2aSThomas Huth if (class == ELFCLASS64) {
451fcf5ef2aSThomas Huth note_size = AARCH64_PRSTATUS_NOTE_SIZE;
452fcf5ef2aSThomas Huth note_size += AARCH64_PRFPREG_NOTE_SIZE;
453538baab2SAndrew Jones #ifdef TARGET_AARCH64
454538baab2SAndrew Jones if (cpu_isar_feature(aa64_sve, cpu)) {
4557fbc6a40SRichard Henderson note_size += AARCH64_SVE_NOTE_SIZE(&cpu->env);
456538baab2SAndrew Jones }
457538baab2SAndrew Jones #endif
458fcf5ef2aSThomas Huth } else {
459fcf5ef2aSThomas Huth note_size = ARM_PRSTATUS_NOTE_SIZE;
4607fbc6a40SRichard Henderson if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
461fcf5ef2aSThomas Huth note_size += ARM_VFP_NOTE_SIZE;
462fcf5ef2aSThomas Huth }
463fcf5ef2aSThomas Huth }
464fcf5ef2aSThomas Huth
465fcf5ef2aSThomas Huth return note_size * nr_cpus;
466fcf5ef2aSThomas Huth }
467