xref: /openbmc/qemu/accel/tcg/tcg-all.c (revision d16cab541ab9217977e2a39abf3d79f914146741)
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"
2714a48c1dSMarkus Armbruster #include "sysemu/tcg.h"
285b5968c4SPhilippe Mathieu-Daudé #include "exec/replay-core.h"
29740b1759SClaudio Fontana #include "sysemu/cpu-timers.h"
30d7ec12f8SRichard Henderson #include "tcg/startup.h"
3170f168f8SRichard Henderson #include "tcg/oversized-guest.h"
32dd680bf3SPhilippe Mathieu-Daudé #include "qapi/error.h"
33dd680bf3SPhilippe Mathieu-Daudé #include "qemu/error-report.h"
34940e43aaSClaudio Fontana #include "qemu/accel.h"
350e33928cSPeter Maydell #include "qemu/atomic.h"
36fe174132SPaolo Bonzini #include "qapi/qapi-builtin-visit.h"
37efba8ae9SRichard Henderson #include "qemu/units.h"
3843b972b7SRichard Henderson #if !defined(CONFIG_USER_ONLY)
3943b972b7SRichard Henderson #include "hw/boards.h"
4043b972b7SRichard Henderson #endif
41*eeb6198eSPhilippe Mathieu-Daudé #include "internal-common.h"
42940e43aaSClaudio Fontana 
43db1015e9SEduardo Habkost struct TCGState {
4412ceaef6SPaolo Bonzini     AccelState parent_obj;
4512ceaef6SPaolo Bonzini 
4612ceaef6SPaolo Bonzini     bool mttcg_enabled;
473cfb0456SPeter Maydell     bool one_insn_per_tb;
48a35b3e14SRichard Henderson     int splitwx_enabled;
49fe174132SPaolo Bonzini     unsigned long tb_size;
50db1015e9SEduardo Habkost };
51db1015e9SEduardo Habkost typedef struct TCGState TCGState;
5212ceaef6SPaolo Bonzini 
5312ceaef6SPaolo Bonzini #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
5412ceaef6SPaolo Bonzini 
DECLARE_INSTANCE_CHECKER(TCGState,TCG_STATE,TYPE_TCG_ACCEL)558110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(TCGState, TCG_STATE,
568110fa1dSEduardo Habkost                          TYPE_TCG_ACCEL)
57a9ded601SYang Zhong 
58af0440aeSPaolo Bonzini /*
59af0440aeSPaolo Bonzini  * We default to false if we know other options have been enabled
60af0440aeSPaolo Bonzini  * which are currently incompatible with MTTCG. Otherwise when each
61af0440aeSPaolo Bonzini  * guest (target) has been updated to support:
62af0440aeSPaolo Bonzini  *   - atomic instructions
63af0440aeSPaolo Bonzini  *   - memory ordering primitives (barriers)
64af0440aeSPaolo Bonzini  * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
65af0440aeSPaolo Bonzini  *
66af0440aeSPaolo Bonzini  * Once a guest architecture has been converted to the new primitives
6797e15769SRichard Henderson  * there is one remaining limitation to check:
68af0440aeSPaolo Bonzini  *   - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
69af0440aeSPaolo Bonzini  */
70af0440aeSPaolo Bonzini 
71af0440aeSPaolo Bonzini static bool default_mttcg_enabled(void)
72af0440aeSPaolo Bonzini {
73740b1759SClaudio Fontana     if (icount_enabled() || TCG_OVERSIZED_GUEST) {
74af0440aeSPaolo Bonzini         return false;
7597e15769SRichard Henderson     }
76af0440aeSPaolo Bonzini #ifdef TARGET_SUPPORTS_MTTCG
7797e15769SRichard Henderson # ifndef TCG_GUEST_DEFAULT_MO
7897e15769SRichard Henderson #  error "TARGET_SUPPORTS_MTTCG without TCG_GUEST_DEFAULT_MO"
7997e15769SRichard Henderson # endif
8097e15769SRichard Henderson     return true;
81af0440aeSPaolo Bonzini #else
82af0440aeSPaolo Bonzini     return false;
83af0440aeSPaolo Bonzini #endif
84af0440aeSPaolo Bonzini }
85af0440aeSPaolo Bonzini 
tcg_accel_instance_init(Object * obj)86af0440aeSPaolo Bonzini static void tcg_accel_instance_init(Object *obj)
87af0440aeSPaolo Bonzini {
8812ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
8912ceaef6SPaolo Bonzini 
9012ceaef6SPaolo Bonzini     s->mttcg_enabled = default_mttcg_enabled();
91a35b3e14SRichard Henderson 
92a35b3e14SRichard Henderson     /* If debugging enabled, default "auto on", otherwise off. */
93940e43aaSClaudio Fontana #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY)
94a35b3e14SRichard Henderson     s->splitwx_enabled = -1;
95a35b3e14SRichard Henderson #else
96a35b3e14SRichard Henderson     s->splitwx_enabled = 0;
97a35b3e14SRichard Henderson #endif
98af0440aeSPaolo Bonzini }
99af0440aeSPaolo Bonzini 
100a77dabc3SClaudio Fontana bool mttcg_enabled;
1010e33928cSPeter Maydell bool one_insn_per_tb;
102a77dabc3SClaudio Fontana 
tcg_init_machine(MachineState * ms)1037109ef15SRichard Henderson static int tcg_init_machine(MachineState *ms)
104a9ded601SYang Zhong {
1054f7f5893SPhilippe Mathieu-Daudé     TCGState *s = TCG_STATE(current_accel());
10643b972b7SRichard Henderson #ifdef CONFIG_USER_ONLY
10743b972b7SRichard Henderson     unsigned max_cpus = 1;
10843b972b7SRichard Henderson #else
10943b972b7SRichard Henderson     unsigned max_cpus = ms->smp.max_cpus;
11043b972b7SRichard Henderson #endif
11112ceaef6SPaolo Bonzini 
112fa79cde6SRichard Henderson     tcg_allowed = true;
11312ceaef6SPaolo Bonzini     mttcg_enabled = s->mttcg_enabled;
114fa79cde6SRichard Henderson 
115fa79cde6SRichard Henderson     page_init();
116fa79cde6SRichard Henderson     tb_htable_init();
11743b972b7SRichard Henderson     tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_cpus);
118fa79cde6SRichard Henderson 
119fa79cde6SRichard Henderson #if defined(CONFIG_SOFTMMU)
120fa79cde6SRichard Henderson     /*
121fa79cde6SRichard Henderson      * There's no guest base to take into account, so go ahead and
122fa79cde6SRichard Henderson      * initialize the prologue now.
123fa79cde6SRichard Henderson      */
124935f75aeSRichard Henderson     tcg_prologue_init();
125fa79cde6SRichard Henderson #endif
126fa79cde6SRichard Henderson 
127a9ded601SYang Zhong     return 0;
128a9ded601SYang Zhong }
129a9ded601SYang Zhong 
tcg_get_thread(Object * obj,Error ** errp)13012ceaef6SPaolo Bonzini static char *tcg_get_thread(Object *obj, Error **errp)
131af0440aeSPaolo Bonzini {
13212ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
13312ceaef6SPaolo Bonzini 
13412ceaef6SPaolo Bonzini     return g_strdup(s->mttcg_enabled ? "multi" : "single");
135af0440aeSPaolo Bonzini }
13612ceaef6SPaolo Bonzini 
tcg_set_thread(Object * obj,const char * value,Error ** errp)13712ceaef6SPaolo Bonzini static void tcg_set_thread(Object *obj, const char *value, Error **errp)
13812ceaef6SPaolo Bonzini {
13912ceaef6SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
14012ceaef6SPaolo Bonzini 
14112ceaef6SPaolo Bonzini     if (strcmp(value, "multi") == 0) {
142af0440aeSPaolo Bonzini         if (TCG_OVERSIZED_GUEST) {
143af0440aeSPaolo Bonzini             error_setg(errp, "No MTTCG when guest word size > hosts");
144740b1759SClaudio Fontana         } else if (icount_enabled()) {
145af0440aeSPaolo Bonzini             error_setg(errp, "No MTTCG when icount is enabled");
146af0440aeSPaolo Bonzini         } else {
147af0440aeSPaolo Bonzini #ifndef TARGET_SUPPORTS_MTTCG
148af0440aeSPaolo Bonzini             warn_report("Guest not yet converted to MTTCG - "
149af0440aeSPaolo Bonzini                         "you may get unexpected results");
150af0440aeSPaolo Bonzini #endif
15112ceaef6SPaolo Bonzini             s->mttcg_enabled = true;
152af0440aeSPaolo Bonzini         }
15312ceaef6SPaolo Bonzini     } else if (strcmp(value, "single") == 0) {
15412ceaef6SPaolo Bonzini         s->mttcg_enabled = false;
155af0440aeSPaolo Bonzini     } else {
15612ceaef6SPaolo Bonzini         error_setg(errp, "Invalid 'thread' setting %s", value);
157af0440aeSPaolo Bonzini     }
158af0440aeSPaolo Bonzini }
159af0440aeSPaolo Bonzini 
tcg_get_tb_size(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)160fe174132SPaolo Bonzini static void tcg_get_tb_size(Object *obj, Visitor *v,
161fe174132SPaolo Bonzini                             const char *name, void *opaque,
162fe174132SPaolo Bonzini                             Error **errp)
163fe174132SPaolo Bonzini {
164fe174132SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
165fe174132SPaolo Bonzini     uint32_t value = s->tb_size;
166fe174132SPaolo Bonzini 
167fe174132SPaolo Bonzini     visit_type_uint32(v, name, &value, errp);
168fe174132SPaolo Bonzini }
169fe174132SPaolo Bonzini 
tcg_set_tb_size(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)170fe174132SPaolo Bonzini static void tcg_set_tb_size(Object *obj, Visitor *v,
171fe174132SPaolo Bonzini                             const char *name, void *opaque,
172fe174132SPaolo Bonzini                             Error **errp)
173fe174132SPaolo Bonzini {
174fe174132SPaolo Bonzini     TCGState *s = TCG_STATE(obj);
175fe174132SPaolo Bonzini     uint32_t value;
176fe174132SPaolo Bonzini 
177668f62ecSMarkus Armbruster     if (!visit_type_uint32(v, name, &value, errp)) {
178fe174132SPaolo Bonzini         return;
179fe174132SPaolo Bonzini     }
180fe174132SPaolo Bonzini 
181fe174132SPaolo Bonzini     s->tb_size = value;
182fe174132SPaolo Bonzini }
183fe174132SPaolo Bonzini 
tcg_get_splitwx(Object * obj,Error ** errp)184a35b3e14SRichard Henderson static bool tcg_get_splitwx(Object *obj, Error **errp)
185a35b3e14SRichard Henderson {
186a35b3e14SRichard Henderson     TCGState *s = TCG_STATE(obj);
187a35b3e14SRichard Henderson     return s->splitwx_enabled;
188a35b3e14SRichard Henderson }
189a35b3e14SRichard Henderson 
tcg_set_splitwx(Object * obj,bool value,Error ** errp)190a35b3e14SRichard Henderson static void tcg_set_splitwx(Object *obj, bool value, Error **errp)
191a35b3e14SRichard Henderson {
192a35b3e14SRichard Henderson     TCGState *s = TCG_STATE(obj);
193a35b3e14SRichard Henderson     s->splitwx_enabled = value;
194a35b3e14SRichard Henderson }
195a35b3e14SRichard Henderson 
tcg_get_one_insn_per_tb(Object * obj,Error ** errp)1963cfb0456SPeter Maydell static bool tcg_get_one_insn_per_tb(Object *obj, Error **errp)
1973cfb0456SPeter Maydell {
1983cfb0456SPeter Maydell     TCGState *s = TCG_STATE(obj);
1993cfb0456SPeter Maydell     return s->one_insn_per_tb;
2003cfb0456SPeter Maydell }
2013cfb0456SPeter Maydell 
tcg_set_one_insn_per_tb(Object * obj,bool value,Error ** errp)2023cfb0456SPeter Maydell static void tcg_set_one_insn_per_tb(Object *obj, bool value, Error **errp)
2033cfb0456SPeter Maydell {
2043cfb0456SPeter Maydell     TCGState *s = TCG_STATE(obj);
2053cfb0456SPeter Maydell     s->one_insn_per_tb = value;
2060e33928cSPeter Maydell     /* Set the global also: this changes the behaviour */
2070e33928cSPeter Maydell     qatomic_set(&one_insn_per_tb, value);
2083cfb0456SPeter Maydell }
2093cfb0456SPeter Maydell 
tcg_gdbstub_supported_sstep_flags(void)2103b7a9388SAlex Bennée static int tcg_gdbstub_supported_sstep_flags(void)
2113b7a9388SAlex Bennée {
2123b7a9388SAlex Bennée     /*
2133b7a9388SAlex Bennée      * In replay mode all events will come from the log and can't be
2143b7a9388SAlex Bennée      * suppressed otherwise we would break determinism. However as those
2153b7a9388SAlex Bennée      * events are tied to the number of executed instructions we won't see
2163b7a9388SAlex Bennée      * them occurring every time we single step.
2173b7a9388SAlex Bennée      */
2183b7a9388SAlex Bennée     if (replay_mode != REPLAY_MODE_NONE) {
2193b7a9388SAlex Bennée         return SSTEP_ENABLE;
2203b7a9388SAlex Bennée     } else {
2213b7a9388SAlex Bennée         return SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
2223b7a9388SAlex Bennée     }
2233b7a9388SAlex Bennée }
2243b7a9388SAlex Bennée 
tcg_accel_class_init(ObjectClass * oc,void * data)225a9ded601SYang Zhong static void tcg_accel_class_init(ObjectClass *oc, void *data)
226a9ded601SYang Zhong {
227a9ded601SYang Zhong     AccelClass *ac = ACCEL_CLASS(oc);
228a9ded601SYang Zhong     ac->name = "tcg";
2297109ef15SRichard Henderson     ac->init_machine = tcg_init_machine;
23023af78b0SPhilippe Mathieu-Daudé     ac->cpu_common_realize = tcg_exec_realizefn;
23123af78b0SPhilippe Mathieu-Daudé     ac->cpu_common_unrealize = tcg_exec_unrealizefn;
232a9ded601SYang Zhong     ac->allowed = &tcg_allowed;
2333b7a9388SAlex Bennée     ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
234a9ded601SYang Zhong 
23512ceaef6SPaolo Bonzini     object_class_property_add_str(oc, "thread",
23612ceaef6SPaolo Bonzini                                   tcg_get_thread,
237d2623129SMarkus Armbruster                                   tcg_set_thread);
238fe174132SPaolo Bonzini 
239fe174132SPaolo Bonzini     object_class_property_add(oc, "tb-size", "int",
240fe174132SPaolo Bonzini         tcg_get_tb_size, tcg_set_tb_size,
241d2623129SMarkus Armbruster         NULL, NULL);
242fe174132SPaolo Bonzini     object_class_property_set_description(oc, "tb-size",
2437eecec7dSMarkus Armbruster         "TCG translation block cache size");
244fe174132SPaolo Bonzini 
245a35b3e14SRichard Henderson     object_class_property_add_bool(oc, "split-wx",
246a35b3e14SRichard Henderson         tcg_get_splitwx, tcg_set_splitwx);
247a35b3e14SRichard Henderson     object_class_property_set_description(oc, "split-wx",
248a35b3e14SRichard Henderson         "Map jit pages into separate RW and RX regions");
2493cfb0456SPeter Maydell 
2503cfb0456SPeter Maydell     object_class_property_add_bool(oc, "one-insn-per-tb",
2513cfb0456SPeter Maydell                                    tcg_get_one_insn_per_tb,
2523cfb0456SPeter Maydell                                    tcg_set_one_insn_per_tb);
2533cfb0456SPeter Maydell     object_class_property_set_description(oc, "one-insn-per-tb",
2543cfb0456SPeter Maydell         "Only put one guest insn in each translation block");
25512ceaef6SPaolo Bonzini }
256a9ded601SYang Zhong 
257a9ded601SYang Zhong static const TypeInfo tcg_accel_type = {
258a9ded601SYang Zhong     .name = TYPE_TCG_ACCEL,
259a9ded601SYang Zhong     .parent = TYPE_ACCEL,
260af0440aeSPaolo Bonzini     .instance_init = tcg_accel_instance_init,
261a9ded601SYang Zhong     .class_init = tcg_accel_class_init,
26212ceaef6SPaolo Bonzini     .instance_size = sizeof(TCGState),
263a9ded601SYang Zhong };
2649e5d3b69SGerd Hoffmann module_obj(TYPE_TCG_ACCEL);
265a9ded601SYang Zhong 
register_accel_types(void)266a9ded601SYang Zhong static void register_accel_types(void)
267a9ded601SYang Zhong {
268a9ded601SYang Zhong     type_register_static(&tcg_accel_type);
269a9ded601SYang Zhong }
270a9ded601SYang Zhong 
271a9ded601SYang Zhong type_init(register_accel_types);
272