1 /*
2 * Support for writing ELF notes for LoongArch architectures
3 *
4 * Copyright (c) 2023 Loongarch Technology
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "elf.h"
23 #include "sysemu/dump.h"
24 #include "internals.h"
25
26 /* struct user_pt_regs from arch/loongarch/include/uapi/asm/ptrace.h */
27 struct loongarch_user_regs {
28 uint64_t gpr[32];
29 uint64_t pad1[1];
30 /* Special CSR registers. */
31 uint64_t csr_era;
32 uint64_t csr_badv;
33 uint64_t pad2[10];
34 } QEMU_PACKED;
35
36 QEMU_BUILD_BUG_ON(sizeof(struct loongarch_user_regs) != 360);
37
38 /* struct elf_prstatus from include/uapi/linux/elfcore.h */
39 struct loongarch_elf_prstatus {
40 char pad1[32]; /* 32 == offsetof(struct elf_prstatus, pr_pid) */
41 uint32_t pr_pid;
42 /*
43 * 76 == offsetof(struct elf_prstatus, pr_reg) -
44 * offsetof(struct elf_prstatus, pr_ppid)
45 */
46 char pad2[76];
47 struct loongarch_user_regs pr_reg;
48 uint32_t pr_fpvalid;
49 char pad3[4];
50 } QEMU_PACKED;
51
52 QEMU_BUILD_BUG_ON(sizeof(struct loongarch_elf_prstatus) != 480);
53
54 /* struct user_fp_state from arch/loongarch/include/uapi/asm/ptrace.h */
55 struct loongarch_fpu_struct {
56 uint64_t fpr[32];
57 uint64_t fcc;
58 unsigned int fcsr;
59 } QEMU_PACKED;
60
61 QEMU_BUILD_BUG_ON(sizeof(struct loongarch_fpu_struct) != 268);
62
63 struct loongarch_note {
64 Elf64_Nhdr hdr;
65 char name[8]; /* align_up(sizeof("CORE"), 4) */
66 union {
67 struct loongarch_elf_prstatus prstatus;
68 struct loongarch_fpu_struct fpu;
69 };
70 } QEMU_PACKED;
71
72 #define LOONGARCH_NOTE_HEADER_SIZE offsetof(struct loongarch_note, prstatus)
73 #define LOONGARCH_PRSTATUS_NOTE_SIZE \
74 (LOONGARCH_NOTE_HEADER_SIZE + sizeof(struct loongarch_elf_prstatus))
75 #define LOONGARCH_PRFPREG_NOTE_SIZE \
76 (LOONGARCH_NOTE_HEADER_SIZE + sizeof(struct loongarch_fpu_struct))
77
loongarch_note_init(struct loongarch_note * note,DumpState * s,const char * name,Elf64_Word namesz,Elf64_Word type,Elf64_Word descsz)78 static void loongarch_note_init(struct loongarch_note *note, DumpState *s,
79 const char *name, Elf64_Word namesz,
80 Elf64_Word type, Elf64_Word descsz)
81 {
82 memset(note, 0, sizeof(*note));
83
84 note->hdr.n_namesz = cpu_to_dump32(s, namesz);
85 note->hdr.n_descsz = cpu_to_dump32(s, descsz);
86 note->hdr.n_type = cpu_to_dump32(s, type);
87
88 memcpy(note->name, name, namesz);
89 }
90
loongarch_write_elf64_fprpreg(WriteCoreDumpFunction f,CPULoongArchState * env,int cpuid,DumpState * s)91 static int loongarch_write_elf64_fprpreg(WriteCoreDumpFunction f,
92 CPULoongArchState *env, int cpuid,
93 DumpState *s)
94 {
95 struct loongarch_note note;
96 int ret, i;
97
98 loongarch_note_init(¬e, s, "CORE", 5, NT_PRFPREG, sizeof(note.fpu));
99 note.fpu.fcsr = cpu_to_dump64(s, env->fcsr0);
100 note.fpu.fcc = cpu_to_dump64(s, read_fcc(env));
101
102 for (i = 0; i < 32; ++i) {
103 note.fpu.fpr[i] = cpu_to_dump64(s, env->fpr[i].vreg.UD[0]);
104 }
105
106 ret = f(¬e, LOONGARCH_PRFPREG_NOTE_SIZE, s);
107 if (ret < 0) {
108 return -1;
109 }
110
111 return 0;
112 }
113
loongarch_cpu_write_elf64_note(WriteCoreDumpFunction f,CPUState * cs,int cpuid,DumpState * s)114 int loongarch_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
115 int cpuid, DumpState *s)
116 {
117 struct loongarch_note note;
118 CPULoongArchState *env = &LOONGARCH_CPU(cs)->env;
119 int ret, i;
120
121 loongarch_note_init(¬e, s, "CORE", 5, NT_PRSTATUS,
122 sizeof(note.prstatus));
123 note.prstatus.pr_pid = cpu_to_dump32(s, cpuid);
124 note.prstatus.pr_fpvalid = cpu_to_dump32(s, 1);
125
126 for (i = 0; i < 32; ++i) {
127 note.prstatus.pr_reg.gpr[i] = cpu_to_dump64(s, env->gpr[i]);
128 }
129 note.prstatus.pr_reg.csr_era = cpu_to_dump64(s, env->CSR_ERA);
130 note.prstatus.pr_reg.csr_badv = cpu_to_dump64(s, env->CSR_BADV);
131 ret = f(¬e, LOONGARCH_PRSTATUS_NOTE_SIZE, s);
132 if (ret < 0) {
133 return -1;
134 }
135
136 ret = loongarch_write_elf64_fprpreg(f, env, cpuid, s);
137 if (ret < 0) {
138 return -1;
139 }
140
141 return ret;
142 }
143
cpu_get_dump_info(ArchDumpInfo * info,const GuestPhysBlockList * guest_phys_blocks)144 int cpu_get_dump_info(ArchDumpInfo *info,
145 const GuestPhysBlockList *guest_phys_blocks)
146 {
147 info->d_machine = EM_LOONGARCH;
148 info->d_endian = ELFDATA2LSB;
149 info->d_class = ELFCLASS64;
150
151 return 0;
152 }
153
cpu_get_note_size(int class,int machine,int nr_cpus)154 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
155 {
156 size_t note_size = 0;
157
158 if (class == ELFCLASS64) {
159 note_size = LOONGARCH_PRSTATUS_NOTE_SIZE + LOONGARCH_PRFPREG_NOTE_SIZE;
160 }
161
162 return note_size * nr_cpus;
163 }
164