| 1 | /* |
| 2 | * QEMU CPU model |
| 3 | * |
| 4 | * Copyright (c) 2012-2014 SUSE LINUX Products GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program 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 |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see |
| 18 | * <http://www.gnu.org/licenses/gpl-2.0.html> |
| 19 | */ |
| 20 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include "exec/cpu-common.h" |
| 23 | #include "qapi/error.h" |
| 24 | #include "hw/core/cpu.h" |
| 25 | #include "system/hw_accel.h" |
| 26 | #include "qemu/accel.h" |
| 27 | #include "qemu/log.h" |
| 28 | #include "qemu/main-loop.h" |
| 29 | #include "qemu/lockcnt.h" |
| 30 | #include "qemu/error-report.h" |
| 31 | #include "qemu/qemu-print.h" |
| 32 | #include "qemu/target-info.h" |
| 33 | #include "exec/log.h" |
| 34 | #include "exec/gdbstub.h" |
| 35 | #include "system/tcg.h" |
| 36 | #include "hw/core/qdev-properties.h" |
| 37 | #include "trace.h" |
| 38 | #ifdef CONFIG_PLUGIN |
| 39 | #include "qemu/plugin.h" |
| 40 | #endif |
| 41 | #include "cpu-internal.h" |
| 42 | |
| 43 | CPUState *cpu_by_arch_id(int64_t id) |
| 44 | { |
| 45 | CPUState *cpu; |
| 46 | |
| 47 | CPU_FOREACH(cpu) { |
| 48 | if (cpu->cc->get_arch_id(cpu) == id) { |
| 49 | return cpu; |
| 50 | } |
| 51 | } |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | bool cpu_exists(int64_t id) |
| 56 | { |
| 57 | return !!cpu_by_arch_id(id); |
| 58 | } |
| 59 | |
| 60 | CPUState *cpu_create(const char *typename) |
| 61 | { |
| 62 | Error *err = NULL; |
| 63 | CPUState *cpu = CPU(object_new(typename)); |
| 64 | if (!qdev_realize(DEVICE(cpu), NULL, &err)) { |
| 65 | error_report_err(err); |
| 66 | object_unref(OBJECT(cpu)); |
| 67 | exit(EXIT_FAILURE); |
| 68 | } |
| 69 | return cpu; |
| 70 | } |
| 71 | |
| 72 | void cpu_reset_interrupt(CPUState *cpu, int mask) |
| 73 | { |
| 74 | qatomic_and(&cpu->interrupt_request, ~mask); |
| 75 | } |
| 76 | |
| 77 | void cpu_exit(CPUState *cpu) |
| 78 | { |
| 79 | /* Ensure cpu_exec will see the reason why the exit request was set. */ |
| 80 | qatomic_store_release(&cpu->exit_request, true); |
| 81 | qemu_cpu_kick(cpu); |
| 82 | } |
| 83 | |
| 84 | static int cpu_common_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) |
| 85 | { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int cpu_common_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg) |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | void cpu_dump_state(CPUState *cpu, FILE *f, int flags) |
| 95 | { |
| 96 | if (cpu->cc->dump_state) { |
| 97 | cpu_synchronize_state(cpu); |
| 98 | cpu->cc->dump_state(cpu, f, flags); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void cpu_reset(CPUState *cpu) |
| 103 | { |
| 104 | device_cold_reset(DEVICE(cpu)); |
| 105 | |
| 106 | trace_cpu_reset(cpu->cpu_index); |
| 107 | } |
| 108 | |
| 109 | static void cpu_common_reset_hold(Object *obj, ResetType type) |
| 110 | { |
| 111 | CPUState *cpu = CPU(obj); |
| 112 | |
| 113 | cpu->interrupt_request = 0; |
| 114 | cpu->halted = cpu->start_powered_off; |
| 115 | cpu->mem_io_pc = 0; |
| 116 | cpu->icount_extra = 0; |
| 117 | qatomic_set(&cpu->neg.icount_decr.u32, 0); |
| 118 | cpu->neg.can_do_io = true; |
| 119 | cpu->exception_index = -1; |
| 120 | cpu->crash_occurred = false; |
| 121 | cpu->cflags_next_tb = -1; |
| 122 | |
| 123 | cpu_exec_reset_hold(cpu); |
| 124 | } |
| 125 | |
| 126 | static void cpu_common_reset_exit(Object *obj, ResetType type) |
| 127 | { |
| 128 | if (qemu_loglevel_mask(CPU_LOG_RESET)) { |
| 129 | FILE *f = qemu_log_trylock(); |
| 130 | |
| 131 | if (f) { |
| 132 | CPUState *cpu = CPU(obj); |
| 133 | |
| 134 | fprintf(f, "CPU Reset (CPU %d)\n", cpu->cpu_index); |
| 135 | cpu_dump_state(cpu, f, cpu->cc->reset_dump_flags); |
| 136 | qemu_log_unlock(f); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model) |
| 142 | { |
| 143 | ObjectClass *oc; |
| 144 | CPUClass *cc; |
| 145 | |
| 146 | oc = object_class_by_name(typename); |
| 147 | cc = CPU_CLASS(oc); |
| 148 | assert(cc->class_by_name); |
| 149 | assert(cpu_model); |
| 150 | oc = cc->class_by_name(cpu_model); |
| 151 | if (object_class_dynamic_cast(oc, typename) && |
| 152 | !object_class_is_abstract(oc)) { |
| 153 | return oc; |
| 154 | } |
| 155 | |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | char *cpu_model_from_type(const char *typename) |
| 160 | { |
| 161 | g_autofree char *suffix = g_strdup_printf("-%s", target_cpu_type()); |
| 162 | |
| 163 | if (!object_class_by_name(typename)) { |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | if (g_str_has_suffix(typename, suffix)) { |
| 168 | return g_strndup(typename, strlen(typename) - strlen(suffix)); |
| 169 | } |
| 170 | |
| 171 | return g_strdup(typename); |
| 172 | } |
| 173 | |
| 174 | static void cpu_common_parse_features(const char *typename, char *features, |
| 175 | Error **errp) |
| 176 | { |
| 177 | char *val; |
| 178 | static bool cpu_globals_initialized; |
| 179 | /* Single "key=value" string being parsed */ |
| 180 | char *featurestr = features ? strtok(features, ",") : NULL; |
| 181 | |
| 182 | /* should be called only once, catch invalid users */ |
| 183 | assert(!cpu_globals_initialized); |
| 184 | cpu_globals_initialized = true; |
| 185 | |
| 186 | while (featurestr) { |
| 187 | val = strchr(featurestr, '='); |
| 188 | if (val) { |
| 189 | GlobalProperty *prop = g_new0(typeof(*prop), 1); |
| 190 | *val = 0; |
| 191 | val++; |
| 192 | prop->driver = typename; |
| 193 | prop->property = g_strdup(featurestr); |
| 194 | prop->value = g_strdup(val); |
| 195 | qdev_prop_register_global(prop); |
| 196 | } else { |
| 197 | error_setg(errp, "Expected key=value format, found %s.", |
| 198 | featurestr); |
| 199 | return; |
| 200 | } |
| 201 | featurestr = strtok(NULL, ","); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | const char *parse_cpu_option(const char *cpu_option) |
| 206 | { |
| 207 | ObjectClass *oc; |
| 208 | CPUClass *cc; |
| 209 | gchar **model_pieces; |
| 210 | const char *cpu_type; |
| 211 | |
| 212 | model_pieces = g_strsplit(cpu_option, ",", 2); |
| 213 | if (!model_pieces[0]) { |
| 214 | error_report("-cpu option cannot be empty"); |
| 215 | exit(1); |
| 216 | } |
| 217 | |
| 218 | oc = cpu_class_by_name(target_cpu_type(), model_pieces[0]); |
| 219 | if (oc == NULL) { |
| 220 | error_report("unable to find CPU model '%s'", model_pieces[0]); |
| 221 | g_strfreev(model_pieces); |
| 222 | exit(EXIT_FAILURE); |
| 223 | } |
| 224 | |
| 225 | cpu_type = object_class_get_name(oc); |
| 226 | cc = CPU_CLASS(oc); |
| 227 | cc->parse_features(cpu_type, model_pieces[1], &error_fatal); |
| 228 | g_strfreev(model_pieces); |
| 229 | return cpu_type; |
| 230 | } |
| 231 | |
| 232 | bool cpu_common_realize(CPUState *cpu, Error **errp) |
| 233 | { |
| 234 | if (!accel_cpu_common_realize(cpu, errp)) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | gdb_init_cpu(cpu); |
| 239 | |
| 240 | /* Wait until cpu initialization complete before exposing cpu. */ |
| 241 | cpu_list_add(cpu); |
| 242 | |
| 243 | cpu_vmstate_register(cpu); |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | static void cpu_common_realizefn(DeviceState *dev, Error **errp) |
| 249 | { |
| 250 | cpu_exec_realize(CPU(dev), errp); |
| 251 | |
| 252 | /* NOTE: latest generic point where the cpu is fully realized */ |
| 253 | } |
| 254 | |
| 255 | static void cpu_common_unrealizefn(DeviceState *dev) |
| 256 | { |
| 257 | CPUState *cpu = CPU(dev); |
| 258 | |
| 259 | /* Call the plugin hook before clearing the cpu is fully unrealized */ |
| 260 | #ifdef CONFIG_PLUGIN |
| 261 | if (tcg_enabled()) { |
| 262 | qemu_plugin_vcpu_exit_hook(cpu); |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | /* NOTE: latest generic point before the cpu is fully unrealized */ |
| 267 | cpu_common_unrealize(cpu); |
| 268 | } |
| 269 | |
| 270 | void cpu_common_unrealize(CPUState *cpu) |
| 271 | { |
| 272 | cpu_vmstate_unregister(cpu); |
| 273 | |
| 274 | cpu_list_remove(cpu); |
| 275 | /* |
| 276 | * Now that the vCPU has been removed from the RCU list, we can call |
| 277 | * accel_cpu_common_unrealize, which may free fields using call_rcu. |
| 278 | */ |
| 279 | accel_cpu_common_unrealize(cpu); |
| 280 | cpu_destroy_address_spaces(cpu); |
| 281 | } |
| 282 | |
| 283 | static void cpu_common_initfn(Object *obj) |
| 284 | { |
| 285 | CPUState *cpu = CPU(obj); |
| 286 | |
| 287 | cpu_exec_class_post_init(CPU_GET_CLASS(obj)); |
| 288 | |
| 289 | /* cache the cpu class for the hotpath */ |
| 290 | cpu->cc = CPU_GET_CLASS(cpu); |
| 291 | |
| 292 | cpu->cpu_index = UNASSIGNED_CPU_INDEX; |
| 293 | cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX; |
| 294 | cpu->as = NULL; |
| 295 | /* user-mode doesn't have configurable SMP topology */ |
| 296 | /* the default value is changed by qemu_init_vcpu() for system-mode */ |
| 297 | cpu->nr_threads = 1; |
| 298 | |
| 299 | /* allocate storage for thread info, initialise condition variables */ |
| 300 | cpu->thread = g_new0(QemuThread, 1); |
| 301 | cpu->halt_cond = g_new0(QemuCond, 1); |
| 302 | qemu_cond_init(cpu->halt_cond); |
| 303 | |
| 304 | qemu_mutex_init(&cpu->work_mutex); |
| 305 | qemu_lockcnt_init(&cpu->in_ioctl_lock); |
| 306 | QSIMPLEQ_INIT(&cpu->work_list); |
| 307 | QTAILQ_INIT(&cpu->breakpoints); |
| 308 | QTAILQ_INIT(&cpu->watchpoints); |
| 309 | |
| 310 | cpu_exec_init(cpu); |
| 311 | |
| 312 | /* |
| 313 | * Plugin initialization must wait until the cpu start executing |
| 314 | * code, but we must queue this work before the threads are |
| 315 | * created to ensure we don't race. |
| 316 | */ |
| 317 | #ifdef CONFIG_PLUGIN |
| 318 | if (tcg_enabled()) { |
| 319 | cpu->plugin_state = qemu_plugin_create_vcpu_state(); |
| 320 | qemu_plugin_vcpu_init_hook(cpu); |
| 321 | } |
| 322 | #endif |
| 323 | } |
| 324 | |
| 325 | static void cpu_common_finalize(Object *obj) |
| 326 | { |
| 327 | CPUState *cpu = CPU(obj); |
| 328 | |
| 329 | #ifdef CONFIG_PLUGIN |
| 330 | if (tcg_enabled()) { |
| 331 | g_free(cpu->plugin_state); |
| 332 | } |
| 333 | #endif |
| 334 | free_queued_cpu_work(cpu); |
| 335 | /* If cleanup didn't happen in context to gdb_unregister_coprocessor_all */ |
| 336 | if (cpu->gdb_regs) { |
| 337 | g_array_free(cpu->gdb_regs, TRUE); |
| 338 | } |
| 339 | qemu_lockcnt_destroy(&cpu->in_ioctl_lock); |
| 340 | qemu_mutex_destroy(&cpu->work_mutex); |
| 341 | qemu_cond_destroy(cpu->halt_cond); |
| 342 | g_free(cpu->halt_cond); |
| 343 | g_free(cpu->thread); |
| 344 | } |
| 345 | |
| 346 | static int64_t cpu_common_get_arch_id(CPUState *cpu) |
| 347 | { |
| 348 | return cpu->cpu_index; |
| 349 | } |
| 350 | |
| 351 | static void cpu_common_class_init(ObjectClass *klass, const void *data) |
| 352 | { |
| 353 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 354 | ResettableClass *rc = RESETTABLE_CLASS(klass); |
| 355 | CPUClass *k = CPU_CLASS(klass); |
| 356 | |
| 357 | k->parse_features = cpu_common_parse_features; |
| 358 | k->get_arch_id = cpu_common_get_arch_id; |
| 359 | k->gdb_read_register = cpu_common_gdb_read_register; |
| 360 | k->gdb_write_register = cpu_common_gdb_write_register; |
| 361 | set_bit(DEVICE_CATEGORY_CPU, dc->categories); |
| 362 | dc->realize = cpu_common_realizefn; |
| 363 | dc->unrealize = cpu_common_unrealizefn; |
| 364 | rc->phases.hold = cpu_common_reset_hold; |
| 365 | rc->phases.exit = cpu_common_reset_exit; |
| 366 | cpu_class_init_props(dc); |
| 367 | /* |
| 368 | * Reason: CPUs still need special care by board code: wiring up |
| 369 | * IRQs, adding reset handlers, halting non-first CPUs, ... |
| 370 | */ |
| 371 | dc->user_creatable = false; |
| 372 | } |
| 373 | |
| 374 | static const TypeInfo cpu_type_info = { |
| 375 | .name = TYPE_CPU, |
| 376 | .parent = TYPE_DEVICE, |
| 377 | .instance_size = sizeof(CPUState), |
| 378 | .instance_init = cpu_common_initfn, |
| 379 | .instance_finalize = cpu_common_finalize, |
| 380 | .abstract = true, |
| 381 | .class_size = sizeof(CPUClass), |
| 382 | .class_init = cpu_common_class_init, |
| 383 | }; |
| 384 | |
| 385 | static void cpu_register_types(void) |
| 386 | { |
| 387 | type_register_static(&cpu_type_info); |
| 388 | } |
| 389 | |
| 390 | type_init(cpu_register_types) |
| 391 | |
| 392 | static void cpu_list_entry(gpointer data, gpointer user_data) |
| 393 | { |
| 394 | CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data)); |
| 395 | const char *typename = object_class_get_name(OBJECT_CLASS(data)); |
| 396 | g_autofree char *model = cpu_model_from_type(typename); |
| 397 | |
| 398 | if (cc->deprecation_note) { |
| 399 | qemu_printf(" %s (deprecated)\n", model); |
| 400 | } else { |
| 401 | qemu_printf(" %s\n", model); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void list_cpus(void) |
| 406 | { |
| 407 | CPUClass *cc = CPU_CLASS(object_class_by_name(target_cpu_type())); |
| 408 | |
| 409 | if (cc->list_cpus) { |
| 410 | cc->list_cpus(); |
| 411 | } else { |
| 412 | GSList *list; |
| 413 | |
| 414 | list = object_class_get_list_sorted(TYPE_CPU, false); |
| 415 | qemu_printf("Available CPUs:\n"); |
| 416 | g_slist_foreach(list, cpu_list_entry, NULL); |
| 417 | g_slist_free(list); |
| 418 | } |
| 419 | } |