1 /* 2 * Target-specific parts of the CPU object 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qapi/error.h" 22 #include "qemu/error-report.h" 23 #include "qemu/qemu-print.h" 24 #include "migration/vmstate.h" 25 #ifndef CONFIG_USER_ONLY 26 #include "hw/core/sysemu-cpu-ops.h" 27 #include "exec/address-spaces.h" 28 #endif 29 #include "system/accel-ops.h" 30 #include "system/cpus.h" 31 #include "system/tcg.h" 32 #include "exec/tswap.h" 33 #include "exec/replay-core.h" 34 #include "exec/cpu-common.h" 35 #include "exec/exec-all.h" 36 #include "exec/tb-flush.h" 37 #include "exec/log.h" 38 #include "accel/accel-cpu-target.h" 39 #include "trace/trace-root.h" 40 #include "qemu/accel.h" 41 #include "hw/core/cpu.h" 42 43 #ifndef CONFIG_USER_ONLY 44 static int cpu_common_post_load(void *opaque, int version_id) 45 { 46 if (tcg_enabled()) { 47 CPUState *cpu = opaque; 48 49 /* 50 * 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the 51 * version_id is increased. 52 */ 53 cpu->interrupt_request &= ~0x01; 54 55 tlb_flush(cpu); 56 57 /* 58 * loadvm has just updated the content of RAM, bypassing the 59 * usual mechanisms that ensure we flush TBs for writes to 60 * memory we've translated code from. So we must flush all TBs, 61 * which will now be stale. 62 */ 63 tb_flush(cpu); 64 } 65 66 return 0; 67 } 68 69 static int cpu_common_pre_load(void *opaque) 70 { 71 CPUState *cpu = opaque; 72 73 cpu->exception_index = -1; 74 75 return 0; 76 } 77 78 static bool cpu_common_exception_index_needed(void *opaque) 79 { 80 CPUState *cpu = opaque; 81 82 return tcg_enabled() && cpu->exception_index != -1; 83 } 84 85 static const VMStateDescription vmstate_cpu_common_exception_index = { 86 .name = "cpu_common/exception_index", 87 .version_id = 1, 88 .minimum_version_id = 1, 89 .needed = cpu_common_exception_index_needed, 90 .fields = (const VMStateField[]) { 91 VMSTATE_INT32(exception_index, CPUState), 92 VMSTATE_END_OF_LIST() 93 } 94 }; 95 96 static bool cpu_common_crash_occurred_needed(void *opaque) 97 { 98 CPUState *cpu = opaque; 99 100 return cpu->crash_occurred; 101 } 102 103 static const VMStateDescription vmstate_cpu_common_crash_occurred = { 104 .name = "cpu_common/crash_occurred", 105 .version_id = 1, 106 .minimum_version_id = 1, 107 .needed = cpu_common_crash_occurred_needed, 108 .fields = (const VMStateField[]) { 109 VMSTATE_BOOL(crash_occurred, CPUState), 110 VMSTATE_END_OF_LIST() 111 } 112 }; 113 114 const VMStateDescription vmstate_cpu_common = { 115 .name = "cpu_common", 116 .version_id = 1, 117 .minimum_version_id = 1, 118 .pre_load = cpu_common_pre_load, 119 .post_load = cpu_common_post_load, 120 .fields = (const VMStateField[]) { 121 VMSTATE_UINT32(halted, CPUState), 122 VMSTATE_UINT32(interrupt_request, CPUState), 123 VMSTATE_END_OF_LIST() 124 }, 125 .subsections = (const VMStateDescription * const []) { 126 &vmstate_cpu_common_exception_index, 127 &vmstate_cpu_common_crash_occurred, 128 NULL 129 } 130 }; 131 #endif 132 133 bool cpu_exec_realizefn(CPUState *cpu, Error **errp) 134 { 135 if (!accel_cpu_common_realize(cpu, errp)) { 136 return false; 137 } 138 139 /* Wait until cpu initialization complete before exposing cpu. */ 140 cpu_list_add(cpu); 141 142 #ifdef CONFIG_USER_ONLY 143 assert(qdev_get_vmsd(DEVICE(cpu)) == NULL || 144 qdev_get_vmsd(DEVICE(cpu))->unmigratable); 145 #else 146 if (qdev_get_vmsd(DEVICE(cpu)) == NULL) { 147 vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu); 148 } 149 if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) { 150 vmstate_register(NULL, cpu->cpu_index, cpu->cc->sysemu_ops->legacy_vmsd, cpu); 151 } 152 #endif /* CONFIG_USER_ONLY */ 153 154 return true; 155 } 156 157 void cpu_exec_unrealizefn(CPUState *cpu) 158 { 159 #ifndef CONFIG_USER_ONLY 160 CPUClass *cc = CPU_GET_CLASS(cpu); 161 162 if (cc->sysemu_ops->legacy_vmsd != NULL) { 163 vmstate_unregister(NULL, cc->sysemu_ops->legacy_vmsd, cpu); 164 } 165 if (qdev_get_vmsd(DEVICE(cpu)) == NULL) { 166 vmstate_unregister(NULL, &vmstate_cpu_common, cpu); 167 } 168 #endif 169 170 cpu_list_remove(cpu); 171 /* 172 * Now that the vCPU has been removed from the RCU list, we can call 173 * accel_cpu_common_unrealize, which may free fields using call_rcu. 174 */ 175 accel_cpu_common_unrealize(cpu); 176 } 177 178 void cpu_exec_initfn(CPUState *cpu) 179 { 180 #ifndef CONFIG_USER_ONLY 181 cpu->memory = get_system_memory(); 182 object_ref(OBJECT(cpu->memory)); 183 #endif 184 } 185 186 char *cpu_model_from_type(const char *typename) 187 { 188 const char *suffix = "-" CPU_RESOLVING_TYPE; 189 190 if (!object_class_by_name(typename)) { 191 return NULL; 192 } 193 194 if (g_str_has_suffix(typename, suffix)) { 195 return g_strndup(typename, strlen(typename) - strlen(suffix)); 196 } 197 198 return g_strdup(typename); 199 } 200 201 const char *parse_cpu_option(const char *cpu_option) 202 { 203 ObjectClass *oc; 204 CPUClass *cc; 205 gchar **model_pieces; 206 const char *cpu_type; 207 208 model_pieces = g_strsplit(cpu_option, ",", 2); 209 if (!model_pieces[0]) { 210 error_report("-cpu option cannot be empty"); 211 exit(1); 212 } 213 214 oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]); 215 if (oc == NULL) { 216 error_report("unable to find CPU model '%s'", model_pieces[0]); 217 g_strfreev(model_pieces); 218 exit(EXIT_FAILURE); 219 } 220 221 cpu_type = object_class_get_name(oc); 222 cc = CPU_CLASS(oc); 223 cc->parse_features(cpu_type, model_pieces[1], &error_fatal); 224 g_strfreev(model_pieces); 225 return cpu_type; 226 } 227 228 #ifndef cpu_list 229 static void cpu_list_entry(gpointer data, gpointer user_data) 230 { 231 CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data)); 232 const char *typename = object_class_get_name(OBJECT_CLASS(data)); 233 g_autofree char *model = cpu_model_from_type(typename); 234 235 if (cc->deprecation_note) { 236 qemu_printf(" %s (deprecated)\n", model); 237 } else { 238 qemu_printf(" %s\n", model); 239 } 240 } 241 242 static void cpu_list(void) 243 { 244 GSList *list; 245 246 list = object_class_get_list_sorted(TYPE_CPU, false); 247 qemu_printf("Available CPUs:\n"); 248 g_slist_foreach(list, cpu_list_entry, NULL); 249 g_slist_free(list); 250 } 251 #endif 252 253 void list_cpus(void) 254 { 255 cpu_list(); 256 } 257 258 /* enable or disable single step mode. EXCP_DEBUG is returned by the 259 CPU loop after each instruction */ 260 void cpu_single_step(CPUState *cpu, int enabled) 261 { 262 if (cpu->singlestep_enabled != enabled) { 263 cpu->singlestep_enabled = enabled; 264 265 #if !defined(CONFIG_USER_ONLY) 266 const AccelOpsClass *ops = cpus_get_accel(); 267 if (ops->update_guest_debug) { 268 ops->update_guest_debug(cpu); 269 } 270 #endif 271 272 trace_breakpoint_singlestep(cpu->cpu_index, enabled); 273 } 274 } 275 276 void cpu_abort(CPUState *cpu, const char *fmt, ...) 277 { 278 va_list ap; 279 va_list ap2; 280 281 va_start(ap, fmt); 282 va_copy(ap2, ap); 283 fprintf(stderr, "qemu: fatal: "); 284 vfprintf(stderr, fmt, ap); 285 fprintf(stderr, "\n"); 286 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP); 287 if (qemu_log_separate()) { 288 FILE *logfile = qemu_log_trylock(); 289 if (logfile) { 290 fprintf(logfile, "qemu: fatal: "); 291 vfprintf(logfile, fmt, ap2); 292 fprintf(logfile, "\n"); 293 cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP); 294 qemu_log_unlock(logfile); 295 } 296 } 297 va_end(ap2); 298 va_end(ap); 299 replay_finish(); 300 #if defined(CONFIG_USER_ONLY) 301 { 302 struct sigaction act; 303 sigfillset(&act.sa_mask); 304 act.sa_handler = SIG_DFL; 305 act.sa_flags = 0; 306 sigaction(SIGABRT, &act, NULL); 307 } 308 #endif 309 abort(); 310 } 311 312 bool target_words_bigendian(void) 313 { 314 return TARGET_BIG_ENDIAN; 315 } 316 317 const char *target_name(void) 318 { 319 return TARGET_NAME; 320 } 321