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 "sysemu/cpu-timers.h"
30 #include "qapi/error.h"
31 #include "qemu/error-report.h"
32 #include "qemu/accel.h"
33 #include "qapi/qapi-builtin-visit.h"
34 #include "qemu/units.h"
35 #if !defined(CONFIG_USER_ONLY)
36 #include "hw/boards.h"
41 AccelState parent_obj
;
45 unsigned long tb_size
;
47 typedef struct TCGState TCGState
;
49 #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
51 DECLARE_INSTANCE_CHECKER(TCGState
, TCG_STATE
,
55 * We default to false if we know other options have been enabled
56 * which are currently incompatible with MTTCG. Otherwise when each
57 * guest (target) has been updated to support:
58 * - atomic instructions
59 * - memory ordering primitives (barriers)
60 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
62 * Once a guest architecture has been converted to the new primitives
63 * there are two remaining limitations to check.
65 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
66 * - The host must have a stronger memory order than the guest
68 * It may be possible in future to support strong guests on weak hosts
69 * but that will require tagging all load/stores in a guest with their
70 * implicit memory order requirements which would likely slow things
74 static bool check_tcg_memory_orders_compatible(void)
76 #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
77 return (TCG_GUEST_DEFAULT_MO
& ~TCG_TARGET_DEFAULT_MO
) == 0;
83 static bool default_mttcg_enabled(void)
85 if (icount_enabled() || TCG_OVERSIZED_GUEST
) {
88 #ifdef TARGET_SUPPORTS_MTTCG
89 return check_tcg_memory_orders_compatible();
96 static void tcg_accel_instance_init(Object
*obj
)
98 TCGState
*s
= TCG_STATE(obj
);
100 s
->mttcg_enabled
= default_mttcg_enabled();
102 /* If debugging enabled, default "auto on", otherwise off. */
103 #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY)
104 s
->splitwx_enabled
= -1;
106 s
->splitwx_enabled
= 0;
112 static int tcg_init_machine(MachineState
*ms
)
114 TCGState
*s
= TCG_STATE(current_accel());
115 #ifdef CONFIG_USER_ONLY
116 unsigned max_cpus
= 1;
118 unsigned max_cpus
= ms
->smp
.max_cpus
;
122 mttcg_enabled
= s
->mttcg_enabled
;
126 tcg_init(s
->tb_size
* MiB
, s
->splitwx_enabled
, max_cpus
);
128 #if defined(CONFIG_SOFTMMU)
130 * There's no guest base to take into account, so go ahead and
131 * initialize the prologue now.
133 tcg_prologue_init(tcg_ctx
);
139 static char *tcg_get_thread(Object
*obj
, Error
**errp
)
141 TCGState
*s
= TCG_STATE(obj
);
143 return g_strdup(s
->mttcg_enabled
? "multi" : "single");
146 static void tcg_set_thread(Object
*obj
, const char *value
, Error
**errp
)
148 TCGState
*s
= TCG_STATE(obj
);
150 if (strcmp(value
, "multi") == 0) {
151 if (TCG_OVERSIZED_GUEST
) {
152 error_setg(errp
, "No MTTCG when guest word size > hosts");
153 } else if (icount_enabled()) {
154 error_setg(errp
, "No MTTCG when icount is enabled");
156 #ifndef TARGET_SUPPORTS_MTTCG
157 warn_report("Guest not yet converted to MTTCG - "
158 "you may get unexpected results");
160 if (!check_tcg_memory_orders_compatible()) {
161 warn_report("Guest expects a stronger memory ordering "
162 "than the host provides");
163 error_printf("This may cause strange/hard to debug errors\n");
165 s
->mttcg_enabled
= true;
167 } else if (strcmp(value
, "single") == 0) {
168 s
->mttcg_enabled
= false;
170 error_setg(errp
, "Invalid 'thread' setting %s", value
);
174 static void tcg_get_tb_size(Object
*obj
, Visitor
*v
,
175 const char *name
, void *opaque
,
178 TCGState
*s
= TCG_STATE(obj
);
179 uint32_t value
= s
->tb_size
;
181 visit_type_uint32(v
, name
, &value
, errp
);
184 static void tcg_set_tb_size(Object
*obj
, Visitor
*v
,
185 const char *name
, void *opaque
,
188 TCGState
*s
= TCG_STATE(obj
);
191 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
198 static bool tcg_get_splitwx(Object
*obj
, Error
**errp
)
200 TCGState
*s
= TCG_STATE(obj
);
201 return s
->splitwx_enabled
;
204 static void tcg_set_splitwx(Object
*obj
, bool value
, Error
**errp
)
206 TCGState
*s
= TCG_STATE(obj
);
207 s
->splitwx_enabled
= value
;
210 static void tcg_accel_class_init(ObjectClass
*oc
, void *data
)
212 AccelClass
*ac
= ACCEL_CLASS(oc
);
214 ac
->init_machine
= tcg_init_machine
;
215 ac
->allowed
= &tcg_allowed
;
217 object_class_property_add_str(oc
, "thread",
221 object_class_property_add(oc
, "tb-size", "int",
222 tcg_get_tb_size
, tcg_set_tb_size
,
224 object_class_property_set_description(oc
, "tb-size",
225 "TCG translation block cache size");
227 object_class_property_add_bool(oc
, "split-wx",
228 tcg_get_splitwx
, tcg_set_splitwx
);
229 object_class_property_set_description(oc
, "split-wx",
230 "Map jit pages into separate RW and RX regions");
233 static const TypeInfo tcg_accel_type
= {
234 .name
= TYPE_TCG_ACCEL
,
235 .parent
= TYPE_ACCEL
,
236 .instance_init
= tcg_accel_instance_init
,
237 .class_init
= tcg_accel_class_init
,
238 .instance_size
= sizeof(TCGState
),
240 module_obj(TYPE_TCG_ACCEL
);
242 static void register_accel_types(void)
244 type_register_static(&tcg_accel_type
);
247 type_init(register_accel_types
);