cpu-common.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "hw/core/cpu.h"
  23. #include "system/hw_accel.h"
  24. #include "qemu/log.h"
  25. #include "qemu/main-loop.h"
  26. #include "qemu/lockcnt.h"
  27. #include "exec/log.h"
  28. #include "exec/gdbstub.h"
  29. #include "system/tcg.h"
  30. #include "hw/boards.h"
  31. #include "hw/qdev-properties.h"
  32. #include "trace.h"
  33. #ifdef CONFIG_PLUGIN
  34. #include "qemu/plugin.h"
  35. #endif
  36. CPUState *cpu_by_arch_id(int64_t id)
  37. {
  38. CPUState *cpu;
  39. CPU_FOREACH(cpu) {
  40. if (cpu->cc->get_arch_id(cpu) == id) {
  41. return cpu;
  42. }
  43. }
  44. return NULL;
  45. }
  46. bool cpu_exists(int64_t id)
  47. {
  48. return !!cpu_by_arch_id(id);
  49. }
  50. CPUState *cpu_create(const char *typename)
  51. {
  52. Error *err = NULL;
  53. CPUState *cpu = CPU(object_new(typename));
  54. if (!qdev_realize(DEVICE(cpu), NULL, &err)) {
  55. error_report_err(err);
  56. object_unref(OBJECT(cpu));
  57. exit(EXIT_FAILURE);
  58. }
  59. return cpu;
  60. }
  61. /* Resetting the IRQ comes from across the code base so we take the
  62. * BQL here if we need to. cpu_interrupt assumes it is held.*/
  63. void cpu_reset_interrupt(CPUState *cpu, int mask)
  64. {
  65. bool need_lock = !bql_locked();
  66. if (need_lock) {
  67. bql_lock();
  68. }
  69. cpu->interrupt_request &= ~mask;
  70. if (need_lock) {
  71. bql_unlock();
  72. }
  73. }
  74. void cpu_exit(CPUState *cpu)
  75. {
  76. qatomic_set(&cpu->exit_request, 1);
  77. /* Ensure cpu_exec will see the exit request after TCG has exited. */
  78. smp_wmb();
  79. qatomic_set(&cpu->neg.icount_decr.u16.high, -1);
  80. }
  81. static int cpu_common_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
  82. {
  83. return 0;
  84. }
  85. static int cpu_common_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg)
  86. {
  87. return 0;
  88. }
  89. void cpu_dump_state(CPUState *cpu, FILE *f, int flags)
  90. {
  91. if (cpu->cc->dump_state) {
  92. cpu_synchronize_state(cpu);
  93. cpu->cc->dump_state(cpu, f, flags);
  94. }
  95. }
  96. void cpu_reset(CPUState *cpu)
  97. {
  98. device_cold_reset(DEVICE(cpu));
  99. trace_cpu_reset(cpu->cpu_index);
  100. }
  101. static void cpu_common_reset_hold(Object *obj, ResetType type)
  102. {
  103. CPUState *cpu = CPU(obj);
  104. if (qemu_loglevel_mask(CPU_LOG_RESET)) {
  105. qemu_log("CPU Reset (CPU %d)\n", cpu->cpu_index);
  106. log_cpu_state(cpu, cpu->cc->reset_dump_flags);
  107. }
  108. cpu->interrupt_request = 0;
  109. cpu->halted = cpu->start_powered_off;
  110. cpu->mem_io_pc = 0;
  111. cpu->icount_extra = 0;
  112. qatomic_set(&cpu->neg.icount_decr.u32, 0);
  113. cpu->neg.can_do_io = true;
  114. cpu->exception_index = -1;
  115. cpu->crash_occurred = false;
  116. cpu->cflags_next_tb = -1;
  117. cpu_exec_reset_hold(cpu);
  118. }
  119. ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
  120. {
  121. ObjectClass *oc;
  122. CPUClass *cc;
  123. oc = object_class_by_name(typename);
  124. cc = CPU_CLASS(oc);
  125. assert(cc->class_by_name);
  126. assert(cpu_model);
  127. oc = cc->class_by_name(cpu_model);
  128. if (object_class_dynamic_cast(oc, typename) &&
  129. !object_class_is_abstract(oc)) {
  130. return oc;
  131. }
  132. return NULL;
  133. }
  134. static void cpu_common_parse_features(const char *typename, char *features,
  135. Error **errp)
  136. {
  137. char *val;
  138. static bool cpu_globals_initialized;
  139. /* Single "key=value" string being parsed */
  140. char *featurestr = features ? strtok(features, ",") : NULL;
  141. /* should be called only once, catch invalid users */
  142. assert(!cpu_globals_initialized);
  143. cpu_globals_initialized = true;
  144. while (featurestr) {
  145. val = strchr(featurestr, '=');
  146. if (val) {
  147. GlobalProperty *prop = g_new0(typeof(*prop), 1);
  148. *val = 0;
  149. val++;
  150. prop->driver = typename;
  151. prop->property = g_strdup(featurestr);
  152. prop->value = g_strdup(val);
  153. qdev_prop_register_global(prop);
  154. } else {
  155. error_setg(errp, "Expected key=value format, found %s.",
  156. featurestr);
  157. return;
  158. }
  159. featurestr = strtok(NULL, ",");
  160. }
  161. }
  162. bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
  163. {
  164. if (!accel_cpu_common_realize(cpu, errp)) {
  165. return false;
  166. }
  167. /* Wait until cpu initialization complete before exposing cpu. */
  168. cpu_list_add(cpu);
  169. cpu_vmstate_register(cpu);
  170. return true;
  171. }
  172. static void cpu_common_realizefn(DeviceState *dev, Error **errp)
  173. {
  174. CPUState *cpu = CPU(dev);
  175. Object *machine = qdev_get_machine();
  176. /* qdev_get_machine() can return something that's not TYPE_MACHINE
  177. * if this is one of the user-only emulators; in that case there's
  178. * no need to check the ignore_memory_transaction_failures board flag.
  179. */
  180. if (object_dynamic_cast(machine, TYPE_MACHINE)) {
  181. MachineClass *mc = MACHINE_GET_CLASS(machine);
  182. if (mc) {
  183. cpu->ignore_memory_transaction_failures =
  184. mc->ignore_memory_transaction_failures;
  185. }
  186. }
  187. if (dev->hotplugged) {
  188. cpu_synchronize_post_init(cpu);
  189. cpu_resume(cpu);
  190. }
  191. /* NOTE: latest generic point where the cpu is fully realized */
  192. }
  193. static void cpu_common_unrealizefn(DeviceState *dev)
  194. {
  195. CPUState *cpu = CPU(dev);
  196. /* Call the plugin hook before clearing the cpu is fully unrealized */
  197. #ifdef CONFIG_PLUGIN
  198. if (tcg_enabled()) {
  199. qemu_plugin_vcpu_exit_hook(cpu);
  200. }
  201. #endif
  202. /* NOTE: latest generic point before the cpu is fully unrealized */
  203. cpu_exec_unrealizefn(cpu);
  204. }
  205. void cpu_exec_unrealizefn(CPUState *cpu)
  206. {
  207. cpu_vmstate_unregister(cpu);
  208. cpu_list_remove(cpu);
  209. /*
  210. * Now that the vCPU has been removed from the RCU list, we can call
  211. * accel_cpu_common_unrealize, which may free fields using call_rcu.
  212. */
  213. accel_cpu_common_unrealize(cpu);
  214. }
  215. static void cpu_common_initfn(Object *obj)
  216. {
  217. CPUState *cpu = CPU(obj);
  218. cpu_exec_class_post_init(CPU_GET_CLASS(obj));
  219. /* cache the cpu class for the hotpath */
  220. cpu->cc = CPU_GET_CLASS(cpu);
  221. gdb_init_cpu(cpu);
  222. cpu->cpu_index = UNASSIGNED_CPU_INDEX;
  223. cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX;
  224. cpu->as = NULL;
  225. cpu->num_ases = 0;
  226. /* user-mode doesn't have configurable SMP topology */
  227. /* the default value is changed by qemu_init_vcpu() for system-mode */
  228. cpu->nr_threads = 1;
  229. /* allocate storage for thread info, initialise condition variables */
  230. cpu->thread = g_new0(QemuThread, 1);
  231. cpu->halt_cond = g_new0(QemuCond, 1);
  232. qemu_cond_init(cpu->halt_cond);
  233. qemu_mutex_init(&cpu->work_mutex);
  234. qemu_lockcnt_init(&cpu->in_ioctl_lock);
  235. QSIMPLEQ_INIT(&cpu->work_list);
  236. QTAILQ_INIT(&cpu->breakpoints);
  237. QTAILQ_INIT(&cpu->watchpoints);
  238. cpu_exec_initfn(cpu);
  239. /*
  240. * Plugin initialization must wait until the cpu start executing
  241. * code, but we must queue this work before the threads are
  242. * created to ensure we don't race.
  243. */
  244. #ifdef CONFIG_PLUGIN
  245. if (tcg_enabled()) {
  246. cpu->plugin_state = qemu_plugin_create_vcpu_state();
  247. qemu_plugin_vcpu_init_hook(cpu);
  248. }
  249. #endif
  250. }
  251. static void cpu_common_finalize(Object *obj)
  252. {
  253. CPUState *cpu = CPU(obj);
  254. #ifdef CONFIG_PLUGIN
  255. if (tcg_enabled()) {
  256. g_free(cpu->plugin_state);
  257. }
  258. #endif
  259. free_queued_cpu_work(cpu);
  260. /* If cleanup didn't happen in context to gdb_unregister_coprocessor_all */
  261. if (cpu->gdb_regs) {
  262. g_array_free(cpu->gdb_regs, TRUE);
  263. }
  264. qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
  265. qemu_mutex_destroy(&cpu->work_mutex);
  266. qemu_cond_destroy(cpu->halt_cond);
  267. g_free(cpu->halt_cond);
  268. g_free(cpu->thread);
  269. }
  270. static int64_t cpu_common_get_arch_id(CPUState *cpu)
  271. {
  272. return cpu->cpu_index;
  273. }
  274. static void cpu_common_class_init(ObjectClass *klass, void *data)
  275. {
  276. DeviceClass *dc = DEVICE_CLASS(klass);
  277. ResettableClass *rc = RESETTABLE_CLASS(klass);
  278. CPUClass *k = CPU_CLASS(klass);
  279. k->parse_features = cpu_common_parse_features;
  280. k->get_arch_id = cpu_common_get_arch_id;
  281. k->gdb_read_register = cpu_common_gdb_read_register;
  282. k->gdb_write_register = cpu_common_gdb_write_register;
  283. set_bit(DEVICE_CATEGORY_CPU, dc->categories);
  284. dc->realize = cpu_common_realizefn;
  285. dc->unrealize = cpu_common_unrealizefn;
  286. rc->phases.hold = cpu_common_reset_hold;
  287. cpu_class_init_props(dc);
  288. /*
  289. * Reason: CPUs still need special care by board code: wiring up
  290. * IRQs, adding reset handlers, halting non-first CPUs, ...
  291. */
  292. dc->user_creatable = false;
  293. }
  294. static const TypeInfo cpu_type_info = {
  295. .name = TYPE_CPU,
  296. .parent = TYPE_DEVICE,
  297. .instance_size = sizeof(CPUState),
  298. .instance_init = cpu_common_initfn,
  299. .instance_finalize = cpu_common_finalize,
  300. .abstract = true,
  301. .class_size = sizeof(CPUClass),
  302. .class_init = cpu_common_class_init,
  303. };
  304. static void cpu_register_types(void)
  305. {
  306. type_register_static(&cpu_type_info);
  307. }
  308. type_init(cpu_register_types)