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/accel.h"
28 #include "sysemu/tcg.h"
29 #include "qom/object.h"
31 #include "sysemu/cpus.h"
32 #include "qemu/main-loop.h"
34 #include "qapi/error.h"
35 #include "qemu/error-report.h"
36 #include "hw/boards.h"
37 #include "qapi/qapi-builtin-visit.h"
39 typedef struct TCGState
{
40 AccelState parent_obj
;
43 unsigned long tb_size
;
46 #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
48 #define TCG_STATE(obj) \
49 OBJECT_CHECK(TCGState, (obj), TYPE_TCG_ACCEL)
51 /* mask must never be zero, except for A20 change call */
52 static void tcg_handle_interrupt(CPUState
*cpu
, int mask
)
55 g_assert(qemu_mutex_iothread_locked());
57 old_mask
= cpu
->interrupt_request
;
58 cpu
->interrupt_request
|= mask
;
61 * If called from iothread context, wake the target cpu in
64 if (!qemu_cpu_is_self(cpu
)) {
67 atomic_set(&cpu_neg(cpu
)->icount_decr
.u16
.high
, -1);
70 && (mask
& ~old_mask
) != 0) {
71 cpu_abort(cpu
, "Raised interrupt while not in I/O function");
77 * We default to false if we know other options have been enabled
78 * which are currently incompatible with MTTCG. Otherwise when each
79 * guest (target) has been updated to support:
80 * - atomic instructions
81 * - memory ordering primitives (barriers)
82 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
84 * Once a guest architecture has been converted to the new primitives
85 * there are two remaining limitations to check.
87 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
88 * - The host must have a stronger memory order than the guest
90 * It may be possible in future to support strong guests on weak hosts
91 * but that will require tagging all load/stores in a guest with their
92 * implicit memory order requirements which would likely slow things
96 static bool check_tcg_memory_orders_compatible(void)
98 #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
99 return (TCG_GUEST_DEFAULT_MO
& ~TCG_TARGET_DEFAULT_MO
) == 0;
105 static bool default_mttcg_enabled(void)
107 if (use_icount
|| TCG_OVERSIZED_GUEST
) {
110 #ifdef TARGET_SUPPORTS_MTTCG
111 return check_tcg_memory_orders_compatible();
118 static void tcg_accel_instance_init(Object
*obj
)
120 TCGState
*s
= TCG_STATE(obj
);
122 s
->mttcg_enabled
= default_mttcg_enabled();
125 static int tcg_init(MachineState
*ms
)
127 TCGState
*s
= TCG_STATE(current_accel());
129 tcg_exec_init(s
->tb_size
* 1024 * 1024);
130 cpu_interrupt_handler
= tcg_handle_interrupt
;
131 mttcg_enabled
= s
->mttcg_enabled
;
135 static char *tcg_get_thread(Object
*obj
, Error
**errp
)
137 TCGState
*s
= TCG_STATE(obj
);
139 return g_strdup(s
->mttcg_enabled
? "multi" : "single");
142 static void tcg_set_thread(Object
*obj
, const char *value
, Error
**errp
)
144 TCGState
*s
= TCG_STATE(obj
);
146 if (strcmp(value
, "multi") == 0) {
147 if (TCG_OVERSIZED_GUEST
) {
148 error_setg(errp
, "No MTTCG when guest word size > hosts");
149 } else if (use_icount
) {
150 error_setg(errp
, "No MTTCG when icount is enabled");
152 #ifndef TARGET_SUPPORTS_MTTCG
153 warn_report("Guest not yet converted to MTTCG - "
154 "you may get unexpected results");
156 if (!check_tcg_memory_orders_compatible()) {
157 warn_report("Guest expects a stronger memory ordering "
158 "than the host provides");
159 error_printf("This may cause strange/hard to debug errors\n");
161 s
->mttcg_enabled
= true;
163 } else if (strcmp(value
, "single") == 0) {
164 s
->mttcg_enabled
= false;
166 error_setg(errp
, "Invalid 'thread' setting %s", value
);
170 static void tcg_get_tb_size(Object
*obj
, Visitor
*v
,
171 const char *name
, void *opaque
,
174 TCGState
*s
= TCG_STATE(obj
);
175 uint32_t value
= s
->tb_size
;
177 visit_type_uint32(v
, name
, &value
, errp
);
180 static void tcg_set_tb_size(Object
*obj
, Visitor
*v
,
181 const char *name
, void *opaque
,
184 TCGState
*s
= TCG_STATE(obj
);
188 visit_type_uint32(v
, name
, &value
, &error
);
190 error_propagate(errp
, error
);
197 static void tcg_accel_class_init(ObjectClass
*oc
, void *data
)
199 AccelClass
*ac
= ACCEL_CLASS(oc
);
201 ac
->init_machine
= tcg_init
;
202 ac
->allowed
= &tcg_allowed
;
204 object_class_property_add_str(oc
, "thread",
208 object_class_property_add(oc
, "tb-size", "int",
209 tcg_get_tb_size
, tcg_set_tb_size
,
211 object_class_property_set_description(oc
, "tb-size",
212 "TCG translation block cache size");
216 static const TypeInfo tcg_accel_type
= {
217 .name
= TYPE_TCG_ACCEL
,
218 .parent
= TYPE_ACCEL
,
219 .instance_init
= tcg_accel_instance_init
,
220 .class_init
= tcg_accel_class_init
,
221 .instance_size
= sizeof(TCGState
),
224 static void register_accel_types(void)
226 type_register_static(&tcg_accel_type
);
229 type_init(register_accel_types
);