cpu-system.c 8.1 KB

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