blob: cae77374b3849f7ba7f55a24943c4e5dfbbc453e [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"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020021#include "qapi/error.h"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020022#include "qemu/error-report.h"
Gavin Shandfa47532023-11-15 09:56:03 +100023#include "qemu/qemu-print.h"
Philippe Mathieu-Daudé0f665362025-01-23 13:39:05 +010024#include "system/accel-ops.h"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010025#include "system/cpus.h"
Philippe Mathieu-Daudé42508262023-12-12 11:34:25 +010026#include "exec/tswap.h"
Philippe Mathieu-Daudé5b5968c2022-12-19 18:09:43 +010027#include "exec/replay-core.h"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020028#include "exec/log.h"
Philippe Mathieu-Daudéb12a0f82025-01-23 11:11:24 +010029#include "accel/accel-cpu-target.h"
Richard Hendersonad1a7062021-07-01 08:10:53 -070030#include "trace/trace-root.h"
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020031
Gavin Shan445946f2023-11-15 09:56:02 +100032char *cpu_model_from_type(const char *typename)
33{
34 const char *suffix = "-" CPU_RESOLVING_TYPE;
35
36 if (!object_class_by_name(typename)) {
37 return NULL;
38 }
39
40 if (g_str_has_suffix(typename, suffix)) {
41 return g_strndup(typename, strlen(typename) - strlen(suffix));
42 }
43
44 return g_strdup(typename);
45}
46
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +020047const char *parse_cpu_option(const char *cpu_option)
48{
49 ObjectClass *oc;
50 CPUClass *cc;
51 gchar **model_pieces;
52 const char *cpu_type;
53
54 model_pieces = g_strsplit(cpu_option, ",", 2);
55 if (!model_pieces[0]) {
56 error_report("-cpu option cannot be empty");
57 exit(1);
58 }
59
60 oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
61 if (oc == NULL) {
62 error_report("unable to find CPU model '%s'", model_pieces[0]);
63 g_strfreev(model_pieces);
64 exit(EXIT_FAILURE);
65 }
66
67 cpu_type = object_class_get_name(oc);
68 cc = CPU_CLASS(oc);
69 cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
70 g_strfreev(model_pieces);
71 return cpu_type;
72}
73
Gavin Shandfa47532023-11-15 09:56:03 +100074#ifndef cpu_list
75static void cpu_list_entry(gpointer data, gpointer user_data)
76{
77 CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
78 const char *typename = object_class_get_name(OBJECT_CLASS(data));
79 g_autofree char *model = cpu_model_from_type(typename);
80
81 if (cc->deprecation_note) {
82 qemu_printf(" %s (deprecated)\n", model);
83 } else {
84 qemu_printf(" %s\n", model);
85 }
86}
87
88static void cpu_list(void)
89{
90 GSList *list;
91
92 list = object_class_get_list_sorted(TYPE_CPU, false);
93 qemu_printf("Available CPUs:\n");
94 g_slist_foreach(list, cpu_list_entry, NULL);
95 g_slist_free(list);
96}
97#endif
98
Thomas Huthc138c3b2023-04-19 14:48:31 +020099void list_cpus(void)
Philippe Mathieu-Daudé377bf6f2022-03-14 15:01:08 +0100100{
Philippe Mathieu-Daudé377bf6f2022-03-14 15:01:08 +0100101 cpu_list();
Philippe Mathieu-Daudé377bf6f2022-03-14 15:01:08 +0100102}
103
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200104/* enable or disable single step mode. EXCP_DEBUG is returned by the
105 CPU loop after each instruction */
106void cpu_single_step(CPUState *cpu, int enabled)
107{
108 if (cpu->singlestep_enabled != enabled) {
109 cpu->singlestep_enabled = enabled;
Mads Ynddal412ae122023-03-02 18:58:05 -0800110
111#if !defined(CONFIG_USER_ONLY)
112 const AccelOpsClass *ops = cpus_get_accel();
113 if (ops->update_guest_debug) {
114 ops->update_guest_debug(cpu);
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200115 }
Mads Ynddal412ae122023-03-02 18:58:05 -0800116#endif
117
Richard Hendersonad1a7062021-07-01 08:10:53 -0700118 trace_breakpoint_singlestep(cpu->cpu_index, enabled);
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200119 }
120}
121
122void cpu_abort(CPUState *cpu, const char *fmt, ...)
123{
124 va_list ap;
125 va_list ap2;
126
127 va_start(ap, fmt);
128 va_copy(ap2, ap);
129 fprintf(stderr, "qemu: fatal: ");
130 vfprintf(stderr, fmt, ap);
131 fprintf(stderr, "\n");
132 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
133 if (qemu_log_separate()) {
Richard Hendersonc60f5992022-04-17 11:29:47 -0700134 FILE *logfile = qemu_log_trylock();
Richard Henderson78b54852022-04-17 11:29:49 -0700135 if (logfile) {
136 fprintf(logfile, "qemu: fatal: ");
137 vfprintf(logfile, fmt, ap2);
138 fprintf(logfile, "\n");
139 cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
Richard Henderson78b54852022-04-17 11:29:49 -0700140 qemu_log_unlock(logfile);
141 }
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200142 }
143 va_end(ap2);
144 va_end(ap);
145 replay_finish();
146#if defined(CONFIG_USER_ONLY)
147 {
148 struct sigaction act;
149 sigfillset(&act.sa_mask);
150 act.sa_handler = SIG_DFL;
151 act.sa_flags = 0;
152 sigaction(SIGABRT, &act, NULL);
153 }
154#endif
155 abort();
156}
157
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200158bool target_words_bigendian(void)
159{
Thomas Huthded625e2023-09-07 13:35:00 +0200160 return TARGET_BIG_ENDIAN;
Paolo Bonzinid9f24bf2020-10-06 09:05:29 +0200161}
162
Thomas Huth1077f502023-04-24 18:04:33 +0200163const char *target_name(void)
164{
165 return TARGET_NAME;
166}