xref: /qemu/accel/tcg/tcg-all.c (revision 161f5bc8e965fa8255db435683e6b52042037bb7)
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"
29*161f5bc8SRichard 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"
4143b972b7SRichard Henderson #endif
42eeb6198eSPhilippe Mathieu-Daudé #include "internal-common.h"
43f441b4d1SRichard Henderson #include "cpu-param.h"
44f441b4d1SRichard Henderson 
45940e43aaSClaudio Fontana 
46db1015e9SEduardo Habkost struct TCGState {
4712ceaef6SPaolo Bonzini     AccelState parent_obj;
4812ceaef6SPaolo Bonzini 
4912ceaef6SPaolo Bonzini     bool mttcg_enabled;
503cfb0456SPeter Maydell     bool one_insn_per_tb;
51a35b3e14SRichard Henderson     int splitwx_enabled;
52fe174132SPaolo Bonzini     unsigned long tb_size;
53db1015e9SEduardo Habkost };
54db1015e9SEduardo Habkost typedef struct TCGState TCGState;
5512ceaef6SPaolo Bonzini 
5612ceaef6SPaolo Bonzini #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
5712ceaef6SPaolo Bonzini 
588110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(TCGState, TCG_STATE,
598110fa1dSEduardo Habkost                          TYPE_TCG_ACCEL)
60a9ded601SYang Zhong 
61af0440aeSPaolo Bonzini /*
62af0440aeSPaolo Bonzini  * We default to false if we know other options have been enabled
63af0440aeSPaolo Bonzini  * which are currently incompatible with MTTCG. Otherwise when each
64af0440aeSPaolo Bonzini  * guest (target) has been updated to support:
65af0440aeSPaolo Bonzini  *   - atomic instructions
66af0440aeSPaolo Bonzini  *   - memory ordering primitives (barriers)
67af0440aeSPaolo Bonzini  * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
68af0440aeSPaolo Bonzini  *
69af0440aeSPaolo Bonzini  * Once a guest architecture has been converted to the new primitives
7097e15769SRichard Henderson  * there is one remaining limitation to check:
71af0440aeSPaolo Bonzini  *   - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
72af0440aeSPaolo Bonzini  */
73af0440aeSPaolo Bonzini 
74af0440aeSPaolo Bonzini static bool default_mttcg_enabled(void)
75af0440aeSPaolo Bonzini {
76f441b4d1SRichard Henderson     if (icount_enabled()) {
77af0440aeSPaolo Bonzini         return false;
7897e15769SRichard Henderson     }
79af0440aeSPaolo Bonzini #ifdef TARGET_SUPPORTS_MTTCG
8097e15769SRichard Henderson # ifndef TCG_GUEST_DEFAULT_MO
8197e15769SRichard Henderson #  error "TARGET_SUPPORTS_MTTCG without TCG_GUEST_DEFAULT_MO"
8297e15769SRichard Henderson # endif
8397e15769SRichard Henderson     return true;
84af0440aeSPaolo Bonzini #else
85af0440aeSPaolo Bonzini     return false;
86af0440aeSPaolo Bonzini #endif
87af0440aeSPaolo Bonzini }
88af0440aeSPaolo Bonzini 
89af0440aeSPaolo Bonzini static void tcg_accel_instance_init(Object *obj)
90af0440aeSPaolo Bonzini {
9112ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
9212ceaef6SPaolo Bonzini 
9312ceaef6SPaolo Bonzini     s->mttcg_enabled = default_mttcg_enabled();
94a35b3e14SRichard Henderson 
95a35b3e14SRichard Henderson     /* If debugging enabled, default "auto on", otherwise off. */
96940e43aaSClaudio Fontana #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY)
97a35b3e14SRichard Henderson     s->splitwx_enabled = -1;
98a35b3e14SRichard Henderson #else
99a35b3e14SRichard Henderson     s->splitwx_enabled = 0;
100a35b3e14SRichard Henderson #endif
101af0440aeSPaolo Bonzini }
102af0440aeSPaolo Bonzini 
103a77dabc3SClaudio Fontana bool mttcg_enabled;
1040e33928cSPeter Maydell bool one_insn_per_tb;
105a77dabc3SClaudio Fontana 
1067109ef15SRichard Henderson static int tcg_init_machine(MachineState *ms)
107a9ded601SYang Zhong {
1084f7f5893SPhilippe Mathieu-Daudé     TCGState *s = TCG_STATE(current_accel());
10943b972b7SRichard Henderson #ifdef CONFIG_USER_ONLY
11043b972b7SRichard Henderson     unsigned max_cpus = 1;
11143b972b7SRichard Henderson #else
11243b972b7SRichard Henderson     unsigned max_cpus = ms->smp.max_cpus;
11343b972b7SRichard Henderson #endif
11412ceaef6SPaolo Bonzini 
115fa79cde6SRichard Henderson     tcg_allowed = true;
11612ceaef6SPaolo Bonzini     mttcg_enabled = s->mttcg_enabled;
117fa79cde6SRichard Henderson 
118fa79cde6SRichard Henderson     page_init();
119fa79cde6SRichard Henderson     tb_htable_init();
12043b972b7SRichard Henderson     tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_cpus);
121fa79cde6SRichard Henderson 
122fa79cde6SRichard Henderson #if defined(CONFIG_SOFTMMU)
123fa79cde6SRichard Henderson     /*
124fa79cde6SRichard Henderson      * There's no guest base to take into account, so go ahead and
125fa79cde6SRichard Henderson      * initialize the prologue now.
126fa79cde6SRichard Henderson      */
127935f75aeSRichard Henderson     tcg_prologue_init();
128fa79cde6SRichard Henderson #endif
129fa79cde6SRichard Henderson 
130558ee1edSPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY
131558ee1edSPhilippe Mathieu-Daudé     qdev_create_fake_machine();
132558ee1edSPhilippe Mathieu-Daudé #endif
133558ee1edSPhilippe Mathieu-Daudé 
134a9ded601SYang Zhong     return 0;
135a9ded601SYang Zhong }
136a9ded601SYang Zhong 
13712ceaef6SPaolo Bonzini static char *tcg_get_thread(Object *obj, Error **errp)
138af0440aeSPaolo Bonzini {
13912ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
14012ceaef6SPaolo Bonzini 
14112ceaef6SPaolo Bonzini     return g_strdup(s->mttcg_enabled ? "multi" : "single");
142af0440aeSPaolo Bonzini }
14312ceaef6SPaolo Bonzini 
14412ceaef6SPaolo Bonzini static void tcg_set_thread(Object *obj, const char *value, Error **errp)
14512ceaef6SPaolo Bonzini {
14612ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
14712ceaef6SPaolo Bonzini 
14812ceaef6SPaolo Bonzini     if (strcmp(value, "multi") == 0) {
149f441b4d1SRichard Henderson         if (icount_enabled()) {
150af0440aeSPaolo Bonzini             error_setg(errp, "No MTTCG when icount is enabled");
151af0440aeSPaolo Bonzini         } else {
152af0440aeSPaolo Bonzini #ifndef TARGET_SUPPORTS_MTTCG
153af0440aeSPaolo Bonzini             warn_report("Guest not yet converted to MTTCG - "
154af0440aeSPaolo Bonzini                         "you may get unexpected results");
155af0440aeSPaolo Bonzini #endif
15612ceaef6SPaolo Bonzini             s->mttcg_enabled = true;
157af0440aeSPaolo Bonzini         }
15812ceaef6SPaolo Bonzini     } else if (strcmp(value, "single") == 0) {
15912ceaef6SPaolo Bonzini         s->mttcg_enabled = false;
160af0440aeSPaolo Bonzini     } else {
16112ceaef6SPaolo Bonzini         error_setg(errp, "Invalid 'thread' setting %s", value);
162af0440aeSPaolo Bonzini     }
163af0440aeSPaolo Bonzini }
164af0440aeSPaolo Bonzini 
165fe174132SPaolo Bonzini static void tcg_get_tb_size(Object *obj, Visitor *v,
166fe174132SPaolo Bonzini                             const char *name, void *opaque,
167fe174132SPaolo Bonzini                             Error **errp)
168fe174132SPaolo Bonzini {
169fe174132SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
170fe174132SPaolo Bonzini     uint32_t value = s->tb_size;
171fe174132SPaolo Bonzini 
172fe174132SPaolo Bonzini     visit_type_uint32(v, name, &value, errp);
173fe174132SPaolo Bonzini }
174fe174132SPaolo Bonzini 
175fe174132SPaolo Bonzini static void tcg_set_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;
181fe174132SPaolo Bonzini 
182668f62ecSMarkus Armbruster     if (!visit_type_uint32(v, name, &value, errp)) {
183fe174132SPaolo Bonzini         return;
184fe174132SPaolo Bonzini     }
185fe174132SPaolo Bonzini 
186fe174132SPaolo Bonzini     s->tb_size = value;
187fe174132SPaolo Bonzini }
188fe174132SPaolo Bonzini 
189a35b3e14SRichard Henderson static bool tcg_get_splitwx(Object *obj, Error **errp)
190a35b3e14SRichard Henderson {
191a35b3e14SRichard Henderson     TCGState *s = TCG_STATE(obj);
192a35b3e14SRichard Henderson     return s->splitwx_enabled;
193a35b3e14SRichard Henderson }
194a35b3e14SRichard Henderson 
195a35b3e14SRichard Henderson static void tcg_set_splitwx(Object *obj, bool value, Error **errp)
196a35b3e14SRichard Henderson {
197a35b3e14SRichard Henderson     TCGState *s = TCG_STATE(obj);
198a35b3e14SRichard Henderson     s->splitwx_enabled = value;
199a35b3e14SRichard Henderson }
200a35b3e14SRichard Henderson 
2013cfb0456SPeter Maydell static bool tcg_get_one_insn_per_tb(Object *obj, Error **errp)
2023cfb0456SPeter Maydell {
2033cfb0456SPeter Maydell     TCGState *s = TCG_STATE(obj);
2043cfb0456SPeter Maydell     return s->one_insn_per_tb;
2053cfb0456SPeter Maydell }
2063cfb0456SPeter Maydell 
2073cfb0456SPeter Maydell static void tcg_set_one_insn_per_tb(Object *obj, bool value, Error **errp)
2083cfb0456SPeter Maydell {
2093cfb0456SPeter Maydell     TCGState *s = TCG_STATE(obj);
2103cfb0456SPeter Maydell     s->one_insn_per_tb = value;
2110e33928cSPeter Maydell     /* Set the global also: this changes the behaviour */
2120e33928cSPeter Maydell     qatomic_set(&one_insn_per_tb, value);
2133cfb0456SPeter Maydell }
2143cfb0456SPeter Maydell 
2153b7a9388SAlex Bennée static int tcg_gdbstub_supported_sstep_flags(void)
2163b7a9388SAlex Bennée {
2173b7a9388SAlex Bennée     /*
2183b7a9388SAlex Bennée      * In replay mode all events will come from the log and can't be
2193b7a9388SAlex Bennée      * suppressed otherwise we would break determinism. However as those
2203b7a9388SAlex Bennée      * events are tied to the number of executed instructions we won't see
2213b7a9388SAlex Bennée      * them occurring every time we single step.
2223b7a9388SAlex Bennée      */
2233b7a9388SAlex Bennée     if (replay_mode != REPLAY_MODE_NONE) {
2243b7a9388SAlex Bennée         return SSTEP_ENABLE;
2253b7a9388SAlex Bennée     } else {
2263b7a9388SAlex Bennée         return SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
2273b7a9388SAlex Bennée     }
2283b7a9388SAlex Bennée }
2293b7a9388SAlex Bennée 
230a9ded601SYang Zhong static void tcg_accel_class_init(ObjectClass *oc, void *data)
231a9ded601SYang Zhong {
232a9ded601SYang Zhong     AccelClass *ac = ACCEL_CLASS(oc);
233a9ded601SYang Zhong     ac->name = "tcg";
2347109ef15SRichard Henderson     ac->init_machine = tcg_init_machine;
23523af78b0SPhilippe Mathieu-Daudé     ac->cpu_common_realize = tcg_exec_realizefn;
23623af78b0SPhilippe Mathieu-Daudé     ac->cpu_common_unrealize = tcg_exec_unrealizefn;
237a9ded601SYang Zhong     ac->allowed = &tcg_allowed;
2383b7a9388SAlex Bennée     ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
239a9ded601SYang Zhong 
24012ceaef6SPaolo Bonzini     object_class_property_add_str(oc, "thread",
24112ceaef6SPaolo Bonzini                                   tcg_get_thread,
242d2623129SMarkus Armbruster                                   tcg_set_thread);
243fe174132SPaolo Bonzini 
244fe174132SPaolo Bonzini     object_class_property_add(oc, "tb-size", "int",
245fe174132SPaolo Bonzini         tcg_get_tb_size, tcg_set_tb_size,
246d2623129SMarkus Armbruster         NULL, NULL);
247fe174132SPaolo Bonzini     object_class_property_set_description(oc, "tb-size",
2487eecec7dSMarkus Armbruster         "TCG translation block cache size");
249fe174132SPaolo Bonzini 
250a35b3e14SRichard Henderson     object_class_property_add_bool(oc, "split-wx",
251a35b3e14SRichard Henderson         tcg_get_splitwx, tcg_set_splitwx);
252a35b3e14SRichard Henderson     object_class_property_set_description(oc, "split-wx",
253a35b3e14SRichard Henderson         "Map jit pages into separate RW and RX regions");
2543cfb0456SPeter Maydell 
2553cfb0456SPeter Maydell     object_class_property_add_bool(oc, "one-insn-per-tb",
2563cfb0456SPeter Maydell                                    tcg_get_one_insn_per_tb,
2573cfb0456SPeter Maydell                                    tcg_set_one_insn_per_tb);
2583cfb0456SPeter Maydell     object_class_property_set_description(oc, "one-insn-per-tb",
2593cfb0456SPeter Maydell         "Only put one guest insn in each translation block");
26012ceaef6SPaolo Bonzini }
261a9ded601SYang Zhong 
262a9ded601SYang Zhong static const TypeInfo tcg_accel_type = {
263a9ded601SYang Zhong     .name = TYPE_TCG_ACCEL,
264a9ded601SYang Zhong     .parent = TYPE_ACCEL,
265af0440aeSPaolo Bonzini     .instance_init = tcg_accel_instance_init,
266a9ded601SYang Zhong     .class_init = tcg_accel_class_init,
26712ceaef6SPaolo Bonzini     .instance_size = sizeof(TCGState),
268a9ded601SYang Zhong };
2699e5d3b69SGerd Hoffmann module_obj(TYPE_TCG_ACCEL);
270a9ded601SYang Zhong 
271a9ded601SYang Zhong static void register_accel_types(void)
272a9ded601SYang Zhong {
273a9ded601SYang Zhong     type_register_static(&tcg_accel_type);
274a9ded601SYang Zhong }
275a9ded601SYang Zhong 
276a9ded601SYang Zhong type_init(register_accel_types);
277