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