cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "sysemu/hw_accel.h"
  24. #include "qemu/notify.h"
  25. #include "qemu/log.h"
  26. #include "qemu/main-loop.h"
  27. #include "exec/log.h"
  28. #include "qemu/error-report.h"
  29. #include "qemu/qemu-print.h"
  30. #include "sysemu/tcg.h"
  31. #include "hw/boards.h"
  32. #include "hw/qdev-properties.h"
  33. #include "trace-root.h"
  34. #include "qemu/plugin.h"
  35. CPUInterruptHandler cpu_interrupt_handler;
  36. CPUState *cpu_by_arch_id(int64_t id)
  37. {
  38. CPUState *cpu;
  39. CPU_FOREACH(cpu) {
  40. CPUClass *cc = CPU_GET_CLASS(cpu);
  41. if (cc->get_arch_id(cpu) == id) {
  42. return cpu;
  43. }
  44. }
  45. return NULL;
  46. }
  47. bool cpu_exists(int64_t id)
  48. {
  49. return !!cpu_by_arch_id(id);
  50. }
  51. CPUState *cpu_create(const char *typename)
  52. {
  53. Error *err = NULL;
  54. CPUState *cpu = CPU(object_new(typename));
  55. object_property_set_bool(OBJECT(cpu), true, "realized", &err);
  56. if (err != NULL) {
  57. error_report_err(err);
  58. object_unref(OBJECT(cpu));
  59. exit(EXIT_FAILURE);
  60. }
  61. return cpu;
  62. }
  63. bool cpu_paging_enabled(const CPUState *cpu)
  64. {
  65. CPUClass *cc = CPU_GET_CLASS(cpu);
  66. return cc->get_paging_enabled(cpu);
  67. }
  68. static bool cpu_common_get_paging_enabled(const CPUState *cpu)
  69. {
  70. return false;
  71. }
  72. void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
  73. Error **errp)
  74. {
  75. CPUClass *cc = CPU_GET_CLASS(cpu);
  76. cc->get_memory_mapping(cpu, list, errp);
  77. }
  78. static void cpu_common_get_memory_mapping(CPUState *cpu,
  79. MemoryMappingList *list,
  80. Error **errp)
  81. {
  82. error_setg(errp, "Obtaining memory mappings is unsupported on this CPU.");
  83. }
  84. /* Resetting the IRQ comes from across the code base so we take the
  85. * BQL here if we need to. cpu_interrupt assumes it is held.*/
  86. void cpu_reset_interrupt(CPUState *cpu, int mask)
  87. {
  88. bool need_lock = !qemu_mutex_iothread_locked();
  89. if (need_lock) {
  90. qemu_mutex_lock_iothread();
  91. }
  92. cpu->interrupt_request &= ~mask;
  93. if (need_lock) {
  94. qemu_mutex_unlock_iothread();
  95. }
  96. }
  97. void cpu_exit(CPUState *cpu)
  98. {
  99. atomic_set(&cpu->exit_request, 1);
  100. /* Ensure cpu_exec will see the exit request after TCG has exited. */
  101. smp_wmb();
  102. atomic_set(&cpu->icount_decr_ptr->u16.high, -1);
  103. }
  104. int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  105. void *opaque)
  106. {
  107. CPUClass *cc = CPU_GET_CLASS(cpu);
  108. return (*cc->write_elf32_qemunote)(f, cpu, opaque);
  109. }
  110. static int cpu_common_write_elf32_qemunote(WriteCoreDumpFunction f,
  111. CPUState *cpu, void *opaque)
  112. {
  113. return 0;
  114. }
  115. int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
  116. int cpuid, void *opaque)
  117. {
  118. CPUClass *cc = CPU_GET_CLASS(cpu);
  119. return (*cc->write_elf32_note)(f, cpu, cpuid, opaque);
  120. }
  121. static int cpu_common_write_elf32_note(WriteCoreDumpFunction f,
  122. CPUState *cpu, int cpuid,
  123. void *opaque)
  124. {
  125. return -1;
  126. }
  127. int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  128. void *opaque)
  129. {
  130. CPUClass *cc = CPU_GET_CLASS(cpu);
  131. return (*cc->write_elf64_qemunote)(f, cpu, opaque);
  132. }
  133. static int cpu_common_write_elf64_qemunote(WriteCoreDumpFunction f,
  134. CPUState *cpu, void *opaque)
  135. {
  136. return 0;
  137. }
  138. int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
  139. int cpuid, void *opaque)
  140. {
  141. CPUClass *cc = CPU_GET_CLASS(cpu);
  142. return (*cc->write_elf64_note)(f, cpu, cpuid, opaque);
  143. }
  144. static int cpu_common_write_elf64_note(WriteCoreDumpFunction f,
  145. CPUState *cpu, int cpuid,
  146. void *opaque)
  147. {
  148. return -1;
  149. }
  150. static int cpu_common_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
  151. {
  152. return 0;
  153. }
  154. static int cpu_common_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg)
  155. {
  156. return 0;
  157. }
  158. static bool cpu_common_debug_check_watchpoint(CPUState *cpu, CPUWatchpoint *wp)
  159. {
  160. /* If no extra check is required, QEMU watchpoint match can be considered
  161. * as an architectural match.
  162. */
  163. return true;
  164. }
  165. static bool cpu_common_virtio_is_big_endian(CPUState *cpu)
  166. {
  167. return target_words_bigendian();
  168. }
  169. static void cpu_common_noop(CPUState *cpu)
  170. {
  171. }
  172. static bool cpu_common_exec_interrupt(CPUState *cpu, int int_req)
  173. {
  174. return false;
  175. }
  176. GuestPanicInformation *cpu_get_crash_info(CPUState *cpu)
  177. {
  178. CPUClass *cc = CPU_GET_CLASS(cpu);
  179. GuestPanicInformation *res = NULL;
  180. if (cc->get_crash_info) {
  181. res = cc->get_crash_info(cpu);
  182. }
  183. return res;
  184. }
  185. void cpu_dump_state(CPUState *cpu, FILE *f, int flags)
  186. {
  187. CPUClass *cc = CPU_GET_CLASS(cpu);
  188. if (cc->dump_state) {
  189. cpu_synchronize_state(cpu);
  190. cc->dump_state(cpu, f, flags);
  191. }
  192. }
  193. void cpu_dump_statistics(CPUState *cpu, int flags)
  194. {
  195. CPUClass *cc = CPU_GET_CLASS(cpu);
  196. if (cc->dump_statistics) {
  197. cc->dump_statistics(cpu, flags);
  198. }
  199. }
  200. void cpu_reset(CPUState *cpu)
  201. {
  202. device_cold_reset(DEVICE(cpu));
  203. trace_guest_cpu_reset(cpu);
  204. }
  205. static void cpu_common_reset(DeviceState *dev)
  206. {
  207. CPUState *cpu = CPU(dev);
  208. CPUClass *cc = CPU_GET_CLASS(cpu);
  209. if (qemu_loglevel_mask(CPU_LOG_RESET)) {
  210. qemu_log("CPU Reset (CPU %d)\n", cpu->cpu_index);
  211. log_cpu_state(cpu, cc->reset_dump_flags);
  212. }
  213. cpu->interrupt_request = 0;
  214. cpu->halted = 0;
  215. cpu->mem_io_pc = 0;
  216. cpu->icount_extra = 0;
  217. atomic_set(&cpu->icount_decr_ptr->u32, 0);
  218. cpu->can_do_io = 1;
  219. cpu->exception_index = -1;
  220. cpu->crash_occurred = false;
  221. cpu->cflags_next_tb = -1;
  222. if (tcg_enabled()) {
  223. cpu_tb_jmp_cache_clear(cpu);
  224. tcg_flush_softmmu_tlb(cpu);
  225. }
  226. }
  227. static bool cpu_common_has_work(CPUState *cs)
  228. {
  229. return false;
  230. }
  231. ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
  232. {
  233. CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
  234. assert(cpu_model && cc->class_by_name);
  235. return cc->class_by_name(cpu_model);
  236. }
  237. static void cpu_common_parse_features(const char *typename, char *features,
  238. Error **errp)
  239. {
  240. char *val;
  241. static bool cpu_globals_initialized;
  242. /* Single "key=value" string being parsed */
  243. char *featurestr = features ? strtok(features, ",") : NULL;
  244. /* should be called only once, catch invalid users */
  245. assert(!cpu_globals_initialized);
  246. cpu_globals_initialized = true;
  247. while (featurestr) {
  248. val = strchr(featurestr, '=');
  249. if (val) {
  250. GlobalProperty *prop = g_new0(typeof(*prop), 1);
  251. *val = 0;
  252. val++;
  253. prop->driver = typename;
  254. prop->property = g_strdup(featurestr);
  255. prop->value = g_strdup(val);
  256. qdev_prop_register_global(prop);
  257. } else {
  258. error_setg(errp, "Expected key=value format, found %s.",
  259. featurestr);
  260. return;
  261. }
  262. featurestr = strtok(NULL, ",");
  263. }
  264. }
  265. static void cpu_common_realizefn(DeviceState *dev, Error **errp)
  266. {
  267. CPUState *cpu = CPU(dev);
  268. Object *machine = qdev_get_machine();
  269. /* qdev_get_machine() can return something that's not TYPE_MACHINE
  270. * if this is one of the user-only emulators; in that case there's
  271. * no need to check the ignore_memory_transaction_failures board flag.
  272. */
  273. if (object_dynamic_cast(machine, TYPE_MACHINE)) {
  274. ObjectClass *oc = object_get_class(machine);
  275. MachineClass *mc = MACHINE_CLASS(oc);
  276. if (mc) {
  277. cpu->ignore_memory_transaction_failures =
  278. mc->ignore_memory_transaction_failures;
  279. }
  280. }
  281. if (dev->hotplugged) {
  282. cpu_synchronize_post_init(cpu);
  283. cpu_resume(cpu);
  284. }
  285. /* NOTE: latest generic point where the cpu is fully realized */
  286. trace_init_vcpu(cpu);
  287. }
  288. static void cpu_common_unrealizefn(DeviceState *dev)
  289. {
  290. CPUState *cpu = CPU(dev);
  291. /* NOTE: latest generic point before the cpu is fully unrealized */
  292. trace_fini_vcpu(cpu);
  293. qemu_plugin_vcpu_exit_hook(cpu);
  294. cpu_exec_unrealizefn(cpu);
  295. }
  296. static void cpu_common_initfn(Object *obj)
  297. {
  298. CPUState *cpu = CPU(obj);
  299. CPUClass *cc = CPU_GET_CLASS(obj);
  300. cpu->cpu_index = UNASSIGNED_CPU_INDEX;
  301. cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX;
  302. cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
  303. /* *-user doesn't have configurable SMP topology */
  304. /* the default value is changed by qemu_init_vcpu() for softmmu */
  305. cpu->nr_cores = 1;
  306. cpu->nr_threads = 1;
  307. qemu_mutex_init(&cpu->work_mutex);
  308. QTAILQ_INIT(&cpu->breakpoints);
  309. QTAILQ_INIT(&cpu->watchpoints);
  310. cpu_exec_initfn(cpu);
  311. }
  312. static void cpu_common_finalize(Object *obj)
  313. {
  314. CPUState *cpu = CPU(obj);
  315. qemu_mutex_destroy(&cpu->work_mutex);
  316. }
  317. static int64_t cpu_common_get_arch_id(CPUState *cpu)
  318. {
  319. return cpu->cpu_index;
  320. }
  321. static vaddr cpu_adjust_watchpoint_address(CPUState *cpu, vaddr addr, int len)
  322. {
  323. return addr;
  324. }
  325. static void generic_handle_interrupt(CPUState *cpu, int mask)
  326. {
  327. cpu->interrupt_request |= mask;
  328. if (!qemu_cpu_is_self(cpu)) {
  329. qemu_cpu_kick(cpu);
  330. }
  331. }
  332. CPUInterruptHandler cpu_interrupt_handler = generic_handle_interrupt;
  333. static void cpu_class_init(ObjectClass *klass, void *data)
  334. {
  335. DeviceClass *dc = DEVICE_CLASS(klass);
  336. CPUClass *k = CPU_CLASS(klass);
  337. k->parse_features = cpu_common_parse_features;
  338. k->get_arch_id = cpu_common_get_arch_id;
  339. k->has_work = cpu_common_has_work;
  340. k->get_paging_enabled = cpu_common_get_paging_enabled;
  341. k->get_memory_mapping = cpu_common_get_memory_mapping;
  342. k->write_elf32_qemunote = cpu_common_write_elf32_qemunote;
  343. k->write_elf32_note = cpu_common_write_elf32_note;
  344. k->write_elf64_qemunote = cpu_common_write_elf64_qemunote;
  345. k->write_elf64_note = cpu_common_write_elf64_note;
  346. k->gdb_read_register = cpu_common_gdb_read_register;
  347. k->gdb_write_register = cpu_common_gdb_write_register;
  348. k->virtio_is_big_endian = cpu_common_virtio_is_big_endian;
  349. k->debug_excp_handler = cpu_common_noop;
  350. k->debug_check_watchpoint = cpu_common_debug_check_watchpoint;
  351. k->cpu_exec_enter = cpu_common_noop;
  352. k->cpu_exec_exit = cpu_common_noop;
  353. k->cpu_exec_interrupt = cpu_common_exec_interrupt;
  354. k->adjust_watchpoint_address = cpu_adjust_watchpoint_address;
  355. set_bit(DEVICE_CATEGORY_CPU, dc->categories);
  356. dc->realize = cpu_common_realizefn;
  357. dc->unrealize = cpu_common_unrealizefn;
  358. dc->reset = cpu_common_reset;
  359. device_class_set_props(dc, cpu_common_props);
  360. /*
  361. * Reason: CPUs still need special care by board code: wiring up
  362. * IRQs, adding reset handlers, halting non-first CPUs, ...
  363. */
  364. dc->user_creatable = false;
  365. }
  366. static const TypeInfo cpu_type_info = {
  367. .name = TYPE_CPU,
  368. .parent = TYPE_DEVICE,
  369. .instance_size = sizeof(CPUState),
  370. .instance_init = cpu_common_initfn,
  371. .instance_finalize = cpu_common_finalize,
  372. .abstract = true,
  373. .class_size = sizeof(CPUClass),
  374. .class_init = cpu_class_init,
  375. };
  376. static void cpu_register_types(void)
  377. {
  378. type_register_static(&cpu_type_info);
  379. }
  380. type_init(cpu_register_types)