blob: 7f3b244ed11e9b46a3e8478ae1d14deee1ea8946 [file] [log] [blame]
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +02001/*
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"
Pierrick Bouvier8d535c32025-03-24 21:58:48 -070021#include "cpu.h"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020022#include "qapi/error.h"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020023#include "qemu/error-report.h"
Gavin Shandfa47532023-11-15 09:56:03 +100024#include "qemu/qemu-print.h"
Philippe Mathieu-Daudé0f665362025-01-23 13:39:05 +010025#include "system/accel-ops.h"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010026#include "system/cpus.h"
Philippe Mathieu-Daudé42508262023-12-12 11:34:25 +010027#include "exec/tswap.h"
Philippe Mathieu-Daudé5b5968c2022-12-19 18:09:43 +010028#include "exec/replay-core.h"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020029#include "exec/log.h"
Philippe Mathieu-Daudéb12a0f82025-01-23 11:11:24 +010030#include "accel/accel-cpu-target.h"
Richard Hendersonad1a7062021-07-01 08:10:53 -070031#include "trace/trace-root.h"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020032
Pierrick Bouvier8d535c32025-03-24 21:58:48 -070033/* Validate correct placement of CPUArchState. */
34QEMU_BUILD_BUG_ON(offsetof(ArchCPU, parent_obj) != 0);
35QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) != sizeof(CPUState));
36
Gavin Shan445946f2023-11-15 09:56:02 +100037char *cpu_model_from_type(const char *typename)
38{
39 const char *suffix = "-" CPU_RESOLVING_TYPE;
40
41 if (!object_class_by_name(typename)) {
42 return NULL;
43 }
44
45 if (g_str_has_suffix(typename, suffix)) {
46 return g_strndup(typename, strlen(typename) - strlen(suffix));
47 }
48
49 return g_strdup(typename);
50}
51
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020052const char *parse_cpu_option(const char *cpu_option)
53{
54 ObjectClass *oc;
55 CPUClass *cc;
56 gchar **model_pieces;
57 const char *cpu_type;
58
59 model_pieces = g_strsplit(cpu_option, ",", 2);
60 if (!model_pieces[0]) {
61 error_report("-cpu option cannot be empty");
62 exit(1);
63 }
64
65 oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
66 if (oc == NULL) {
67 error_report("unable to find CPU model '%s'", model_pieces[0]);
68 g_strfreev(model_pieces);
69 exit(EXIT_FAILURE);
70 }
71
72 cpu_type = object_class_get_name(oc);
73 cc = CPU_CLASS(oc);
74 cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
75 g_strfreev(model_pieces);
76 return cpu_type;
77}
78
Gavin Shandfa47532023-11-15 09:56:03 +100079#ifndef cpu_list
80static void cpu_list_entry(gpointer data, gpointer user_data)
81{
82 CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
83 const char *typename = object_class_get_name(OBJECT_CLASS(data));
84 g_autofree char *model = cpu_model_from_type(typename);
85
86 if (cc->deprecation_note) {
87 qemu_printf(" %s (deprecated)\n", model);
88 } else {
89 qemu_printf(" %s\n", model);
90 }
91}
92
93static void cpu_list(void)
94{
95 GSList *list;
96
97 list = object_class_get_list_sorted(TYPE_CPU, false);
98 qemu_printf("Available CPUs:\n");
99 g_slist_foreach(list, cpu_list_entry, NULL);
100 g_slist_free(list);
101}
102#endif
103
Thomas Huthc138c3b2023-04-19 14:48:31 +0200104void list_cpus(void)
Philippe Mathieu-Daudé377bf6f2022-03-14 15:01:08 +0100105{
Philippe Mathieu-Daudé377bf6f2022-03-14 15:01:08 +0100106 cpu_list();
Philippe Mathieu-Daudé377bf6f2022-03-14 15:01:08 +0100107}
108
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200109/* enable or disable single step mode. EXCP_DEBUG is returned by the
110 CPU loop after each instruction */
111void cpu_single_step(CPUState *cpu, int enabled)
112{
113 if (cpu->singlestep_enabled != enabled) {
114 cpu->singlestep_enabled = enabled;
Mads Ynddal412ae122023-03-02 18:58:05 -0800115
116#if !defined(CONFIG_USER_ONLY)
117 const AccelOpsClass *ops = cpus_get_accel();
118 if (ops->update_guest_debug) {
119 ops->update_guest_debug(cpu);
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200120 }
Mads Ynddal412ae122023-03-02 18:58:05 -0800121#endif
122
Richard Hendersonad1a7062021-07-01 08:10:53 -0700123 trace_breakpoint_singlestep(cpu->cpu_index, enabled);
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200124 }
125}
126
127void cpu_abort(CPUState *cpu, const char *fmt, ...)
128{
129 va_list ap;
130 va_list ap2;
131
132 va_start(ap, fmt);
133 va_copy(ap2, ap);
134 fprintf(stderr, "qemu: fatal: ");
135 vfprintf(stderr, fmt, ap);
136 fprintf(stderr, "\n");
137 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
138 if (qemu_log_separate()) {
Richard Hendersonc60f5992022-04-17 11:29:47 -0700139 FILE *logfile = qemu_log_trylock();
Richard Henderson78b54852022-04-17 11:29:49 -0700140 if (logfile) {
141 fprintf(logfile, "qemu: fatal: ");
142 vfprintf(logfile, fmt, ap2);
143 fprintf(logfile, "\n");
144 cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
Richard Henderson78b54852022-04-17 11:29:49 -0700145 qemu_log_unlock(logfile);
146 }
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200147 }
148 va_end(ap2);
149 va_end(ap);
150 replay_finish();
151#if defined(CONFIG_USER_ONLY)
152 {
153 struct sigaction act;
154 sigfillset(&act.sa_mask);
155 act.sa_handler = SIG_DFL;
156 act.sa_flags = 0;
157 sigaction(SIGABRT, &act, NULL);
158 }
159#endif
160 abort();
161}
162
Pierrick Bouviercfac5cd2025-03-17 11:34:00 -0700163#undef target_words_bigendian
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200164bool target_words_bigendian(void)
165{
Thomas Huthded625e2023-09-07 13:35:00 +0200166 return TARGET_BIG_ENDIAN;
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200167}
168
Thomas Huth1077f502023-04-24 18:04:33 +0200169const char *target_name(void)
170{
171 return TARGET_NAME;
172}