2
0

cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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, uint8_t *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. CPUClass *klass = CPU_GET_CLASS(cpu);
  203. if (klass->reset != NULL) {
  204. (*klass->reset)(cpu);
  205. }
  206. trace_guest_cpu_reset(cpu);
  207. }
  208. static void cpu_common_reset(CPUState *cpu)
  209. {
  210. CPUClass *cc = CPU_GET_CLASS(cpu);
  211. if (qemu_loglevel_mask(CPU_LOG_RESET)) {
  212. qemu_log("CPU Reset (CPU %d)\n", cpu->cpu_index);
  213. log_cpu_state(cpu, cc->reset_dump_flags);
  214. }
  215. cpu->interrupt_request = 0;
  216. cpu->halted = 0;
  217. cpu->mem_io_pc = 0;
  218. cpu->icount_extra = 0;
  219. atomic_set(&cpu->icount_decr_ptr->u32, 0);
  220. cpu->can_do_io = 1;
  221. cpu->exception_index = -1;
  222. cpu->crash_occurred = false;
  223. cpu->cflags_next_tb = -1;
  224. if (tcg_enabled()) {
  225. cpu_tb_jmp_cache_clear(cpu);
  226. tcg_flush_softmmu_tlb(cpu);
  227. }
  228. }
  229. static bool cpu_common_has_work(CPUState *cs)
  230. {
  231. return false;
  232. }
  233. ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
  234. {
  235. CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
  236. assert(cpu_model && cc->class_by_name);
  237. return cc->class_by_name(cpu_model);
  238. }
  239. static void cpu_common_parse_features(const char *typename, char *features,
  240. Error **errp)
  241. {
  242. char *val;
  243. static bool cpu_globals_initialized;
  244. /* Single "key=value" string being parsed */
  245. char *featurestr = features ? strtok(features, ",") : NULL;
  246. /* should be called only once, catch invalid users */
  247. assert(!cpu_globals_initialized);
  248. cpu_globals_initialized = true;
  249. while (featurestr) {
  250. val = strchr(featurestr, '=');
  251. if (val) {
  252. GlobalProperty *prop = g_new0(typeof(*prop), 1);
  253. *val = 0;
  254. val++;
  255. prop->driver = typename;
  256. prop->property = g_strdup(featurestr);
  257. prop->value = g_strdup(val);
  258. qdev_prop_register_global(prop);
  259. } else {
  260. error_setg(errp, "Expected key=value format, found %s.",
  261. featurestr);
  262. return;
  263. }
  264. featurestr = strtok(NULL, ",");
  265. }
  266. }
  267. static void cpu_common_realizefn(DeviceState *dev, Error **errp)
  268. {
  269. CPUState *cpu = CPU(dev);
  270. Object *machine = qdev_get_machine();
  271. /* qdev_get_machine() can return something that's not TYPE_MACHINE
  272. * if this is one of the user-only emulators; in that case there's
  273. * no need to check the ignore_memory_transaction_failures board flag.
  274. */
  275. if (object_dynamic_cast(machine, TYPE_MACHINE)) {
  276. ObjectClass *oc = object_get_class(machine);
  277. MachineClass *mc = MACHINE_CLASS(oc);
  278. if (mc) {
  279. cpu->ignore_memory_transaction_failures =
  280. mc->ignore_memory_transaction_failures;
  281. }
  282. }
  283. if (dev->hotplugged) {
  284. cpu_synchronize_post_init(cpu);
  285. cpu_resume(cpu);
  286. }
  287. /* NOTE: latest generic point where the cpu is fully realized */
  288. trace_init_vcpu(cpu);
  289. }
  290. static void cpu_common_unrealizefn(DeviceState *dev, Error **errp)
  291. {
  292. CPUState *cpu = CPU(dev);
  293. /* NOTE: latest generic point before the cpu is fully unrealized */
  294. trace_fini_vcpu(cpu);
  295. qemu_plugin_vcpu_exit_hook(cpu);
  296. cpu_exec_unrealizefn(cpu);
  297. }
  298. static void cpu_common_initfn(Object *obj)
  299. {
  300. CPUState *cpu = CPU(obj);
  301. CPUClass *cc = CPU_GET_CLASS(obj);
  302. cpu->cpu_index = UNASSIGNED_CPU_INDEX;
  303. cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX;
  304. cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
  305. /* *-user doesn't have configurable SMP topology */
  306. /* the default value is changed by qemu_init_vcpu() for softmmu */
  307. cpu->nr_cores = 1;
  308. cpu->nr_threads = 1;
  309. qemu_mutex_init(&cpu->work_mutex);
  310. QTAILQ_INIT(&cpu->breakpoints);
  311. QTAILQ_INIT(&cpu->watchpoints);
  312. cpu_exec_initfn(cpu);
  313. }
  314. static void cpu_common_finalize(Object *obj)
  315. {
  316. CPUState *cpu = CPU(obj);
  317. qemu_mutex_destroy(&cpu->work_mutex);
  318. }
  319. static int64_t cpu_common_get_arch_id(CPUState *cpu)
  320. {
  321. return cpu->cpu_index;
  322. }
  323. static vaddr cpu_adjust_watchpoint_address(CPUState *cpu, vaddr addr, int len)
  324. {
  325. return addr;
  326. }
  327. static void generic_handle_interrupt(CPUState *cpu, int mask)
  328. {
  329. cpu->interrupt_request |= mask;
  330. if (!qemu_cpu_is_self(cpu)) {
  331. qemu_cpu_kick(cpu);
  332. }
  333. }
  334. CPUInterruptHandler cpu_interrupt_handler = generic_handle_interrupt;
  335. static void cpu_class_init(ObjectClass *klass, void *data)
  336. {
  337. DeviceClass *dc = DEVICE_CLASS(klass);
  338. CPUClass *k = CPU_CLASS(klass);
  339. k->parse_features = cpu_common_parse_features;
  340. k->reset = cpu_common_reset;
  341. k->get_arch_id = cpu_common_get_arch_id;
  342. k->has_work = cpu_common_has_work;
  343. k->get_paging_enabled = cpu_common_get_paging_enabled;
  344. k->get_memory_mapping = cpu_common_get_memory_mapping;
  345. k->write_elf32_qemunote = cpu_common_write_elf32_qemunote;
  346. k->write_elf32_note = cpu_common_write_elf32_note;
  347. k->write_elf64_qemunote = cpu_common_write_elf64_qemunote;
  348. k->write_elf64_note = cpu_common_write_elf64_note;
  349. k->gdb_read_register = cpu_common_gdb_read_register;
  350. k->gdb_write_register = cpu_common_gdb_write_register;
  351. k->virtio_is_big_endian = cpu_common_virtio_is_big_endian;
  352. k->debug_excp_handler = cpu_common_noop;
  353. k->debug_check_watchpoint = cpu_common_debug_check_watchpoint;
  354. k->cpu_exec_enter = cpu_common_noop;
  355. k->cpu_exec_exit = cpu_common_noop;
  356. k->cpu_exec_interrupt = cpu_common_exec_interrupt;
  357. k->adjust_watchpoint_address = cpu_adjust_watchpoint_address;
  358. set_bit(DEVICE_CATEGORY_CPU, dc->categories);
  359. dc->realize = cpu_common_realizefn;
  360. dc->unrealize = cpu_common_unrealizefn;
  361. dc->props = cpu_common_props;
  362. /*
  363. * Reason: CPUs still need special care by board code: wiring up
  364. * IRQs, adding reset handlers, halting non-first CPUs, ...
  365. */
  366. dc->user_creatable = false;
  367. }
  368. static const TypeInfo cpu_type_info = {
  369. .name = TYPE_CPU,
  370. .parent = TYPE_DEVICE,
  371. .instance_size = sizeof(CPUState),
  372. .instance_init = cpu_common_initfn,
  373. .instance_finalize = cpu_common_finalize,
  374. .abstract = true,
  375. .class_size = sizeof(CPUClass),
  376. .class_init = cpu_class_init,
  377. };
  378. static void cpu_register_types(void)
  379. {
  380. type_register_static(&cpu_type_info);
  381. }
  382. type_init(cpu_register_types)