cpu-system.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * QEMU CPU model (system specific)
  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 "exec/address-spaces.h"
  23. #include "exec/cputlb.h"
  24. #include "exec/memory.h"
  25. #include "exec/tb-flush.h"
  26. #include "exec/tswap.h"
  27. #include "hw/qdev-core.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/core/sysemu-cpu-ops.h"
  30. #include "migration/vmstate.h"
  31. #include "system/tcg.h"
  32. bool cpu_paging_enabled(const CPUState *cpu)
  33. {
  34. CPUClass *cc = CPU_GET_CLASS(cpu);
  35. if (cc->sysemu_ops->get_paging_enabled) {
  36. return cc->sysemu_ops->get_paging_enabled(cpu);
  37. }
  38. return false;
  39. }
  40. bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
  41. Error **errp)
  42. {
  43. CPUClass *cc = CPU_GET_CLASS(cpu);
  44. if (cc->sysemu_ops->get_memory_mapping) {
  45. return cc->sysemu_ops->get_memory_mapping(cpu, list, errp);
  46. }
  47. error_setg(errp, "Obtaining memory mappings is unsupported on this CPU.");
  48. return false;
  49. }
  50. hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
  51. MemTxAttrs *attrs)
  52. {
  53. CPUClass *cc = CPU_GET_CLASS(cpu);
  54. hwaddr paddr;
  55. if (cc->sysemu_ops->get_phys_page_attrs_debug) {
  56. paddr = cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr, attrs);
  57. } else {
  58. /* Fallback for CPUs which don't implement the _attrs_ hook */
  59. *attrs = MEMTXATTRS_UNSPECIFIED;
  60. paddr = cc->sysemu_ops->get_phys_page_debug(cpu, addr);
  61. }
  62. /* Indicate that this is a debug access. */
  63. attrs->debug = 1;
  64. return paddr;
  65. }
  66. hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
  67. {
  68. MemTxAttrs attrs = {};
  69. return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
  70. }
  71. int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
  72. {
  73. int ret = 0;
  74. if (cpu->cc->sysemu_ops->asidx_from_attrs) {
  75. ret = cpu->cc->sysemu_ops->asidx_from_attrs(cpu, attrs);
  76. assert(ret < cpu->num_ases && ret >= 0);
  77. }
  78. return ret;
  79. }
  80. int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  81. void *opaque)
  82. {
  83. CPUClass *cc = CPU_GET_CLASS(cpu);
  84. if (!cc->sysemu_ops->write_elf32_qemunote) {
  85. return 0;
  86. }
  87. return (*cc->sysemu_ops->write_elf32_qemunote)(f, cpu, opaque);
  88. }
  89. int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
  90. int cpuid, void *opaque)
  91. {
  92. CPUClass *cc = CPU_GET_CLASS(cpu);
  93. if (!cc->sysemu_ops->write_elf32_note) {
  94. return -1;
  95. }
  96. return (*cc->sysemu_ops->write_elf32_note)(f, cpu, cpuid, opaque);
  97. }
  98. int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  99. void *opaque)
  100. {
  101. CPUClass *cc = CPU_GET_CLASS(cpu);
  102. if (!cc->sysemu_ops->write_elf64_qemunote) {
  103. return 0;
  104. }
  105. return (*cc->sysemu_ops->write_elf64_qemunote)(f, cpu, opaque);
  106. }
  107. int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
  108. int cpuid, void *opaque)
  109. {
  110. CPUClass *cc = CPU_GET_CLASS(cpu);
  111. if (!cc->sysemu_ops->write_elf64_note) {
  112. return -1;
  113. }
  114. return (*cc->sysemu_ops->write_elf64_note)(f, cpu, cpuid, opaque);
  115. }
  116. bool cpu_virtio_is_big_endian(CPUState *cpu)
  117. {
  118. CPUClass *cc = CPU_GET_CLASS(cpu);
  119. if (cc->sysemu_ops->virtio_is_big_endian) {
  120. return cc->sysemu_ops->virtio_is_big_endian(cpu);
  121. }
  122. return target_words_bigendian();
  123. }
  124. GuestPanicInformation *cpu_get_crash_info(CPUState *cpu)
  125. {
  126. CPUClass *cc = CPU_GET_CLASS(cpu);
  127. GuestPanicInformation *res = NULL;
  128. if (cc->sysemu_ops->get_crash_info) {
  129. res = cc->sysemu_ops->get_crash_info(cpu);
  130. }
  131. return res;
  132. }
  133. static const Property cpu_system_props[] = {
  134. /*
  135. * Create a memory property for system CPU object, so users can
  136. * wire up its memory. The default if no link is set up is to use
  137. * the system address space.
  138. */
  139. DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
  140. MemoryRegion *),
  141. };
  142. static bool cpu_get_start_powered_off(Object *obj, Error **errp)
  143. {
  144. CPUState *cpu = CPU(obj);
  145. return cpu->start_powered_off;
  146. }
  147. static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
  148. {
  149. CPUState *cpu = CPU(obj);
  150. cpu->start_powered_off = value;
  151. }
  152. void cpu_class_init_props(DeviceClass *dc)
  153. {
  154. ObjectClass *oc = OBJECT_CLASS(dc);
  155. /*
  156. * We can't use DEFINE_PROP_BOOL in the Property array for this
  157. * property, because we want this to be settable after realize.
  158. */
  159. object_class_property_add_bool(oc, "start-powered-off",
  160. cpu_get_start_powered_off,
  161. cpu_set_start_powered_off);
  162. device_class_set_props(dc, cpu_system_props);
  163. }
  164. void cpu_exec_initfn(CPUState *cpu)
  165. {
  166. cpu->memory = get_system_memory();
  167. object_ref(OBJECT(cpu->memory));
  168. }
  169. static int cpu_common_post_load(void *opaque, int version_id)
  170. {
  171. if (tcg_enabled()) {
  172. CPUState *cpu = opaque;
  173. /*
  174. * 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
  175. * version_id is increased.
  176. */
  177. cpu->interrupt_request &= ~0x01;
  178. tlb_flush(cpu);
  179. /*
  180. * loadvm has just updated the content of RAM, bypassing the
  181. * usual mechanisms that ensure we flush TBs for writes to
  182. * memory we've translated code from. So we must flush all TBs,
  183. * which will now be stale.
  184. */
  185. tb_flush(cpu);
  186. }
  187. return 0;
  188. }
  189. static int cpu_common_pre_load(void *opaque)
  190. {
  191. CPUState *cpu = opaque;
  192. cpu->exception_index = -1;
  193. return 0;
  194. }
  195. static bool cpu_common_exception_index_needed(void *opaque)
  196. {
  197. CPUState *cpu = opaque;
  198. return tcg_enabled() && cpu->exception_index != -1;
  199. }
  200. static const VMStateDescription vmstate_cpu_common_exception_index = {
  201. .name = "cpu_common/exception_index",
  202. .version_id = 1,
  203. .minimum_version_id = 1,
  204. .needed = cpu_common_exception_index_needed,
  205. .fields = (const VMStateField[]) {
  206. VMSTATE_INT32(exception_index, CPUState),
  207. VMSTATE_END_OF_LIST()
  208. }
  209. };
  210. static bool cpu_common_crash_occurred_needed(void *opaque)
  211. {
  212. CPUState *cpu = opaque;
  213. return cpu->crash_occurred;
  214. }
  215. static const VMStateDescription vmstate_cpu_common_crash_occurred = {
  216. .name = "cpu_common/crash_occurred",
  217. .version_id = 1,
  218. .minimum_version_id = 1,
  219. .needed = cpu_common_crash_occurred_needed,
  220. .fields = (const VMStateField[]) {
  221. VMSTATE_BOOL(crash_occurred, CPUState),
  222. VMSTATE_END_OF_LIST()
  223. }
  224. };
  225. const VMStateDescription vmstate_cpu_common = {
  226. .name = "cpu_common",
  227. .version_id = 1,
  228. .minimum_version_id = 1,
  229. .pre_load = cpu_common_pre_load,
  230. .post_load = cpu_common_post_load,
  231. .fields = (const VMStateField[]) {
  232. VMSTATE_UINT32(halted, CPUState),
  233. VMSTATE_UINT32(interrupt_request, CPUState),
  234. VMSTATE_END_OF_LIST()
  235. },
  236. .subsections = (const VMStateDescription * const []) {
  237. &vmstate_cpu_common_exception_index,
  238. &vmstate_cpu_common_crash_occurred,
  239. NULL
  240. }
  241. };
  242. void cpu_vmstate_register(CPUState *cpu)
  243. {
  244. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  245. vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
  246. }
  247. if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
  248. vmstate_register(NULL, cpu->cpu_index,
  249. cpu->cc->sysemu_ops->legacy_vmsd, cpu);
  250. }
  251. }
  252. void cpu_vmstate_unregister(CPUState *cpu)
  253. {
  254. CPUClass *cc = CPU_GET_CLASS(cpu);
  255. if (cc->sysemu_ops->legacy_vmsd != NULL) {
  256. vmstate_unregister(NULL, cc->sysemu_ops->legacy_vmsd, cpu);
  257. }
  258. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  259. vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
  260. }
  261. }