1 /* 2 * QEMU System Emulator, accelerator interfaces 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * Copyright (c) 2014 Red Hat Inc. 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 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 23 * THE SOFTWARE. 24 */ 25 26 #include "qemu/osdep.h" 27 #include "sysemu/tcg.h" 28 #include "exec/replay-core.h" 29 #include "sysemu/cpu-timers.h" 30 #include "tcg/startup.h" 31 #include "tcg/oversized-guest.h" 32 #include "qapi/error.h" 33 #include "qemu/error-report.h" 34 #include "qemu/accel.h" 35 #include "qemu/atomic.h" 36 #include "qapi/qapi-builtin-visit.h" 37 #include "qemu/units.h" 38 #if !defined(CONFIG_USER_ONLY) 39 #include "hw/boards.h" 40 #endif 41 #include "internal-common.h" 42 43 struct TCGState { 44 AccelState parent_obj; 45 46 bool mttcg_enabled; 47 bool one_insn_per_tb; 48 int splitwx_enabled; 49 unsigned long tb_size; 50 }; 51 typedef struct TCGState TCGState; 52 53 #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg") 54 55 DECLARE_INSTANCE_CHECKER(TCGState, TCG_STATE, 56 TYPE_TCG_ACCEL) 57 58 /* 59 * We default to false if we know other options have been enabled 60 * which are currently incompatible with MTTCG. Otherwise when each 61 * guest (target) has been updated to support: 62 * - atomic instructions 63 * - memory ordering primitives (barriers) 64 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak 65 * 66 * Once a guest architecture has been converted to the new primitives 67 * there is one remaining limitation to check: 68 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host) 69 */ 70 71 static bool default_mttcg_enabled(void) 72 { 73 if (icount_enabled() || TCG_OVERSIZED_GUEST) { 74 return false; 75 } 76 #ifdef TARGET_SUPPORTS_MTTCG 77 # ifndef TCG_GUEST_DEFAULT_MO 78 # error "TARGET_SUPPORTS_MTTCG without TCG_GUEST_DEFAULT_MO" 79 # endif 80 return true; 81 #else 82 return false; 83 #endif 84 } 85 86 static void tcg_accel_instance_init(Object *obj) 87 { 88 TCGState *s = TCG_STATE(obj); 89 90 s->mttcg_enabled = default_mttcg_enabled(); 91 92 /* If debugging enabled, default "auto on", otherwise off. */ 93 #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY) 94 s->splitwx_enabled = -1; 95 #else 96 s->splitwx_enabled = 0; 97 #endif 98 } 99 100 bool mttcg_enabled; 101 bool one_insn_per_tb; 102 103 static int tcg_init_machine(MachineState *ms) 104 { 105 TCGState *s = TCG_STATE(current_accel()); 106 #ifdef CONFIG_USER_ONLY 107 unsigned max_cpus = 1; 108 #else 109 unsigned max_cpus = ms->smp.max_cpus; 110 #endif 111 112 tcg_allowed = true; 113 mttcg_enabled = s->mttcg_enabled; 114 115 page_init(); 116 tb_htable_init(); 117 tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_cpus); 118 119 #if defined(CONFIG_SOFTMMU) 120 /* 121 * There's no guest base to take into account, so go ahead and 122 * initialize the prologue now. 123 */ 124 tcg_prologue_init(); 125 #endif 126 127 return 0; 128 } 129 130 static char *tcg_get_thread(Object *obj, Error **errp) 131 { 132 TCGState *s = TCG_STATE(obj); 133 134 return g_strdup(s->mttcg_enabled ? "multi" : "single"); 135 } 136 137 static void tcg_set_thread(Object *obj, const char *value, Error **errp) 138 { 139 TCGState *s = TCG_STATE(obj); 140 141 if (strcmp(value, "multi") == 0) { 142 if (TCG_OVERSIZED_GUEST) { 143 error_setg(errp, "No MTTCG when guest word size > hosts"); 144 } else if (icount_enabled()) { 145 error_setg(errp, "No MTTCG when icount is enabled"); 146 } else { 147 #ifndef TARGET_SUPPORTS_MTTCG 148 warn_report("Guest not yet converted to MTTCG - " 149 "you may get unexpected results"); 150 #endif 151 s->mttcg_enabled = true; 152 } 153 } else if (strcmp(value, "single") == 0) { 154 s->mttcg_enabled = false; 155 } else { 156 error_setg(errp, "Invalid 'thread' setting %s", value); 157 } 158 } 159 160 static void tcg_get_tb_size(Object *obj, Visitor *v, 161 const char *name, void *opaque, 162 Error **errp) 163 { 164 TCGState *s = TCG_STATE(obj); 165 uint32_t value = s->tb_size; 166 167 visit_type_uint32(v, name, &value, errp); 168 } 169 170 static void tcg_set_tb_size(Object *obj, Visitor *v, 171 const char *name, void *opaque, 172 Error **errp) 173 { 174 TCGState *s = TCG_STATE(obj); 175 uint32_t value; 176 177 if (!visit_type_uint32(v, name, &value, errp)) { 178 return; 179 } 180 181 s->tb_size = value; 182 } 183 184 static bool tcg_get_splitwx(Object *obj, Error **errp) 185 { 186 TCGState *s = TCG_STATE(obj); 187 return s->splitwx_enabled; 188 } 189 190 static void tcg_set_splitwx(Object *obj, bool value, Error **errp) 191 { 192 TCGState *s = TCG_STATE(obj); 193 s->splitwx_enabled = value; 194 } 195 196 static bool tcg_get_one_insn_per_tb(Object *obj, Error **errp) 197 { 198 TCGState *s = TCG_STATE(obj); 199 return s->one_insn_per_tb; 200 } 201 202 static void tcg_set_one_insn_per_tb(Object *obj, bool value, Error **errp) 203 { 204 TCGState *s = TCG_STATE(obj); 205 s->one_insn_per_tb = value; 206 /* Set the global also: this changes the behaviour */ 207 qatomic_set(&one_insn_per_tb, value); 208 } 209 210 static int tcg_gdbstub_supported_sstep_flags(void) 211 { 212 /* 213 * In replay mode all events will come from the log and can't be 214 * suppressed otherwise we would break determinism. However as those 215 * events are tied to the number of executed instructions we won't see 216 * them occurring every time we single step. 217 */ 218 if (replay_mode != REPLAY_MODE_NONE) { 219 return SSTEP_ENABLE; 220 } else { 221 return SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; 222 } 223 } 224 225 static void tcg_accel_class_init(ObjectClass *oc, void *data) 226 { 227 AccelClass *ac = ACCEL_CLASS(oc); 228 ac->name = "tcg"; 229 ac->init_machine = tcg_init_machine; 230 ac->cpu_common_realize = tcg_exec_realizefn; 231 ac->cpu_common_unrealize = tcg_exec_unrealizefn; 232 ac->allowed = &tcg_allowed; 233 ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags; 234 235 object_class_property_add_str(oc, "thread", 236 tcg_get_thread, 237 tcg_set_thread); 238 239 object_class_property_add(oc, "tb-size", "int", 240 tcg_get_tb_size, tcg_set_tb_size, 241 NULL, NULL); 242 object_class_property_set_description(oc, "tb-size", 243 "TCG translation block cache size"); 244 245 object_class_property_add_bool(oc, "split-wx", 246 tcg_get_splitwx, tcg_set_splitwx); 247 object_class_property_set_description(oc, "split-wx", 248 "Map jit pages into separate RW and RX regions"); 249 250 object_class_property_add_bool(oc, "one-insn-per-tb", 251 tcg_get_one_insn_per_tb, 252 tcg_set_one_insn_per_tb); 253 object_class_property_set_description(oc, "one-insn-per-tb", 254 "Only put one guest insn in each translation block"); 255 } 256 257 static const TypeInfo tcg_accel_type = { 258 .name = TYPE_TCG_ACCEL, 259 .parent = TYPE_ACCEL, 260 .instance_init = tcg_accel_instance_init, 261 .class_init = tcg_accel_class_init, 262 .instance_size = sizeof(TCGState), 263 }; 264 module_obj(TYPE_TCG_ACCEL); 265 266 static void register_accel_types(void) 267 { 268 type_register_static(&tcg_accel_type); 269 } 270 271 type_init(register_accel_types); 272