xref: /qemu/cpu-target.c (revision edbb66bb305b7d7f9b13734d222cc5e333180948)
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 "cpu.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
24 #include "qemu/qemu-print.h"
25 #include "system/accel-ops.h"
26 #include "system/cpus.h"
27 #include "exec/cpu-common.h"
28 #include "exec/tswap.h"
29 #include "exec/replay-core.h"
30 #include "exec/log.h"
31 #include "accel/accel-cpu-target.h"
32 #include "trace/trace-root.h"
33 
34 /* Validate correct placement of CPUArchState. */
35 QEMU_BUILD_BUG_ON(offsetof(ArchCPU, parent_obj) != 0);
36 QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) != sizeof(CPUState));
37 
38 char *cpu_model_from_type(const char *typename)
39 {
40     const char *suffix = "-" CPU_RESOLVING_TYPE;
41 
42     if (!object_class_by_name(typename)) {
43         return NULL;
44     }
45 
46     if (g_str_has_suffix(typename, suffix)) {
47         return g_strndup(typename, strlen(typename) - strlen(suffix));
48     }
49 
50     return g_strdup(typename);
51 }
52 
53 const char *parse_cpu_option(const char *cpu_option)
54 {
55     ObjectClass *oc;
56     CPUClass *cc;
57     gchar **model_pieces;
58     const char *cpu_type;
59 
60     model_pieces = g_strsplit(cpu_option, ",", 2);
61     if (!model_pieces[0]) {
62         error_report("-cpu option cannot be empty");
63         exit(1);
64     }
65 
66     oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
67     if (oc == NULL) {
68         error_report("unable to find CPU model '%s'", model_pieces[0]);
69         g_strfreev(model_pieces);
70         exit(EXIT_FAILURE);
71     }
72 
73     cpu_type = object_class_get_name(oc);
74     cc = CPU_CLASS(oc);
75     cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
76     g_strfreev(model_pieces);
77     return cpu_type;
78 }
79 
80 static 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 
93 void list_cpus(void)
94 {
95     CPUClass *cc = CPU_CLASS(object_class_by_name(CPU_RESOLVING_TYPE));
96 
97     if (cc->list_cpus) {
98         cc->list_cpus();
99     } else {
100         GSList *list;
101 
102         list = object_class_get_list_sorted(TYPE_CPU, false);
103         qemu_printf("Available CPUs:\n");
104         g_slist_foreach(list, cpu_list_entry, NULL);
105         g_slist_free(list);
106     }
107 }
108 
109 /* enable or disable single step mode. EXCP_DEBUG is returned by the
110    CPU loop after each instruction */
111 void cpu_single_step(CPUState *cpu, int enabled)
112 {
113     if (cpu->singlestep_enabled != enabled) {
114         cpu->singlestep_enabled = enabled;
115 
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);
120         }
121 #endif
122 
123         trace_breakpoint_singlestep(cpu->cpu_index, enabled);
124     }
125 }
126 
127 void 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()) {
139         FILE *logfile = qemu_log_trylock();
140         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);
145             qemu_log_unlock(logfile);
146         }
147     }
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 
163 #undef target_words_bigendian
164 bool target_words_bigendian(void)
165 {
166     return TARGET_BIG_ENDIAN;
167 }
168 
169 const char *target_name(void)
170 {
171     return TARGET_NAME;
172 }
173