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