xref: /openbmc/qemu/cpu-target.c (revision ca05578fc80f4253ed19f4c4128a4cbd5b83f0b5)
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/cpu-common.h"
29 #include "exec/log.h"
30 #include "accel/accel-cpu-target.h"
31 #include "trace/trace-root.h"
32 #include "qemu/accel.h"
33 #include "hw/core/cpu.h"
34 
35 bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
36 {
37     if (!accel_cpu_common_realize(cpu, errp)) {
38         return false;
39     }
40 
41     /* Wait until cpu initialization complete before exposing cpu. */
42     cpu_list_add(cpu);
43 
44     cpu_vmstate_register(cpu);
45 
46     return true;
47 }
48 
49 void cpu_exec_unrealizefn(CPUState *cpu)
50 {
51     cpu_vmstate_unregister(cpu);
52 
53     cpu_list_remove(cpu);
54     /*
55      * Now that the vCPU has been removed from the RCU list, we can call
56      * accel_cpu_common_unrealize, which may free fields using call_rcu.
57      */
58     accel_cpu_common_unrealize(cpu);
59 }
60 
61 char *cpu_model_from_type(const char *typename)
62 {
63     const char *suffix = "-" CPU_RESOLVING_TYPE;
64 
65     if (!object_class_by_name(typename)) {
66         return NULL;
67     }
68 
69     if (g_str_has_suffix(typename, suffix)) {
70         return g_strndup(typename, strlen(typename) - strlen(suffix));
71     }
72 
73     return g_strdup(typename);
74 }
75 
76 const char *parse_cpu_option(const char *cpu_option)
77 {
78     ObjectClass *oc;
79     CPUClass *cc;
80     gchar **model_pieces;
81     const char *cpu_type;
82 
83     model_pieces = g_strsplit(cpu_option, ",", 2);
84     if (!model_pieces[0]) {
85         error_report("-cpu option cannot be empty");
86         exit(1);
87     }
88 
89     oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
90     if (oc == NULL) {
91         error_report("unable to find CPU model '%s'", model_pieces[0]);
92         g_strfreev(model_pieces);
93         exit(EXIT_FAILURE);
94     }
95 
96     cpu_type = object_class_get_name(oc);
97     cc = CPU_CLASS(oc);
98     cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
99     g_strfreev(model_pieces);
100     return cpu_type;
101 }
102 
103 #ifndef cpu_list
104 static void cpu_list_entry(gpointer data, gpointer user_data)
105 {
106     CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
107     const char *typename = object_class_get_name(OBJECT_CLASS(data));
108     g_autofree char *model = cpu_model_from_type(typename);
109 
110     if (cc->deprecation_note) {
111         qemu_printf("  %s (deprecated)\n", model);
112     } else {
113         qemu_printf("  %s\n", model);
114     }
115 }
116 
117 static void cpu_list(void)
118 {
119     GSList *list;
120 
121     list = object_class_get_list_sorted(TYPE_CPU, false);
122     qemu_printf("Available CPUs:\n");
123     g_slist_foreach(list, cpu_list_entry, NULL);
124     g_slist_free(list);
125 }
126 #endif
127 
128 void list_cpus(void)
129 {
130     cpu_list();
131 }
132 
133 /* enable or disable single step mode. EXCP_DEBUG is returned by the
134    CPU loop after each instruction */
135 void cpu_single_step(CPUState *cpu, int enabled)
136 {
137     if (cpu->singlestep_enabled != enabled) {
138         cpu->singlestep_enabled = enabled;
139 
140 #if !defined(CONFIG_USER_ONLY)
141         const AccelOpsClass *ops = cpus_get_accel();
142         if (ops->update_guest_debug) {
143             ops->update_guest_debug(cpu);
144         }
145 #endif
146 
147         trace_breakpoint_singlestep(cpu->cpu_index, enabled);
148     }
149 }
150 
151 void cpu_abort(CPUState *cpu, const char *fmt, ...)
152 {
153     va_list ap;
154     va_list ap2;
155 
156     va_start(ap, fmt);
157     va_copy(ap2, ap);
158     fprintf(stderr, "qemu: fatal: ");
159     vfprintf(stderr, fmt, ap);
160     fprintf(stderr, "\n");
161     cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
162     if (qemu_log_separate()) {
163         FILE *logfile = qemu_log_trylock();
164         if (logfile) {
165             fprintf(logfile, "qemu: fatal: ");
166             vfprintf(logfile, fmt, ap2);
167             fprintf(logfile, "\n");
168             cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
169             qemu_log_unlock(logfile);
170         }
171     }
172     va_end(ap2);
173     va_end(ap);
174     replay_finish();
175 #if defined(CONFIG_USER_ONLY)
176     {
177         struct sigaction act;
178         sigfillset(&act.sa_mask);
179         act.sa_handler = SIG_DFL;
180         act.sa_flags = 0;
181         sigaction(SIGABRT, &act, NULL);
182     }
183 #endif
184     abort();
185 }
186 
187 bool target_words_bigendian(void)
188 {
189     return TARGET_BIG_ENDIAN;
190 }
191 
192 const char *target_name(void)
193 {
194     return TARGET_NAME;
195 }
196