2
0

cpu-target.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Target-specific parts of the CPU object
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qapi/error.h"
  21. #include "qemu/error-report.h"
  22. #include "qemu/qemu-print.h"
  23. #include "migration/vmstate.h"
  24. #ifndef CONFIG_USER_ONLY
  25. #include "hw/core/sysemu-cpu-ops.h"
  26. #endif
  27. #include "system/accel-ops.h"
  28. #include "system/cpus.h"
  29. #include "system/tcg.h"
  30. #include "exec/tswap.h"
  31. #include "exec/replay-core.h"
  32. #include "exec/cpu-common.h"
  33. #include "exec/cputlb.h"
  34. #include "exec/exec-all.h"
  35. #include "exec/tb-flush.h"
  36. #include "exec/log.h"
  37. #include "accel/accel-cpu-target.h"
  38. #include "trace/trace-root.h"
  39. #include "qemu/accel.h"
  40. #include "hw/core/cpu.h"
  41. #ifndef CONFIG_USER_ONLY
  42. static int cpu_common_post_load(void *opaque, int version_id)
  43. {
  44. if (tcg_enabled()) {
  45. CPUState *cpu = opaque;
  46. /*
  47. * 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
  48. * version_id is increased.
  49. */
  50. cpu->interrupt_request &= ~0x01;
  51. tlb_flush(cpu);
  52. /*
  53. * loadvm has just updated the content of RAM, bypassing the
  54. * usual mechanisms that ensure we flush TBs for writes to
  55. * memory we've translated code from. So we must flush all TBs,
  56. * which will now be stale.
  57. */
  58. tb_flush(cpu);
  59. }
  60. return 0;
  61. }
  62. static int cpu_common_pre_load(void *opaque)
  63. {
  64. CPUState *cpu = opaque;
  65. cpu->exception_index = -1;
  66. return 0;
  67. }
  68. static bool cpu_common_exception_index_needed(void *opaque)
  69. {
  70. CPUState *cpu = opaque;
  71. return tcg_enabled() && cpu->exception_index != -1;
  72. }
  73. static const VMStateDescription vmstate_cpu_common_exception_index = {
  74. .name = "cpu_common/exception_index",
  75. .version_id = 1,
  76. .minimum_version_id = 1,
  77. .needed = cpu_common_exception_index_needed,
  78. .fields = (const VMStateField[]) {
  79. VMSTATE_INT32(exception_index, CPUState),
  80. VMSTATE_END_OF_LIST()
  81. }
  82. };
  83. static bool cpu_common_crash_occurred_needed(void *opaque)
  84. {
  85. CPUState *cpu = opaque;
  86. return cpu->crash_occurred;
  87. }
  88. static const VMStateDescription vmstate_cpu_common_crash_occurred = {
  89. .name = "cpu_common/crash_occurred",
  90. .version_id = 1,
  91. .minimum_version_id = 1,
  92. .needed = cpu_common_crash_occurred_needed,
  93. .fields = (const VMStateField[]) {
  94. VMSTATE_BOOL(crash_occurred, CPUState),
  95. VMSTATE_END_OF_LIST()
  96. }
  97. };
  98. const VMStateDescription vmstate_cpu_common = {
  99. .name = "cpu_common",
  100. .version_id = 1,
  101. .minimum_version_id = 1,
  102. .pre_load = cpu_common_pre_load,
  103. .post_load = cpu_common_post_load,
  104. .fields = (const VMStateField[]) {
  105. VMSTATE_UINT32(halted, CPUState),
  106. VMSTATE_UINT32(interrupt_request, CPUState),
  107. VMSTATE_END_OF_LIST()
  108. },
  109. .subsections = (const VMStateDescription * const []) {
  110. &vmstate_cpu_common_exception_index,
  111. &vmstate_cpu_common_crash_occurred,
  112. NULL
  113. }
  114. };
  115. #endif
  116. bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
  117. {
  118. if (!accel_cpu_common_realize(cpu, errp)) {
  119. return false;
  120. }
  121. /* Wait until cpu initialization complete before exposing cpu. */
  122. cpu_list_add(cpu);
  123. #ifdef CONFIG_USER_ONLY
  124. assert(qdev_get_vmsd(DEVICE(cpu)) == NULL ||
  125. qdev_get_vmsd(DEVICE(cpu))->unmigratable);
  126. #else
  127. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  128. vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
  129. }
  130. if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
  131. vmstate_register(NULL, cpu->cpu_index, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
  132. }
  133. #endif /* CONFIG_USER_ONLY */
  134. return true;
  135. }
  136. void cpu_exec_unrealizefn(CPUState *cpu)
  137. {
  138. #ifndef CONFIG_USER_ONLY
  139. CPUClass *cc = CPU_GET_CLASS(cpu);
  140. if (cc->sysemu_ops->legacy_vmsd != NULL) {
  141. vmstate_unregister(NULL, cc->sysemu_ops->legacy_vmsd, cpu);
  142. }
  143. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  144. vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
  145. }
  146. #endif
  147. cpu_list_remove(cpu);
  148. /*
  149. * Now that the vCPU has been removed from the RCU list, we can call
  150. * accel_cpu_common_unrealize, which may free fields using call_rcu.
  151. */
  152. accel_cpu_common_unrealize(cpu);
  153. }
  154. char *cpu_model_from_type(const char *typename)
  155. {
  156. const char *suffix = "-" CPU_RESOLVING_TYPE;
  157. if (!object_class_by_name(typename)) {
  158. return NULL;
  159. }
  160. if (g_str_has_suffix(typename, suffix)) {
  161. return g_strndup(typename, strlen(typename) - strlen(suffix));
  162. }
  163. return g_strdup(typename);
  164. }
  165. const char *parse_cpu_option(const char *cpu_option)
  166. {
  167. ObjectClass *oc;
  168. CPUClass *cc;
  169. gchar **model_pieces;
  170. const char *cpu_type;
  171. model_pieces = g_strsplit(cpu_option, ",", 2);
  172. if (!model_pieces[0]) {
  173. error_report("-cpu option cannot be empty");
  174. exit(1);
  175. }
  176. oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
  177. if (oc == NULL) {
  178. error_report("unable to find CPU model '%s'", model_pieces[0]);
  179. g_strfreev(model_pieces);
  180. exit(EXIT_FAILURE);
  181. }
  182. cpu_type = object_class_get_name(oc);
  183. cc = CPU_CLASS(oc);
  184. cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
  185. g_strfreev(model_pieces);
  186. return cpu_type;
  187. }
  188. #ifndef cpu_list
  189. static void cpu_list_entry(gpointer data, gpointer user_data)
  190. {
  191. CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
  192. const char *typename = object_class_get_name(OBJECT_CLASS(data));
  193. g_autofree char *model = cpu_model_from_type(typename);
  194. if (cc->deprecation_note) {
  195. qemu_printf(" %s (deprecated)\n", model);
  196. } else {
  197. qemu_printf(" %s\n", model);
  198. }
  199. }
  200. static void cpu_list(void)
  201. {
  202. GSList *list;
  203. list = object_class_get_list_sorted(TYPE_CPU, false);
  204. qemu_printf("Available CPUs:\n");
  205. g_slist_foreach(list, cpu_list_entry, NULL);
  206. g_slist_free(list);
  207. }
  208. #endif
  209. void list_cpus(void)
  210. {
  211. cpu_list();
  212. }
  213. /* enable or disable single step mode. EXCP_DEBUG is returned by the
  214. CPU loop after each instruction */
  215. void cpu_single_step(CPUState *cpu, int enabled)
  216. {
  217. if (cpu->singlestep_enabled != enabled) {
  218. cpu->singlestep_enabled = enabled;
  219. #if !defined(CONFIG_USER_ONLY)
  220. const AccelOpsClass *ops = cpus_get_accel();
  221. if (ops->update_guest_debug) {
  222. ops->update_guest_debug(cpu);
  223. }
  224. #endif
  225. trace_breakpoint_singlestep(cpu->cpu_index, enabled);
  226. }
  227. }
  228. void cpu_abort(CPUState *cpu, const char *fmt, ...)
  229. {
  230. va_list ap;
  231. va_list ap2;
  232. va_start(ap, fmt);
  233. va_copy(ap2, ap);
  234. fprintf(stderr, "qemu: fatal: ");
  235. vfprintf(stderr, fmt, ap);
  236. fprintf(stderr, "\n");
  237. cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  238. if (qemu_log_separate()) {
  239. FILE *logfile = qemu_log_trylock();
  240. if (logfile) {
  241. fprintf(logfile, "qemu: fatal: ");
  242. vfprintf(logfile, fmt, ap2);
  243. fprintf(logfile, "\n");
  244. cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  245. qemu_log_unlock(logfile);
  246. }
  247. }
  248. va_end(ap2);
  249. va_end(ap);
  250. replay_finish();
  251. #if defined(CONFIG_USER_ONLY)
  252. {
  253. struct sigaction act;
  254. sigfillset(&act.sa_mask);
  255. act.sa_handler = SIG_DFL;
  256. act.sa_flags = 0;
  257. sigaction(SIGABRT, &act, NULL);
  258. }
  259. #endif
  260. abort();
  261. }
  262. bool target_words_bigendian(void)
  263. {
  264. return TARGET_BIG_ENDIAN;
  265. }
  266. const char *target_name(void)
  267. {
  268. return TARGET_NAME;
  269. }