xref: /qemu/cpu-target.c (revision cc944932ecef3b7a56ae62d89dd92fb9e56c5cc8)
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 "system/accel-ops.h"
25 #include "system/cpus.h"
26 #include "exec/tswap.h"
27 #include "exec/replay-core.h"
28 #include "exec/log.h"
29 #include "accel/accel-cpu-target.h"
30 #include "trace/trace-root.h"
31 
32 char *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 
47 const 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 
74 #ifndef cpu_list
75 static 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 
88 static 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 
99 void list_cpus(void)
100 {
101     cpu_list();
102 }
103 
104 /* enable or disable single step mode. EXCP_DEBUG is returned by the
105    CPU loop after each instruction */
106 void cpu_single_step(CPUState *cpu, int enabled)
107 {
108     if (cpu->singlestep_enabled != enabled) {
109         cpu->singlestep_enabled = enabled;
110 
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);
115         }
116 #endif
117 
118         trace_breakpoint_singlestep(cpu->cpu_index, enabled);
119     }
120 }
121 
122 void 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()) {
134         FILE *logfile = qemu_log_trylock();
135         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);
140             qemu_log_unlock(logfile);
141         }
142     }
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 
158 #undef target_words_bigendian
159 bool target_words_bigendian(void)
160 {
161     return TARGET_BIG_ENDIAN;
162 }
163 
164 const char *target_name(void)
165 {
166     return TARGET_NAME;
167 }
168