xref: /openbmc/qemu/target/loongarch/arch_dump.c (revision 990d2c18)
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 
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 
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(&note, s, "CORE", 5, NT_PRFPREG, sizeof(note.fpu));
99     note.fpu.fcsr = cpu_to_dump64(s, env->fcsr0);
100 
101     for (i = 0; i < 8; i++) {
102         note.fpu.fcc |= env->cf[i] << (8 * i);
103     }
104     note.fpu.fcc = cpu_to_dump64(s, note.fpu.fcc);
105 
106     for (i = 0; i < 32; ++i) {
107         note.fpu.fpr[i] = cpu_to_dump64(s, env->fpr[i].vreg.UD[0]);
108     }
109 
110     ret = f(&note, LOONGARCH_PRFPREG_NOTE_SIZE, s);
111     if (ret < 0) {
112         return -1;
113     }
114 
115     return 0;
116 }
117 
118 int loongarch_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
119                                    int cpuid, DumpState *s)
120 {
121     struct loongarch_note note;
122     CPULoongArchState *env = &LOONGARCH_CPU(cs)->env;
123     int ret, i;
124 
125     loongarch_note_init(&note, s, "CORE", 5, NT_PRSTATUS,
126                         sizeof(note.prstatus));
127     note.prstatus.pr_pid = cpu_to_dump32(s, cpuid);
128     note.prstatus.pr_fpvalid = cpu_to_dump32(s, 1);
129 
130     for (i = 0; i < 32; ++i) {
131         note.prstatus.pr_reg.gpr[i] = cpu_to_dump64(s, env->gpr[i]);
132     }
133     note.prstatus.pr_reg.csr_era  = cpu_to_dump64(s, env->CSR_ERA);
134     note.prstatus.pr_reg.csr_badv = cpu_to_dump64(s, env->CSR_BADV);
135     ret = f(&note, LOONGARCH_PRSTATUS_NOTE_SIZE, s);
136     if (ret < 0) {
137         return -1;
138     }
139 
140     ret = loongarch_write_elf64_fprpreg(f, env, cpuid, s);
141     if (ret < 0) {
142         return -1;
143     }
144 
145     return ret;
146 }
147 
148 int cpu_get_dump_info(ArchDumpInfo *info,
149                       const GuestPhysBlockList *guest_phys_blocks)
150 {
151     info->d_machine = EM_LOONGARCH;
152     info->d_endian = ELFDATA2LSB;
153     info->d_class = ELFCLASS64;
154 
155     return 0;
156 }
157 
158 ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
159 {
160     size_t note_size = 0;
161 
162     if (class == ELFCLASS64) {
163         note_size = LOONGARCH_PRSTATUS_NOTE_SIZE + LOONGARCH_PRFPREG_NOTE_SIZE;
164     }
165 
166     return note_size * nr_cpus;
167 }
168