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 "qemu-common.h" 28 #include "sysemu/tcg.h" 29 #include "sysemu/cpu-timers.h" 30 #include "tcg/tcg.h" 31 #include "qapi/error.h" 32 #include "qemu/error-report.h" 33 #include "qemu/accel.h" 34 #include "qapi/qapi-builtin-visit.h" 35 36 #ifndef CONFIG_USER_ONLY 37 #include "tcg-cpus.h" 38 #endif /* CONFIG_USER_ONLY */ 39 40 struct TCGState { 41 AccelState parent_obj; 42 43 bool mttcg_enabled; 44 int splitwx_enabled; 45 unsigned long tb_size; 46 }; 47 typedef struct TCGState TCGState; 48 49 #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg") 50 51 DECLARE_INSTANCE_CHECKER(TCGState, TCG_STATE, 52 TYPE_TCG_ACCEL) 53 54 /* 55 * We default to false if we know other options have been enabled 56 * which are currently incompatible with MTTCG. Otherwise when each 57 * guest (target) has been updated to support: 58 * - atomic instructions 59 * - memory ordering primitives (barriers) 60 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak 61 * 62 * Once a guest architecture has been converted to the new primitives 63 * there are two remaining limitations to check. 64 * 65 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host) 66 * - The host must have a stronger memory order than the guest 67 * 68 * It may be possible in future to support strong guests on weak hosts 69 * but that will require tagging all load/stores in a guest with their 70 * implicit memory order requirements which would likely slow things 71 * down a lot. 72 */ 73 74 static bool check_tcg_memory_orders_compatible(void) 75 { 76 #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO) 77 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0; 78 #else 79 return false; 80 #endif 81 } 82 83 static bool default_mttcg_enabled(void) 84 { 85 if (icount_enabled() || TCG_OVERSIZED_GUEST) { 86 return false; 87 } else { 88 #ifdef TARGET_SUPPORTS_MTTCG 89 return check_tcg_memory_orders_compatible(); 90 #else 91 return false; 92 #endif 93 } 94 } 95 96 static void tcg_accel_instance_init(Object *obj) 97 { 98 TCGState *s = TCG_STATE(obj); 99 100 s->mttcg_enabled = default_mttcg_enabled(); 101 102 /* If debugging enabled, default "auto on", otherwise off. */ 103 #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY) 104 s->splitwx_enabled = -1; 105 #else 106 s->splitwx_enabled = 0; 107 #endif 108 } 109 110 bool mttcg_enabled; 111 112 static int tcg_init(MachineState *ms) 113 { 114 TCGState *s = TCG_STATE(current_accel()); 115 116 tcg_exec_init(s->tb_size * 1024 * 1024, s->splitwx_enabled); 117 mttcg_enabled = s->mttcg_enabled; 118 119 /* 120 * Initialize TCG regions only for softmmu. 121 * 122 * This needs to be done later for user mode, because the prologue 123 * generation needs to be delayed so that GUEST_BASE is already set. 124 */ 125 #ifndef CONFIG_USER_ONLY 126 tcg_region_init(); 127 128 if (mttcg_enabled) { 129 cpus_register_accel(&tcg_cpus_mttcg); 130 } else if (icount_enabled()) { 131 cpus_register_accel(&tcg_cpus_icount); 132 } else { 133 cpus_register_accel(&tcg_cpus_rr); 134 } 135 #endif /* !CONFIG_USER_ONLY */ 136 137 return 0; 138 } 139 140 static char *tcg_get_thread(Object *obj, Error **errp) 141 { 142 TCGState *s = TCG_STATE(obj); 143 144 return g_strdup(s->mttcg_enabled ? "multi" : "single"); 145 } 146 147 static void tcg_set_thread(Object *obj, const char *value, Error **errp) 148 { 149 TCGState *s = TCG_STATE(obj); 150 151 if (strcmp(value, "multi") == 0) { 152 if (TCG_OVERSIZED_GUEST) { 153 error_setg(errp, "No MTTCG when guest word size > hosts"); 154 } else if (icount_enabled()) { 155 error_setg(errp, "No MTTCG when icount is enabled"); 156 } else { 157 #ifndef TARGET_SUPPORTS_MTTCG 158 warn_report("Guest not yet converted to MTTCG - " 159 "you may get unexpected results"); 160 #endif 161 if (!check_tcg_memory_orders_compatible()) { 162 warn_report("Guest expects a stronger memory ordering " 163 "than the host provides"); 164 error_printf("This may cause strange/hard to debug errors\n"); 165 } 166 s->mttcg_enabled = true; 167 } 168 } else if (strcmp(value, "single") == 0) { 169 s->mttcg_enabled = false; 170 } else { 171 error_setg(errp, "Invalid 'thread' setting %s", value); 172 } 173 } 174 175 static void tcg_get_tb_size(Object *obj, Visitor *v, 176 const char *name, void *opaque, 177 Error **errp) 178 { 179 TCGState *s = TCG_STATE(obj); 180 uint32_t value = s->tb_size; 181 182 visit_type_uint32(v, name, &value, errp); 183 } 184 185 static void tcg_set_tb_size(Object *obj, Visitor *v, 186 const char *name, void *opaque, 187 Error **errp) 188 { 189 TCGState *s = TCG_STATE(obj); 190 uint32_t value; 191 192 if (!visit_type_uint32(v, name, &value, errp)) { 193 return; 194 } 195 196 s->tb_size = value; 197 } 198 199 static bool tcg_get_splitwx(Object *obj, Error **errp) 200 { 201 TCGState *s = TCG_STATE(obj); 202 return s->splitwx_enabled; 203 } 204 205 static void tcg_set_splitwx(Object *obj, bool value, Error **errp) 206 { 207 TCGState *s = TCG_STATE(obj); 208 s->splitwx_enabled = value; 209 } 210 211 static void tcg_accel_class_init(ObjectClass *oc, void *data) 212 { 213 AccelClass *ac = ACCEL_CLASS(oc); 214 ac->name = "tcg"; 215 ac->init_machine = tcg_init; 216 ac->allowed = &tcg_allowed; 217 218 object_class_property_add_str(oc, "thread", 219 tcg_get_thread, 220 tcg_set_thread); 221 222 object_class_property_add(oc, "tb-size", "int", 223 tcg_get_tb_size, tcg_set_tb_size, 224 NULL, NULL); 225 object_class_property_set_description(oc, "tb-size", 226 "TCG translation block cache size"); 227 228 object_class_property_add_bool(oc, "split-wx", 229 tcg_get_splitwx, tcg_set_splitwx); 230 object_class_property_set_description(oc, "split-wx", 231 "Map jit pages into separate RW and RX regions"); 232 } 233 234 static const TypeInfo tcg_accel_type = { 235 .name = TYPE_TCG_ACCEL, 236 .parent = TYPE_ACCEL, 237 .instance_init = tcg_accel_instance_init, 238 .class_init = tcg_accel_class_init, 239 .instance_size = sizeof(TCGState), 240 }; 241 242 static void register_accel_types(void) 243 { 244 type_register_static(&tcg_accel_type); 245 } 246 247 type_init(register_accel_types); 248