xref: /qemu/accel/tcg/tcg-all.c (revision 60b2c2e66b81c323c0bc70ea4233cdbf8cdae5b2)
1a9ded601SYang Zhong /*
2a9ded601SYang Zhong  * QEMU System Emulator, accelerator interfaces
3a9ded601SYang Zhong  *
4a9ded601SYang Zhong  * Copyright (c) 2003-2008 Fabrice Bellard
5a9ded601SYang Zhong  * Copyright (c) 2014 Red Hat Inc.
6a9ded601SYang Zhong  *
7a9ded601SYang Zhong  * Permission is hereby granted, free of charge, to any person obtaining a copy
8a9ded601SYang Zhong  * of this software and associated documentation files (the "Software"), to deal
9a9ded601SYang Zhong  * in the Software without restriction, including without limitation the rights
10a9ded601SYang Zhong  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11a9ded601SYang Zhong  * copies of the Software, and to permit persons to whom the Software is
12a9ded601SYang Zhong  * furnished to do so, subject to the following conditions:
13a9ded601SYang Zhong  *
14a9ded601SYang Zhong  * The above copyright notice and this permission notice shall be included in
15a9ded601SYang Zhong  * all copies or substantial portions of the Software.
16a9ded601SYang Zhong  *
17a9ded601SYang Zhong  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18a9ded601SYang Zhong  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19a9ded601SYang Zhong  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20a9ded601SYang Zhong  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21a9ded601SYang Zhong  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22a9ded601SYang Zhong  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23a9ded601SYang Zhong  * THE SOFTWARE.
24a9ded601SYang Zhong  */
25a9ded601SYang Zhong 
26a9ded601SYang Zhong #include "qemu/osdep.h"
2732cad1ffSPhilippe Mathieu-Daudé #include "system/tcg.h"
285b5968c4SPhilippe Mathieu-Daudé #include "exec/replay-core.h"
29161f5bc8SRichard Henderson #include "exec/icount.h"
30d7ec12f8SRichard Henderson #include "tcg/startup.h"
31dd680bf3SPhilippe Mathieu-Daudé #include "qapi/error.h"
32dd680bf3SPhilippe Mathieu-Daudé #include "qemu/error-report.h"
33940e43aaSClaudio Fontana #include "qemu/accel.h"
340e33928cSPeter Maydell #include "qemu/atomic.h"
35fe174132SPaolo Bonzini #include "qapi/qapi-builtin-visit.h"
36efba8ae9SRichard Henderson #include "qemu/units.h"
37558ee1edSPhilippe Mathieu-Daudé #if defined(CONFIG_USER_ONLY)
38558ee1edSPhilippe Mathieu-Daudé #include "hw/qdev-core.h"
39558ee1edSPhilippe Mathieu-Daudé #else
4043b972b7SRichard Henderson #include "hw/boards.h"
41*60b2c2e6SPhilippe Mathieu-Daudé #include "system/tcg.h"
4243b972b7SRichard Henderson #endif
43eeb6198eSPhilippe Mathieu-Daudé #include "internal-common.h"
44f441b4d1SRichard Henderson #include "cpu-param.h"
45f441b4d1SRichard Henderson 
46940e43aaSClaudio Fontana 
47db1015e9SEduardo Habkost struct TCGState {
4812ceaef6SPaolo Bonzini     AccelState parent_obj;
4912ceaef6SPaolo Bonzini 
5012ceaef6SPaolo Bonzini     bool mttcg_enabled;
513cfb0456SPeter Maydell     bool one_insn_per_tb;
52a35b3e14SRichard Henderson     int splitwx_enabled;
53fe174132SPaolo Bonzini     unsigned long tb_size;
54db1015e9SEduardo Habkost };
55db1015e9SEduardo Habkost typedef struct TCGState TCGState;
5612ceaef6SPaolo Bonzini 
5712ceaef6SPaolo Bonzini #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
5812ceaef6SPaolo Bonzini 
598110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(TCGState, TCG_STATE,
608110fa1dSEduardo Habkost                          TYPE_TCG_ACCEL)
61a9ded601SYang Zhong 
62*60b2c2e6SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
63*60b2c2e6SPhilippe Mathieu-Daudé 
64*60b2c2e6SPhilippe Mathieu-Daudé static bool mttcg_enabled;
65*60b2c2e6SPhilippe Mathieu-Daudé 
66*60b2c2e6SPhilippe Mathieu-Daudé bool qemu_tcg_mttcg_enabled(void)
67*60b2c2e6SPhilippe Mathieu-Daudé {
68*60b2c2e6SPhilippe Mathieu-Daudé     return mttcg_enabled;
69*60b2c2e6SPhilippe Mathieu-Daudé }
70*60b2c2e6SPhilippe Mathieu-Daudé 
71*60b2c2e6SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
72*60b2c2e6SPhilippe Mathieu-Daudé 
73af0440aeSPaolo Bonzini /*
74af0440aeSPaolo Bonzini  * We default to false if we know other options have been enabled
75af0440aeSPaolo Bonzini  * which are currently incompatible with MTTCG. Otherwise when each
76af0440aeSPaolo Bonzini  * guest (target) has been updated to support:
77af0440aeSPaolo Bonzini  *   - atomic instructions
78af0440aeSPaolo Bonzini  *   - memory ordering primitives (barriers)
79af0440aeSPaolo Bonzini  * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
80af0440aeSPaolo Bonzini  *
81af0440aeSPaolo Bonzini  * Once a guest architecture has been converted to the new primitives
8297e15769SRichard Henderson  * there is one remaining limitation to check:
83af0440aeSPaolo Bonzini  *   - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
84af0440aeSPaolo Bonzini  */
85af0440aeSPaolo Bonzini 
86af0440aeSPaolo Bonzini static bool default_mttcg_enabled(void)
87af0440aeSPaolo Bonzini {
88f441b4d1SRichard Henderson     if (icount_enabled()) {
89af0440aeSPaolo Bonzini         return false;
9097e15769SRichard Henderson     }
91af0440aeSPaolo Bonzini #ifdef TARGET_SUPPORTS_MTTCG
9297e15769SRichard Henderson     return true;
93af0440aeSPaolo Bonzini #else
94af0440aeSPaolo Bonzini     return false;
95af0440aeSPaolo Bonzini #endif
96af0440aeSPaolo Bonzini }
97af0440aeSPaolo Bonzini 
98af0440aeSPaolo Bonzini static void tcg_accel_instance_init(Object *obj)
99af0440aeSPaolo Bonzini {
10012ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
10112ceaef6SPaolo Bonzini 
10212ceaef6SPaolo Bonzini     s->mttcg_enabled = default_mttcg_enabled();
103a35b3e14SRichard Henderson 
104a35b3e14SRichard Henderson     /* If debugging enabled, default "auto on", otherwise off. */
105940e43aaSClaudio Fontana #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY)
106a35b3e14SRichard Henderson     s->splitwx_enabled = -1;
107a35b3e14SRichard Henderson #else
108a35b3e14SRichard Henderson     s->splitwx_enabled = 0;
109a35b3e14SRichard Henderson #endif
110af0440aeSPaolo Bonzini }
111af0440aeSPaolo Bonzini 
1120e33928cSPeter Maydell bool one_insn_per_tb;
113a77dabc3SClaudio Fontana 
1147109ef15SRichard Henderson static int tcg_init_machine(MachineState *ms)
115a9ded601SYang Zhong {
1164f7f5893SPhilippe Mathieu-Daudé     TCGState *s = TCG_STATE(current_accel());
117a9d107faSRichard Henderson     unsigned max_threads = 1;
11812ceaef6SPaolo Bonzini 
119fa79cde6SRichard Henderson     tcg_allowed = true;
120fa79cde6SRichard Henderson 
121fa79cde6SRichard Henderson     page_init();
122fa79cde6SRichard Henderson     tb_htable_init();
123a9d107faSRichard Henderson 
124a9d107faSRichard Henderson #ifndef CONFIG_USER_ONLY
125*60b2c2e6SPhilippe Mathieu-Daudé     if (s->mttcg_enabled) {
126a9d107faSRichard Henderson         max_threads = ms->smp.max_cpus;
127*60b2c2e6SPhilippe Mathieu-Daudé         mttcg_enabled = true;
128a9d107faSRichard Henderson     }
129a9d107faSRichard Henderson #endif
130a9d107faSRichard Henderson     tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_threads);
131fa79cde6SRichard Henderson 
132fa79cde6SRichard Henderson #if defined(CONFIG_SOFTMMU)
133fa79cde6SRichard Henderson     /*
134fa79cde6SRichard Henderson      * There's no guest base to take into account, so go ahead and
135fa79cde6SRichard Henderson      * initialize the prologue now.
136fa79cde6SRichard Henderson      */
137935f75aeSRichard Henderson     tcg_prologue_init();
138fa79cde6SRichard Henderson #endif
139fa79cde6SRichard Henderson 
140558ee1edSPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY
141558ee1edSPhilippe Mathieu-Daudé     qdev_create_fake_machine();
142558ee1edSPhilippe Mathieu-Daudé #endif
143558ee1edSPhilippe Mathieu-Daudé 
144a9ded601SYang Zhong     return 0;
145a9ded601SYang Zhong }
146a9ded601SYang Zhong 
14712ceaef6SPaolo Bonzini static char *tcg_get_thread(Object *obj, Error **errp)
148af0440aeSPaolo Bonzini {
14912ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
15012ceaef6SPaolo Bonzini 
15112ceaef6SPaolo Bonzini     return g_strdup(s->mttcg_enabled ? "multi" : "single");
152af0440aeSPaolo Bonzini }
15312ceaef6SPaolo Bonzini 
15412ceaef6SPaolo Bonzini static void tcg_set_thread(Object *obj, const char *value, Error **errp)
15512ceaef6SPaolo Bonzini {
15612ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
15712ceaef6SPaolo Bonzini 
15812ceaef6SPaolo Bonzini     if (strcmp(value, "multi") == 0) {
159f441b4d1SRichard Henderson         if (icount_enabled()) {
160af0440aeSPaolo Bonzini             error_setg(errp, "No MTTCG when icount is enabled");
161af0440aeSPaolo Bonzini         } else {
162af0440aeSPaolo Bonzini #ifndef TARGET_SUPPORTS_MTTCG
163af0440aeSPaolo Bonzini             warn_report("Guest not yet converted to MTTCG - "
164af0440aeSPaolo Bonzini                         "you may get unexpected results");
165af0440aeSPaolo Bonzini #endif
16612ceaef6SPaolo Bonzini             s->mttcg_enabled = true;
167af0440aeSPaolo Bonzini         }
16812ceaef6SPaolo Bonzini     } else if (strcmp(value, "single") == 0) {
16912ceaef6SPaolo Bonzini         s->mttcg_enabled = false;
170af0440aeSPaolo Bonzini     } else {
17112ceaef6SPaolo Bonzini         error_setg(errp, "Invalid 'thread' setting %s", value);
172af0440aeSPaolo Bonzini     }
173af0440aeSPaolo Bonzini }
174af0440aeSPaolo Bonzini 
175fe174132SPaolo Bonzini static void tcg_get_tb_size(Object *obj, Visitor *v,
176fe174132SPaolo Bonzini                             const char *name, void *opaque,
177fe174132SPaolo Bonzini                             Error **errp)
178fe174132SPaolo Bonzini {
179fe174132SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
180fe174132SPaolo Bonzini     uint32_t value = s->tb_size;
181fe174132SPaolo Bonzini 
182fe174132SPaolo Bonzini     visit_type_uint32(v, name, &value, errp);
183fe174132SPaolo Bonzini }
184fe174132SPaolo Bonzini 
185fe174132SPaolo Bonzini static void tcg_set_tb_size(Object *obj, Visitor *v,
186fe174132SPaolo Bonzini                             const char *name, void *opaque,
187fe174132SPaolo Bonzini                             Error **errp)
188fe174132SPaolo Bonzini {
189fe174132SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
190fe174132SPaolo Bonzini     uint32_t value;
191fe174132SPaolo Bonzini 
192668f62ecSMarkus Armbruster     if (!visit_type_uint32(v, name, &value, errp)) {
193fe174132SPaolo Bonzini         return;
194fe174132SPaolo Bonzini     }
195fe174132SPaolo Bonzini 
196fe174132SPaolo Bonzini     s->tb_size = value;
197fe174132SPaolo Bonzini }
198fe174132SPaolo Bonzini 
199a35b3e14SRichard Henderson static bool tcg_get_splitwx(Object *obj, Error **errp)
200a35b3e14SRichard Henderson {
201a35b3e14SRichard Henderson     TCGState *s = TCG_STATE(obj);
202a35b3e14SRichard Henderson     return s->splitwx_enabled;
203a35b3e14SRichard Henderson }
204a35b3e14SRichard Henderson 
205a35b3e14SRichard Henderson static void tcg_set_splitwx(Object *obj, bool value, Error **errp)
206a35b3e14SRichard Henderson {
207a35b3e14SRichard Henderson     TCGState *s = TCG_STATE(obj);
208a35b3e14SRichard Henderson     s->splitwx_enabled = value;
209a35b3e14SRichard Henderson }
210a35b3e14SRichard Henderson 
2113cfb0456SPeter Maydell static bool tcg_get_one_insn_per_tb(Object *obj, Error **errp)
2123cfb0456SPeter Maydell {
2133cfb0456SPeter Maydell     TCGState *s = TCG_STATE(obj);
2143cfb0456SPeter Maydell     return s->one_insn_per_tb;
2153cfb0456SPeter Maydell }
2163cfb0456SPeter Maydell 
2173cfb0456SPeter Maydell static void tcg_set_one_insn_per_tb(Object *obj, bool value, Error **errp)
2183cfb0456SPeter Maydell {
2193cfb0456SPeter Maydell     TCGState *s = TCG_STATE(obj);
2203cfb0456SPeter Maydell     s->one_insn_per_tb = value;
2210e33928cSPeter Maydell     /* Set the global also: this changes the behaviour */
2220e33928cSPeter Maydell     qatomic_set(&one_insn_per_tb, value);
2233cfb0456SPeter Maydell }
2243cfb0456SPeter Maydell 
2253b7a9388SAlex Bennée static int tcg_gdbstub_supported_sstep_flags(void)
2263b7a9388SAlex Bennée {
2273b7a9388SAlex Bennée     /*
2283b7a9388SAlex Bennée      * In replay mode all events will come from the log and can't be
2293b7a9388SAlex Bennée      * suppressed otherwise we would break determinism. However as those
2303b7a9388SAlex Bennée      * events are tied to the number of executed instructions we won't see
2313b7a9388SAlex Bennée      * them occurring every time we single step.
2323b7a9388SAlex Bennée      */
2333b7a9388SAlex Bennée     if (replay_mode != REPLAY_MODE_NONE) {
2343b7a9388SAlex Bennée         return SSTEP_ENABLE;
2353b7a9388SAlex Bennée     } else {
2363b7a9388SAlex Bennée         return SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
2373b7a9388SAlex Bennée     }
2383b7a9388SAlex Bennée }
2393b7a9388SAlex Bennée 
240a9ded601SYang Zhong static void tcg_accel_class_init(ObjectClass *oc, void *data)
241a9ded601SYang Zhong {
242a9ded601SYang Zhong     AccelClass *ac = ACCEL_CLASS(oc);
243a9ded601SYang Zhong     ac->name = "tcg";
2447109ef15SRichard Henderson     ac->init_machine = tcg_init_machine;
24523af78b0SPhilippe Mathieu-Daudé     ac->cpu_common_realize = tcg_exec_realizefn;
24623af78b0SPhilippe Mathieu-Daudé     ac->cpu_common_unrealize = tcg_exec_unrealizefn;
247a9ded601SYang Zhong     ac->allowed = &tcg_allowed;
2483b7a9388SAlex Bennée     ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
249a9ded601SYang Zhong 
25012ceaef6SPaolo Bonzini     object_class_property_add_str(oc, "thread",
25112ceaef6SPaolo Bonzini                                   tcg_get_thread,
252d2623129SMarkus Armbruster                                   tcg_set_thread);
253fe174132SPaolo Bonzini 
254fe174132SPaolo Bonzini     object_class_property_add(oc, "tb-size", "int",
255fe174132SPaolo Bonzini         tcg_get_tb_size, tcg_set_tb_size,
256d2623129SMarkus Armbruster         NULL, NULL);
257fe174132SPaolo Bonzini     object_class_property_set_description(oc, "tb-size",
2587eecec7dSMarkus Armbruster         "TCG translation block cache size");
259fe174132SPaolo Bonzini 
260a35b3e14SRichard Henderson     object_class_property_add_bool(oc, "split-wx",
261a35b3e14SRichard Henderson         tcg_get_splitwx, tcg_set_splitwx);
262a35b3e14SRichard Henderson     object_class_property_set_description(oc, "split-wx",
263a35b3e14SRichard Henderson         "Map jit pages into separate RW and RX regions");
2643cfb0456SPeter Maydell 
2653cfb0456SPeter Maydell     object_class_property_add_bool(oc, "one-insn-per-tb",
2663cfb0456SPeter Maydell                                    tcg_get_one_insn_per_tb,
2673cfb0456SPeter Maydell                                    tcg_set_one_insn_per_tb);
2683cfb0456SPeter Maydell     object_class_property_set_description(oc, "one-insn-per-tb",
2693cfb0456SPeter Maydell         "Only put one guest insn in each translation block");
27012ceaef6SPaolo Bonzini }
271a9ded601SYang Zhong 
272a9ded601SYang Zhong static const TypeInfo tcg_accel_type = {
273a9ded601SYang Zhong     .name = TYPE_TCG_ACCEL,
274a9ded601SYang Zhong     .parent = TYPE_ACCEL,
275af0440aeSPaolo Bonzini     .instance_init = tcg_accel_instance_init,
276a9ded601SYang Zhong     .class_init = tcg_accel_class_init,
27712ceaef6SPaolo Bonzini     .instance_size = sizeof(TCGState),
278a9ded601SYang Zhong };
2799e5d3b69SGerd Hoffmann module_obj(TYPE_TCG_ACCEL);
280a9ded601SYang Zhong 
281a9ded601SYang Zhong static void register_accel_types(void)
282a9ded601SYang Zhong {
283a9ded601SYang Zhong     type_register_static(&tcg_accel_type);
284a9ded601SYang Zhong }
285a9ded601SYang Zhong 
286a9ded601SYang Zhong type_init(register_accel_types);
287