xref: /qemu/cpu-target.c (revision 96adf9b404e51b9acdf9592595ad935905de1f4e)
1 /*
2  * Target-specific parts of the CPU object
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "qemu/error-report.h"
23 #include "qemu/qemu-print.h"
24 #include "migration/vmstate.h"
25 #ifndef CONFIG_USER_ONLY
26 #include "hw/core/sysemu-cpu-ops.h"
27 #endif
28 #include "system/accel-ops.h"
29 #include "system/cpus.h"
30 #include "system/tcg.h"
31 #include "exec/tswap.h"
32 #include "exec/replay-core.h"
33 #include "exec/cpu-common.h"
34 #include "exec/exec-all.h"
35 #include "exec/tb-flush.h"
36 #include "exec/log.h"
37 #include "accel/accel-cpu-target.h"
38 #include "trace/trace-root.h"
39 #include "qemu/accel.h"
40 #include "hw/core/cpu.h"
41 
42 #ifndef CONFIG_USER_ONLY
43 static int cpu_common_post_load(void *opaque, int version_id)
44 {
45     if (tcg_enabled()) {
46         CPUState *cpu = opaque;
47 
48         /*
49          * 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
50          * version_id is increased.
51          */
52         cpu->interrupt_request &= ~0x01;
53 
54         tlb_flush(cpu);
55 
56         /*
57          * loadvm has just updated the content of RAM, bypassing the
58          * usual mechanisms that ensure we flush TBs for writes to
59          * memory we've translated code from. So we must flush all TBs,
60          * which will now be stale.
61          */
62         tb_flush(cpu);
63     }
64 
65     return 0;
66 }
67 
68 static int cpu_common_pre_load(void *opaque)
69 {
70     CPUState *cpu = opaque;
71 
72     cpu->exception_index = -1;
73 
74     return 0;
75 }
76 
77 static bool cpu_common_exception_index_needed(void *opaque)
78 {
79     CPUState *cpu = opaque;
80 
81     return tcg_enabled() && cpu->exception_index != -1;
82 }
83 
84 static const VMStateDescription vmstate_cpu_common_exception_index = {
85     .name = "cpu_common/exception_index",
86     .version_id = 1,
87     .minimum_version_id = 1,
88     .needed = cpu_common_exception_index_needed,
89     .fields = (const VMStateField[]) {
90         VMSTATE_INT32(exception_index, CPUState),
91         VMSTATE_END_OF_LIST()
92     }
93 };
94 
95 static bool cpu_common_crash_occurred_needed(void *opaque)
96 {
97     CPUState *cpu = opaque;
98 
99     return cpu->crash_occurred;
100 }
101 
102 static const VMStateDescription vmstate_cpu_common_crash_occurred = {
103     .name = "cpu_common/crash_occurred",
104     .version_id = 1,
105     .minimum_version_id = 1,
106     .needed = cpu_common_crash_occurred_needed,
107     .fields = (const VMStateField[]) {
108         VMSTATE_BOOL(crash_occurred, CPUState),
109         VMSTATE_END_OF_LIST()
110     }
111 };
112 
113 const VMStateDescription vmstate_cpu_common = {
114     .name = "cpu_common",
115     .version_id = 1,
116     .minimum_version_id = 1,
117     .pre_load = cpu_common_pre_load,
118     .post_load = cpu_common_post_load,
119     .fields = (const VMStateField[]) {
120         VMSTATE_UINT32(halted, CPUState),
121         VMSTATE_UINT32(interrupt_request, CPUState),
122         VMSTATE_END_OF_LIST()
123     },
124     .subsections = (const VMStateDescription * const []) {
125         &vmstate_cpu_common_exception_index,
126         &vmstate_cpu_common_crash_occurred,
127         NULL
128     }
129 };
130 #endif
131 
132 bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
133 {
134     if (!accel_cpu_common_realize(cpu, errp)) {
135         return false;
136     }
137 
138     /* Wait until cpu initialization complete before exposing cpu. */
139     cpu_list_add(cpu);
140 
141 #ifdef CONFIG_USER_ONLY
142     assert(qdev_get_vmsd(DEVICE(cpu)) == NULL ||
143            qdev_get_vmsd(DEVICE(cpu))->unmigratable);
144 #else
145     if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
146         vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
147     }
148     if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
149         vmstate_register(NULL, cpu->cpu_index, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
150     }
151 #endif /* CONFIG_USER_ONLY */
152 
153     return true;
154 }
155 
156 void cpu_exec_unrealizefn(CPUState *cpu)
157 {
158 #ifndef CONFIG_USER_ONLY
159     CPUClass *cc = CPU_GET_CLASS(cpu);
160 
161     if (cc->sysemu_ops->legacy_vmsd != NULL) {
162         vmstate_unregister(NULL, cc->sysemu_ops->legacy_vmsd, cpu);
163     }
164     if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
165         vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
166     }
167 #endif
168 
169     cpu_list_remove(cpu);
170     /*
171      * Now that the vCPU has been removed from the RCU list, we can call
172      * accel_cpu_common_unrealize, which may free fields using call_rcu.
173      */
174     accel_cpu_common_unrealize(cpu);
175 }
176 
177 char *cpu_model_from_type(const char *typename)
178 {
179     const char *suffix = "-" CPU_RESOLVING_TYPE;
180 
181     if (!object_class_by_name(typename)) {
182         return NULL;
183     }
184 
185     if (g_str_has_suffix(typename, suffix)) {
186         return g_strndup(typename, strlen(typename) - strlen(suffix));
187     }
188 
189     return g_strdup(typename);
190 }
191 
192 const char *parse_cpu_option(const char *cpu_option)
193 {
194     ObjectClass *oc;
195     CPUClass *cc;
196     gchar **model_pieces;
197     const char *cpu_type;
198 
199     model_pieces = g_strsplit(cpu_option, ",", 2);
200     if (!model_pieces[0]) {
201         error_report("-cpu option cannot be empty");
202         exit(1);
203     }
204 
205     oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
206     if (oc == NULL) {
207         error_report("unable to find CPU model '%s'", model_pieces[0]);
208         g_strfreev(model_pieces);
209         exit(EXIT_FAILURE);
210     }
211 
212     cpu_type = object_class_get_name(oc);
213     cc = CPU_CLASS(oc);
214     cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
215     g_strfreev(model_pieces);
216     return cpu_type;
217 }
218 
219 #ifndef cpu_list
220 static void cpu_list_entry(gpointer data, gpointer user_data)
221 {
222     CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
223     const char *typename = object_class_get_name(OBJECT_CLASS(data));
224     g_autofree char *model = cpu_model_from_type(typename);
225 
226     if (cc->deprecation_note) {
227         qemu_printf("  %s (deprecated)\n", model);
228     } else {
229         qemu_printf("  %s\n", model);
230     }
231 }
232 
233 static void cpu_list(void)
234 {
235     GSList *list;
236 
237     list = object_class_get_list_sorted(TYPE_CPU, false);
238     qemu_printf("Available CPUs:\n");
239     g_slist_foreach(list, cpu_list_entry, NULL);
240     g_slist_free(list);
241 }
242 #endif
243 
244 void list_cpus(void)
245 {
246     cpu_list();
247 }
248 
249 /* enable or disable single step mode. EXCP_DEBUG is returned by the
250    CPU loop after each instruction */
251 void cpu_single_step(CPUState *cpu, int enabled)
252 {
253     if (cpu->singlestep_enabled != enabled) {
254         cpu->singlestep_enabled = enabled;
255 
256 #if !defined(CONFIG_USER_ONLY)
257         const AccelOpsClass *ops = cpus_get_accel();
258         if (ops->update_guest_debug) {
259             ops->update_guest_debug(cpu);
260         }
261 #endif
262 
263         trace_breakpoint_singlestep(cpu->cpu_index, enabled);
264     }
265 }
266 
267 void cpu_abort(CPUState *cpu, const char *fmt, ...)
268 {
269     va_list ap;
270     va_list ap2;
271 
272     va_start(ap, fmt);
273     va_copy(ap2, ap);
274     fprintf(stderr, "qemu: fatal: ");
275     vfprintf(stderr, fmt, ap);
276     fprintf(stderr, "\n");
277     cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
278     if (qemu_log_separate()) {
279         FILE *logfile = qemu_log_trylock();
280         if (logfile) {
281             fprintf(logfile, "qemu: fatal: ");
282             vfprintf(logfile, fmt, ap2);
283             fprintf(logfile, "\n");
284             cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
285             qemu_log_unlock(logfile);
286         }
287     }
288     va_end(ap2);
289     va_end(ap);
290     replay_finish();
291 #if defined(CONFIG_USER_ONLY)
292     {
293         struct sigaction act;
294         sigfillset(&act.sa_mask);
295         act.sa_handler = SIG_DFL;
296         act.sa_flags = 0;
297         sigaction(SIGABRT, &act, NULL);
298     }
299 #endif
300     abort();
301 }
302 
303 bool target_words_bigendian(void)
304 {
305     return TARGET_BIG_ENDIAN;
306 }
307 
308 const char *target_name(void)
309 {
310     return TARGET_NAME;
311 }
312