2
0

cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. if (!qdev_realize(DEVICE(cpu), NULL, &err)) {
  56. error_report_err(err);
  57. object_unref(OBJECT(cpu));
  58. exit(EXIT_FAILURE);
  59. }
  60. return cpu;
  61. }
  62. bool cpu_paging_enabled(const CPUState *cpu)
  63. {
  64. CPUClass *cc = CPU_GET_CLASS(cpu);
  65. return cc->get_paging_enabled(cpu);
  66. }
  67. static bool cpu_common_get_paging_enabled(const CPUState *cpu)
  68. {
  69. return false;
  70. }
  71. void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
  72. Error **errp)
  73. {
  74. CPUClass *cc = CPU_GET_CLASS(cpu);
  75. cc->get_memory_mapping(cpu, list, errp);
  76. }
  77. static void cpu_common_get_memory_mapping(CPUState *cpu,
  78. MemoryMappingList *list,
  79. Error **errp)
  80. {
  81. error_setg(errp, "Obtaining memory mappings is unsupported on this CPU.");
  82. }
  83. /* Resetting the IRQ comes from across the code base so we take the
  84. * BQL here if we need to. cpu_interrupt assumes it is held.*/
  85. void cpu_reset_interrupt(CPUState *cpu, int mask)
  86. {
  87. bool need_lock = !qemu_mutex_iothread_locked();
  88. if (need_lock) {
  89. qemu_mutex_lock_iothread();
  90. }
  91. cpu->interrupt_request &= ~mask;
  92. if (need_lock) {
  93. qemu_mutex_unlock_iothread();
  94. }
  95. }
  96. void cpu_exit(CPUState *cpu)
  97. {
  98. atomic_set(&cpu->exit_request, 1);
  99. /* Ensure cpu_exec will see the exit request after TCG has exited. */
  100. smp_wmb();
  101. atomic_set(&cpu->icount_decr_ptr->u16.high, -1);
  102. }
  103. int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  104. void *opaque)
  105. {
  106. CPUClass *cc = CPU_GET_CLASS(cpu);
  107. return (*cc->write_elf32_qemunote)(f, cpu, opaque);
  108. }
  109. static int cpu_common_write_elf32_qemunote(WriteCoreDumpFunction f,
  110. CPUState *cpu, void *opaque)
  111. {
  112. return 0;
  113. }
  114. int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
  115. int cpuid, void *opaque)
  116. {
  117. CPUClass *cc = CPU_GET_CLASS(cpu);
  118. return (*cc->write_elf32_note)(f, cpu, cpuid, opaque);
  119. }
  120. static int cpu_common_write_elf32_note(WriteCoreDumpFunction f,
  121. CPUState *cpu, int cpuid,
  122. void *opaque)
  123. {
  124. return -1;
  125. }
  126. int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  127. void *opaque)
  128. {
  129. CPUClass *cc = CPU_GET_CLASS(cpu);
  130. return (*cc->write_elf64_qemunote)(f, cpu, opaque);
  131. }
  132. static int cpu_common_write_elf64_qemunote(WriteCoreDumpFunction f,
  133. CPUState *cpu, void *opaque)
  134. {
  135. return 0;
  136. }
  137. int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
  138. int cpuid, void *opaque)
  139. {
  140. CPUClass *cc = CPU_GET_CLASS(cpu);
  141. return (*cc->write_elf64_note)(f, cpu, cpuid, opaque);
  142. }
  143. static int cpu_common_write_elf64_note(WriteCoreDumpFunction f,
  144. CPUState *cpu, int cpuid,
  145. void *opaque)
  146. {
  147. return -1;
  148. }
  149. static int cpu_common_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
  150. {
  151. return 0;
  152. }
  153. static int cpu_common_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg)
  154. {
  155. return 0;
  156. }
  157. static bool cpu_common_debug_check_watchpoint(CPUState *cpu, CPUWatchpoint *wp)
  158. {
  159. /* If no extra check is required, QEMU watchpoint match can be considered
  160. * as an architectural match.
  161. */
  162. return true;
  163. }
  164. static bool cpu_common_virtio_is_big_endian(CPUState *cpu)
  165. {
  166. return target_words_bigendian();
  167. }
  168. static void cpu_common_noop(CPUState *cpu)
  169. {
  170. }
  171. static bool cpu_common_exec_interrupt(CPUState *cpu, int int_req)
  172. {
  173. return false;
  174. }
  175. #if !defined(CONFIG_USER_ONLY)
  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. #endif
  186. void cpu_dump_state(CPUState *cpu, FILE *f, int flags)
  187. {
  188. CPUClass *cc = CPU_GET_CLASS(cpu);
  189. if (cc->dump_state) {
  190. cpu_synchronize_state(cpu);
  191. cc->dump_state(cpu, f, flags);
  192. }
  193. }
  194. void cpu_dump_statistics(CPUState *cpu, int flags)
  195. {
  196. CPUClass *cc = CPU_GET_CLASS(cpu);
  197. if (cc->dump_statistics) {
  198. cc->dump_statistics(cpu, flags);
  199. }
  200. }
  201. void cpu_reset(CPUState *cpu)
  202. {
  203. device_cold_reset(DEVICE(cpu));
  204. trace_guest_cpu_reset(cpu);
  205. }
  206. static void cpu_common_reset(DeviceState *dev)
  207. {
  208. CPUState *cpu = CPU(dev);
  209. CPUClass *cc = CPU_GET_CLASS(cpu);
  210. if (qemu_loglevel_mask(CPU_LOG_RESET)) {
  211. qemu_log("CPU Reset (CPU %d)\n", cpu->cpu_index);
  212. log_cpu_state(cpu, cc->reset_dump_flags);
  213. }
  214. cpu->interrupt_request = 0;
  215. cpu->halted = 0;
  216. cpu->mem_io_pc = 0;
  217. cpu->icount_extra = 0;
  218. atomic_set(&cpu->icount_decr_ptr->u32, 0);
  219. cpu->can_do_io = 1;
  220. cpu->exception_index = -1;
  221. cpu->crash_occurred = false;
  222. cpu->cflags_next_tb = -1;
  223. if (tcg_enabled()) {
  224. cpu_tb_jmp_cache_clear(cpu);
  225. tcg_flush_softmmu_tlb(cpu);
  226. }
  227. }
  228. static bool cpu_common_has_work(CPUState *cs)
  229. {
  230. return false;
  231. }
  232. ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
  233. {
  234. CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
  235. assert(cpu_model && cc->class_by_name);
  236. return cc->class_by_name(cpu_model);
  237. }
  238. static void cpu_common_parse_features(const char *typename, char *features,
  239. Error **errp)
  240. {
  241. char *val;
  242. static bool cpu_globals_initialized;
  243. /* Single "key=value" string being parsed */
  244. char *featurestr = features ? strtok(features, ",") : NULL;
  245. /* should be called only once, catch invalid users */
  246. assert(!cpu_globals_initialized);
  247. cpu_globals_initialized = true;
  248. while (featurestr) {
  249. val = strchr(featurestr, '=');
  250. if (val) {
  251. GlobalProperty *prop = g_new0(typeof(*prop), 1);
  252. *val = 0;
  253. val++;
  254. prop->driver = typename;
  255. prop->property = g_strdup(featurestr);
  256. prop->value = g_strdup(val);
  257. qdev_prop_register_global(prop);
  258. } else {
  259. error_setg(errp, "Expected key=value format, found %s.",
  260. featurestr);
  261. return;
  262. }
  263. featurestr = strtok(NULL, ",");
  264. }
  265. }
  266. static void cpu_common_realizefn(DeviceState *dev, Error **errp)
  267. {
  268. CPUState *cpu = CPU(dev);
  269. Object *machine = qdev_get_machine();
  270. /* qdev_get_machine() can return something that's not TYPE_MACHINE
  271. * if this is one of the user-only emulators; in that case there's
  272. * no need to check the ignore_memory_transaction_failures board flag.
  273. */
  274. if (object_dynamic_cast(machine, TYPE_MACHINE)) {
  275. ObjectClass *oc = object_get_class(machine);
  276. MachineClass *mc = MACHINE_CLASS(oc);
  277. if (mc) {
  278. cpu->ignore_memory_transaction_failures =
  279. mc->ignore_memory_transaction_failures;
  280. }
  281. }
  282. if (dev->hotplugged) {
  283. cpu_synchronize_post_init(cpu);
  284. cpu_resume(cpu);
  285. }
  286. /* NOTE: latest generic point where the cpu is fully realized */
  287. trace_init_vcpu(cpu);
  288. }
  289. static void cpu_common_unrealizefn(DeviceState *dev)
  290. {
  291. CPUState *cpu = CPU(dev);
  292. /* NOTE: latest generic point before the cpu is fully unrealized */
  293. trace_fini_vcpu(cpu);
  294. qemu_plugin_vcpu_exit_hook(cpu);
  295. cpu_exec_unrealizefn(cpu);
  296. }
  297. static void cpu_common_initfn(Object *obj)
  298. {
  299. CPUState *cpu = CPU(obj);
  300. CPUClass *cc = CPU_GET_CLASS(obj);
  301. cpu->cpu_index = UNASSIGNED_CPU_INDEX;
  302. cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX;
  303. cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
  304. /* *-user doesn't have configurable SMP topology */
  305. /* the default value is changed by qemu_init_vcpu() for softmmu */
  306. cpu->nr_cores = 1;
  307. cpu->nr_threads = 1;
  308. qemu_mutex_init(&cpu->work_mutex);
  309. QSIMPLEQ_INIT(&cpu->work_list);
  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->get_arch_id = cpu_common_get_arch_id;
  341. k->has_work = cpu_common_has_work;
  342. k->get_paging_enabled = cpu_common_get_paging_enabled;
  343. k->get_memory_mapping = cpu_common_get_memory_mapping;
  344. k->write_elf32_qemunote = cpu_common_write_elf32_qemunote;
  345. k->write_elf32_note = cpu_common_write_elf32_note;
  346. k->write_elf64_qemunote = cpu_common_write_elf64_qemunote;
  347. k->write_elf64_note = cpu_common_write_elf64_note;
  348. k->gdb_read_register = cpu_common_gdb_read_register;
  349. k->gdb_write_register = cpu_common_gdb_write_register;
  350. k->virtio_is_big_endian = cpu_common_virtio_is_big_endian;
  351. k->debug_excp_handler = cpu_common_noop;
  352. k->debug_check_watchpoint = cpu_common_debug_check_watchpoint;
  353. k->cpu_exec_enter = cpu_common_noop;
  354. k->cpu_exec_exit = cpu_common_noop;
  355. k->cpu_exec_interrupt = cpu_common_exec_interrupt;
  356. k->adjust_watchpoint_address = cpu_adjust_watchpoint_address;
  357. set_bit(DEVICE_CATEGORY_CPU, dc->categories);
  358. dc->realize = cpu_common_realizefn;
  359. dc->unrealize = cpu_common_unrealizefn;
  360. dc->reset = cpu_common_reset;
  361. device_class_set_props(dc, 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)