2 * QEMU System Emulator, accelerator interfaces
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2014 Red Hat Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "sysemu/tcg.h"
28 #include "exec/replay-core.h"
29 #include "sysemu/cpu-timers.h"
31 #include "tcg/oversized-guest.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "qemu/accel.h"
35 #include "qemu/atomic.h"
36 #include "qapi/qapi-builtin-visit.h"
37 #include "qemu/units.h"
38 #if !defined(CONFIG_USER_ONLY)
39 #include "hw/boards.h"
44 AccelState parent_obj
;
49 unsigned long tb_size
;
51 typedef struct TCGState TCGState
;
53 #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
55 DECLARE_INSTANCE_CHECKER(TCGState
, TCG_STATE
,
59 * We default to false if we know other options have been enabled
60 * which are currently incompatible with MTTCG. Otherwise when each
61 * guest (target) has been updated to support:
62 * - atomic instructions
63 * - memory ordering primitives (barriers)
64 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
66 * Once a guest architecture has been converted to the new primitives
67 * there are two remaining limitations to check.
69 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
70 * - The host must have a stronger memory order than the guest
72 * It may be possible in future to support strong guests on weak hosts
73 * but that will require tagging all load/stores in a guest with their
74 * implicit memory order requirements which would likely slow things
78 static bool check_tcg_memory_orders_compatible(void)
80 #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
81 return (TCG_GUEST_DEFAULT_MO
& ~TCG_TARGET_DEFAULT_MO
) == 0;
87 static bool default_mttcg_enabled(void)
89 if (icount_enabled() || TCG_OVERSIZED_GUEST
) {
92 #ifdef TARGET_SUPPORTS_MTTCG
93 return check_tcg_memory_orders_compatible();
100 static void tcg_accel_instance_init(Object
*obj
)
102 TCGState
*s
= TCG_STATE(obj
);
104 s
->mttcg_enabled
= default_mttcg_enabled();
106 /* If debugging enabled, default "auto on", otherwise off. */
107 #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY)
108 s
->splitwx_enabled
= -1;
110 s
->splitwx_enabled
= 0;
115 bool one_insn_per_tb
;
117 static int tcg_init_machine(MachineState
*ms
)
119 TCGState
*s
= TCG_STATE(current_accel());
120 #ifdef CONFIG_USER_ONLY
121 unsigned max_cpus
= 1;
123 unsigned max_cpus
= ms
->smp
.max_cpus
;
127 mttcg_enabled
= s
->mttcg_enabled
;
131 tcg_init(s
->tb_size
* MiB
, s
->splitwx_enabled
, max_cpus
);
133 #if defined(CONFIG_SOFTMMU)
135 * There's no guest base to take into account, so go ahead and
136 * initialize the prologue now.
138 tcg_prologue_init(tcg_ctx
);
144 static char *tcg_get_thread(Object
*obj
, Error
**errp
)
146 TCGState
*s
= TCG_STATE(obj
);
148 return g_strdup(s
->mttcg_enabled
? "multi" : "single");
151 static void tcg_set_thread(Object
*obj
, const char *value
, Error
**errp
)
153 TCGState
*s
= TCG_STATE(obj
);
155 if (strcmp(value
, "multi") == 0) {
156 if (TCG_OVERSIZED_GUEST
) {
157 error_setg(errp
, "No MTTCG when guest word size > hosts");
158 } else if (icount_enabled()) {
159 error_setg(errp
, "No MTTCG when icount is enabled");
161 #ifndef TARGET_SUPPORTS_MTTCG
162 warn_report("Guest not yet converted to MTTCG - "
163 "you may get unexpected results");
165 if (!check_tcg_memory_orders_compatible()) {
166 warn_report("Guest expects a stronger memory ordering "
167 "than the host provides");
168 error_printf("This may cause strange/hard to debug errors\n");
170 s
->mttcg_enabled
= true;
172 } else if (strcmp(value
, "single") == 0) {
173 s
->mttcg_enabled
= false;
175 error_setg(errp
, "Invalid 'thread' setting %s", value
);
179 static void tcg_get_tb_size(Object
*obj
, Visitor
*v
,
180 const char *name
, void *opaque
,
183 TCGState
*s
= TCG_STATE(obj
);
184 uint32_t value
= s
->tb_size
;
186 visit_type_uint32(v
, name
, &value
, errp
);
189 static void tcg_set_tb_size(Object
*obj
, Visitor
*v
,
190 const char *name
, void *opaque
,
193 TCGState
*s
= TCG_STATE(obj
);
196 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
203 static bool tcg_get_splitwx(Object
*obj
, Error
**errp
)
205 TCGState
*s
= TCG_STATE(obj
);
206 return s
->splitwx_enabled
;
209 static void tcg_set_splitwx(Object
*obj
, bool value
, Error
**errp
)
211 TCGState
*s
= TCG_STATE(obj
);
212 s
->splitwx_enabled
= value
;
215 static bool tcg_get_one_insn_per_tb(Object
*obj
, Error
**errp
)
217 TCGState
*s
= TCG_STATE(obj
);
218 return s
->one_insn_per_tb
;
221 static void tcg_set_one_insn_per_tb(Object
*obj
, bool value
, Error
**errp
)
223 TCGState
*s
= TCG_STATE(obj
);
224 s
->one_insn_per_tb
= value
;
225 /* Set the global also: this changes the behaviour */
226 qatomic_set(&one_insn_per_tb
, value
);
229 static int tcg_gdbstub_supported_sstep_flags(void)
232 * In replay mode all events will come from the log and can't be
233 * suppressed otherwise we would break determinism. However as those
234 * events are tied to the number of executed instructions we won't see
235 * them occurring every time we single step.
237 if (replay_mode
!= REPLAY_MODE_NONE
) {
240 return SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
244 static void tcg_accel_class_init(ObjectClass
*oc
, void *data
)
246 AccelClass
*ac
= ACCEL_CLASS(oc
);
248 ac
->init_machine
= tcg_init_machine
;
249 ac
->allowed
= &tcg_allowed
;
250 ac
->gdbstub_supported_sstep_flags
= tcg_gdbstub_supported_sstep_flags
;
252 object_class_property_add_str(oc
, "thread",
256 object_class_property_add(oc
, "tb-size", "int",
257 tcg_get_tb_size
, tcg_set_tb_size
,
259 object_class_property_set_description(oc
, "tb-size",
260 "TCG translation block cache size");
262 object_class_property_add_bool(oc
, "split-wx",
263 tcg_get_splitwx
, tcg_set_splitwx
);
264 object_class_property_set_description(oc
, "split-wx",
265 "Map jit pages into separate RW and RX regions");
267 object_class_property_add_bool(oc
, "one-insn-per-tb",
268 tcg_get_one_insn_per_tb
,
269 tcg_set_one_insn_per_tb
);
270 object_class_property_set_description(oc
, "one-insn-per-tb",
271 "Only put one guest insn in each translation block");
274 static const TypeInfo tcg_accel_type
= {
275 .name
= TYPE_TCG_ACCEL
,
276 .parent
= TYPE_ACCEL
,
277 .instance_init
= tcg_accel_instance_init
,
278 .class_init
= tcg_accel_class_init
,
279 .instance_size
= sizeof(TCGState
),
281 module_obj(TYPE_TCG_ACCEL
);
283 static void register_accel_types(void)
285 type_register_static(&tcg_accel_type
);
288 type_init(register_accel_types
);