2
0

cpu-system.c 8.3 KB

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