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" 4160b2c2e6SPhilippe 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 6260b2c2e6SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 6360b2c2e6SPhilippe Mathieu-Daudé bool qemu_tcg_mttcg_enabled(void) 6460b2c2e6SPhilippe Mathieu-Daudé { 65*61fc4c2bSRichard Henderson TCGState *s = TCG_STATE(current_accel()); 66*61fc4c2bSRichard Henderson return s->mttcg_enabled; 6760b2c2e6SPhilippe Mathieu-Daudé } 6860b2c2e6SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 6960b2c2e6SPhilippe Mathieu-Daudé 70af0440aeSPaolo Bonzini /* 71af0440aeSPaolo Bonzini * We default to false if we know other options have been enabled 72af0440aeSPaolo Bonzini * which are currently incompatible with MTTCG. Otherwise when each 73af0440aeSPaolo Bonzini * guest (target) has been updated to support: 74af0440aeSPaolo Bonzini * - atomic instructions 75af0440aeSPaolo Bonzini * - memory ordering primitives (barriers) 76af0440aeSPaolo Bonzini * they can set the appropriate CONFIG flags in ${target}-softmmu.mak 77af0440aeSPaolo Bonzini * 78af0440aeSPaolo Bonzini * Once a guest architecture has been converted to the new primitives 7997e15769SRichard Henderson * there is one remaining limitation to check: 80af0440aeSPaolo Bonzini * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host) 81af0440aeSPaolo Bonzini */ 82af0440aeSPaolo Bonzini 83af0440aeSPaolo Bonzini static bool default_mttcg_enabled(void) 84af0440aeSPaolo Bonzini { 85f441b4d1SRichard Henderson if (icount_enabled()) { 86af0440aeSPaolo Bonzini return false; 8797e15769SRichard Henderson } 88af0440aeSPaolo Bonzini #ifdef TARGET_SUPPORTS_MTTCG 8997e15769SRichard Henderson return true; 90af0440aeSPaolo Bonzini #else 91af0440aeSPaolo Bonzini return false; 92af0440aeSPaolo Bonzini #endif 93af0440aeSPaolo Bonzini } 94af0440aeSPaolo Bonzini 95af0440aeSPaolo Bonzini static void tcg_accel_instance_init(Object *obj) 96af0440aeSPaolo Bonzini { 9712ceaef6SPaolo Bonzini TCGState *s = TCG_STATE(obj); 9812ceaef6SPaolo Bonzini 9912ceaef6SPaolo Bonzini s->mttcg_enabled = default_mttcg_enabled(); 100a35b3e14SRichard Henderson 101a35b3e14SRichard Henderson /* If debugging enabled, default "auto on", otherwise off. */ 102940e43aaSClaudio Fontana #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY) 103a35b3e14SRichard Henderson s->splitwx_enabled = -1; 104a35b3e14SRichard Henderson #else 105a35b3e14SRichard Henderson s->splitwx_enabled = 0; 106a35b3e14SRichard Henderson #endif 107af0440aeSPaolo Bonzini } 108af0440aeSPaolo Bonzini 1090e33928cSPeter Maydell bool one_insn_per_tb; 110a77dabc3SClaudio Fontana 1117109ef15SRichard Henderson static int tcg_init_machine(MachineState *ms) 112a9ded601SYang Zhong { 1134f7f5893SPhilippe Mathieu-Daudé TCGState *s = TCG_STATE(current_accel()); 114a9d107faSRichard Henderson unsigned max_threads = 1; 11512ceaef6SPaolo Bonzini 116fa79cde6SRichard Henderson tcg_allowed = true; 117fa79cde6SRichard Henderson 118fa79cde6SRichard Henderson page_init(); 119fa79cde6SRichard Henderson tb_htable_init(); 120a9d107faSRichard Henderson 121a9d107faSRichard Henderson #ifndef CONFIG_USER_ONLY 12260b2c2e6SPhilippe Mathieu-Daudé if (s->mttcg_enabled) { 123a9d107faSRichard Henderson max_threads = ms->smp.max_cpus; 124a9d107faSRichard Henderson } 125a9d107faSRichard Henderson #endif 126a9d107faSRichard Henderson tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_threads); 127fa79cde6SRichard Henderson 128fa79cde6SRichard Henderson #if defined(CONFIG_SOFTMMU) 129fa79cde6SRichard Henderson /* 130fa79cde6SRichard Henderson * There's no guest base to take into account, so go ahead and 131fa79cde6SRichard Henderson * initialize the prologue now. 132fa79cde6SRichard Henderson */ 133935f75aeSRichard Henderson tcg_prologue_init(); 134fa79cde6SRichard Henderson #endif 135fa79cde6SRichard Henderson 136558ee1edSPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY 137558ee1edSPhilippe Mathieu-Daudé qdev_create_fake_machine(); 138558ee1edSPhilippe Mathieu-Daudé #endif 139558ee1edSPhilippe Mathieu-Daudé 140a9ded601SYang Zhong return 0; 141a9ded601SYang Zhong } 142a9ded601SYang Zhong 14312ceaef6SPaolo Bonzini static char *tcg_get_thread(Object *obj, Error **errp) 144af0440aeSPaolo Bonzini { 14512ceaef6SPaolo Bonzini TCGState *s = TCG_STATE(obj); 14612ceaef6SPaolo Bonzini 14712ceaef6SPaolo Bonzini return g_strdup(s->mttcg_enabled ? "multi" : "single"); 148af0440aeSPaolo Bonzini } 14912ceaef6SPaolo Bonzini 15012ceaef6SPaolo Bonzini static void tcg_set_thread(Object *obj, const char *value, Error **errp) 15112ceaef6SPaolo Bonzini { 15212ceaef6SPaolo Bonzini TCGState *s = TCG_STATE(obj); 15312ceaef6SPaolo Bonzini 15412ceaef6SPaolo Bonzini if (strcmp(value, "multi") == 0) { 155f441b4d1SRichard Henderson if (icount_enabled()) { 156af0440aeSPaolo Bonzini error_setg(errp, "No MTTCG when icount is enabled"); 157af0440aeSPaolo Bonzini } else { 158af0440aeSPaolo Bonzini #ifndef TARGET_SUPPORTS_MTTCG 159af0440aeSPaolo Bonzini warn_report("Guest not yet converted to MTTCG - " 160af0440aeSPaolo Bonzini "you may get unexpected results"); 161af0440aeSPaolo Bonzini #endif 16212ceaef6SPaolo Bonzini s->mttcg_enabled = true; 163af0440aeSPaolo Bonzini } 16412ceaef6SPaolo Bonzini } else if (strcmp(value, "single") == 0) { 16512ceaef6SPaolo Bonzini s->mttcg_enabled = false; 166af0440aeSPaolo Bonzini } else { 16712ceaef6SPaolo Bonzini error_setg(errp, "Invalid 'thread' setting %s", value); 168af0440aeSPaolo Bonzini } 169af0440aeSPaolo Bonzini } 170af0440aeSPaolo Bonzini 171fe174132SPaolo Bonzini static void tcg_get_tb_size(Object *obj, Visitor *v, 172fe174132SPaolo Bonzini const char *name, void *opaque, 173fe174132SPaolo Bonzini Error **errp) 174fe174132SPaolo Bonzini { 175fe174132SPaolo Bonzini TCGState *s = TCG_STATE(obj); 176fe174132SPaolo Bonzini uint32_t value = s->tb_size; 177fe174132SPaolo Bonzini 178fe174132SPaolo Bonzini visit_type_uint32(v, name, &value, errp); 179fe174132SPaolo Bonzini } 180fe174132SPaolo Bonzini 181fe174132SPaolo Bonzini static void tcg_set_tb_size(Object *obj, Visitor *v, 182fe174132SPaolo Bonzini const char *name, void *opaque, 183fe174132SPaolo Bonzini Error **errp) 184fe174132SPaolo Bonzini { 185fe174132SPaolo Bonzini TCGState *s = TCG_STATE(obj); 186fe174132SPaolo Bonzini uint32_t value; 187fe174132SPaolo Bonzini 188668f62ecSMarkus Armbruster if (!visit_type_uint32(v, name, &value, errp)) { 189fe174132SPaolo Bonzini return; 190fe174132SPaolo Bonzini } 191fe174132SPaolo Bonzini 192fe174132SPaolo Bonzini s->tb_size = value; 193fe174132SPaolo Bonzini } 194fe174132SPaolo Bonzini 195a35b3e14SRichard Henderson static bool tcg_get_splitwx(Object *obj, Error **errp) 196a35b3e14SRichard Henderson { 197a35b3e14SRichard Henderson TCGState *s = TCG_STATE(obj); 198a35b3e14SRichard Henderson return s->splitwx_enabled; 199a35b3e14SRichard Henderson } 200a35b3e14SRichard Henderson 201a35b3e14SRichard Henderson static void tcg_set_splitwx(Object *obj, bool value, Error **errp) 202a35b3e14SRichard Henderson { 203a35b3e14SRichard Henderson TCGState *s = TCG_STATE(obj); 204a35b3e14SRichard Henderson s->splitwx_enabled = value; 205a35b3e14SRichard Henderson } 206a35b3e14SRichard Henderson 2073cfb0456SPeter Maydell static bool tcg_get_one_insn_per_tb(Object *obj, Error **errp) 2083cfb0456SPeter Maydell { 2093cfb0456SPeter Maydell TCGState *s = TCG_STATE(obj); 2103cfb0456SPeter Maydell return s->one_insn_per_tb; 2113cfb0456SPeter Maydell } 2123cfb0456SPeter Maydell 2133cfb0456SPeter Maydell static void tcg_set_one_insn_per_tb(Object *obj, bool value, Error **errp) 2143cfb0456SPeter Maydell { 2153cfb0456SPeter Maydell TCGState *s = TCG_STATE(obj); 2163cfb0456SPeter Maydell s->one_insn_per_tb = value; 2170e33928cSPeter Maydell /* Set the global also: this changes the behaviour */ 2180e33928cSPeter Maydell qatomic_set(&one_insn_per_tb, value); 2193cfb0456SPeter Maydell } 2203cfb0456SPeter Maydell 2213b7a9388SAlex Bennée static int tcg_gdbstub_supported_sstep_flags(void) 2223b7a9388SAlex Bennée { 2233b7a9388SAlex Bennée /* 2243b7a9388SAlex Bennée * In replay mode all events will come from the log and can't be 2253b7a9388SAlex Bennée * suppressed otherwise we would break determinism. However as those 2263b7a9388SAlex Bennée * events are tied to the number of executed instructions we won't see 2273b7a9388SAlex Bennée * them occurring every time we single step. 2283b7a9388SAlex Bennée */ 2293b7a9388SAlex Bennée if (replay_mode != REPLAY_MODE_NONE) { 2303b7a9388SAlex Bennée return SSTEP_ENABLE; 2313b7a9388SAlex Bennée } else { 2323b7a9388SAlex Bennée return SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; 2333b7a9388SAlex Bennée } 2343b7a9388SAlex Bennée } 2353b7a9388SAlex Bennée 236a9ded601SYang Zhong static void tcg_accel_class_init(ObjectClass *oc, void *data) 237a9ded601SYang Zhong { 238a9ded601SYang Zhong AccelClass *ac = ACCEL_CLASS(oc); 239a9ded601SYang Zhong ac->name = "tcg"; 2407109ef15SRichard Henderson ac->init_machine = tcg_init_machine; 24123af78b0SPhilippe Mathieu-Daudé ac->cpu_common_realize = tcg_exec_realizefn; 24223af78b0SPhilippe Mathieu-Daudé ac->cpu_common_unrealize = tcg_exec_unrealizefn; 243a9ded601SYang Zhong ac->allowed = &tcg_allowed; 2443b7a9388SAlex Bennée ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags; 245a9ded601SYang Zhong 24612ceaef6SPaolo Bonzini object_class_property_add_str(oc, "thread", 24712ceaef6SPaolo Bonzini tcg_get_thread, 248d2623129SMarkus Armbruster tcg_set_thread); 249fe174132SPaolo Bonzini 250fe174132SPaolo Bonzini object_class_property_add(oc, "tb-size", "int", 251fe174132SPaolo Bonzini tcg_get_tb_size, tcg_set_tb_size, 252d2623129SMarkus Armbruster NULL, NULL); 253fe174132SPaolo Bonzini object_class_property_set_description(oc, "tb-size", 2547eecec7dSMarkus Armbruster "TCG translation block cache size"); 255fe174132SPaolo Bonzini 256a35b3e14SRichard Henderson object_class_property_add_bool(oc, "split-wx", 257a35b3e14SRichard Henderson tcg_get_splitwx, tcg_set_splitwx); 258a35b3e14SRichard Henderson object_class_property_set_description(oc, "split-wx", 259a35b3e14SRichard Henderson "Map jit pages into separate RW and RX regions"); 2603cfb0456SPeter Maydell 2613cfb0456SPeter Maydell object_class_property_add_bool(oc, "one-insn-per-tb", 2623cfb0456SPeter Maydell tcg_get_one_insn_per_tb, 2633cfb0456SPeter Maydell tcg_set_one_insn_per_tb); 2643cfb0456SPeter Maydell object_class_property_set_description(oc, "one-insn-per-tb", 2653cfb0456SPeter Maydell "Only put one guest insn in each translation block"); 26612ceaef6SPaolo Bonzini } 267a9ded601SYang Zhong 268a9ded601SYang Zhong static const TypeInfo tcg_accel_type = { 269a9ded601SYang Zhong .name = TYPE_TCG_ACCEL, 270a9ded601SYang Zhong .parent = TYPE_ACCEL, 271af0440aeSPaolo Bonzini .instance_init = tcg_accel_instance_init, 272a9ded601SYang Zhong .class_init = tcg_accel_class_init, 27312ceaef6SPaolo Bonzini .instance_size = sizeof(TCGState), 274a9ded601SYang Zhong }; 2759e5d3b69SGerd Hoffmann module_obj(TYPE_TCG_ACCEL); 276a9ded601SYang Zhong 277a9ded601SYang Zhong static void register_accel_types(void) 278a9ded601SYang Zhong { 279a9ded601SYang Zhong type_register_static(&tcg_accel_type); 280a9ded601SYang Zhong } 281a9ded601SYang Zhong 282a9ded601SYang Zhong type_init(register_accel_types); 283