2
0

cpu-target.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 "exec/target_page.h"
  22. #include "exec/page-protection.h"
  23. #include "hw/qdev-core.h"
  24. #include "hw/qdev-properties.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/qemu-print.h"
  27. #include "migration/vmstate.h"
  28. #ifdef CONFIG_USER_ONLY
  29. #include "qemu.h"
  30. #include "user/page-protection.h"
  31. #else
  32. #include "hw/core/sysemu-cpu-ops.h"
  33. #include "exec/address-spaces.h"
  34. #include "exec/memory.h"
  35. #endif
  36. #include "system/cpus.h"
  37. #include "system/tcg.h"
  38. #include "exec/tswap.h"
  39. #include "exec/replay-core.h"
  40. #include "exec/cpu-common.h"
  41. #include "exec/exec-all.h"
  42. #include "exec/tb-flush.h"
  43. #include "exec/translation-block.h"
  44. #include "exec/log.h"
  45. #include "accel/accel-cpu-target.h"
  46. #include "trace/trace-root.h"
  47. #include "qemu/accel.h"
  48. #ifndef CONFIG_USER_ONLY
  49. static int cpu_common_post_load(void *opaque, int version_id)
  50. {
  51. CPUState *cpu = opaque;
  52. /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
  53. version_id is increased. */
  54. cpu->interrupt_request &= ~0x01;
  55. tlb_flush(cpu);
  56. /* loadvm has just updated the content of RAM, bypassing the
  57. * usual mechanisms that ensure we flush TBs for writes to
  58. * memory we've translated code from. So we must flush all TBs,
  59. * which will now be stale.
  60. */
  61. tb_flush(cpu);
  62. return 0;
  63. }
  64. static int cpu_common_pre_load(void *opaque)
  65. {
  66. CPUState *cpu = opaque;
  67. cpu->exception_index = -1;
  68. return 0;
  69. }
  70. static bool cpu_common_exception_index_needed(void *opaque)
  71. {
  72. CPUState *cpu = opaque;
  73. return tcg_enabled() && cpu->exception_index != -1;
  74. }
  75. static const VMStateDescription vmstate_cpu_common_exception_index = {
  76. .name = "cpu_common/exception_index",
  77. .version_id = 1,
  78. .minimum_version_id = 1,
  79. .needed = cpu_common_exception_index_needed,
  80. .fields = (const VMStateField[]) {
  81. VMSTATE_INT32(exception_index, CPUState),
  82. VMSTATE_END_OF_LIST()
  83. }
  84. };
  85. static bool cpu_common_crash_occurred_needed(void *opaque)
  86. {
  87. CPUState *cpu = opaque;
  88. return cpu->crash_occurred;
  89. }
  90. static const VMStateDescription vmstate_cpu_common_crash_occurred = {
  91. .name = "cpu_common/crash_occurred",
  92. .version_id = 1,
  93. .minimum_version_id = 1,
  94. .needed = cpu_common_crash_occurred_needed,
  95. .fields = (const VMStateField[]) {
  96. VMSTATE_BOOL(crash_occurred, CPUState),
  97. VMSTATE_END_OF_LIST()
  98. }
  99. };
  100. const VMStateDescription vmstate_cpu_common = {
  101. .name = "cpu_common",
  102. .version_id = 1,
  103. .minimum_version_id = 1,
  104. .pre_load = cpu_common_pre_load,
  105. .post_load = cpu_common_post_load,
  106. .fields = (const VMStateField[]) {
  107. VMSTATE_UINT32(halted, CPUState),
  108. VMSTATE_UINT32(interrupt_request, CPUState),
  109. VMSTATE_END_OF_LIST()
  110. },
  111. .subsections = (const VMStateDescription * const []) {
  112. &vmstate_cpu_common_exception_index,
  113. &vmstate_cpu_common_crash_occurred,
  114. NULL
  115. }
  116. };
  117. #endif
  118. bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
  119. {
  120. if (!accel_cpu_common_realize(cpu, errp)) {
  121. return false;
  122. }
  123. /* Wait until cpu initialization complete before exposing cpu. */
  124. cpu_list_add(cpu);
  125. #ifdef CONFIG_USER_ONLY
  126. assert(qdev_get_vmsd(DEVICE(cpu)) == NULL ||
  127. qdev_get_vmsd(DEVICE(cpu))->unmigratable);
  128. #else
  129. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  130. vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
  131. }
  132. if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
  133. vmstate_register(NULL, cpu->cpu_index, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
  134. }
  135. #endif /* CONFIG_USER_ONLY */
  136. return true;
  137. }
  138. void cpu_exec_unrealizefn(CPUState *cpu)
  139. {
  140. #ifndef CONFIG_USER_ONLY
  141. CPUClass *cc = CPU_GET_CLASS(cpu);
  142. if (cc->sysemu_ops->legacy_vmsd != NULL) {
  143. vmstate_unregister(NULL, cc->sysemu_ops->legacy_vmsd, cpu);
  144. }
  145. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  146. vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
  147. }
  148. #endif
  149. cpu_list_remove(cpu);
  150. /*
  151. * Now that the vCPU has been removed from the RCU list, we can call
  152. * accel_cpu_common_unrealize, which may free fields using call_rcu.
  153. */
  154. accel_cpu_common_unrealize(cpu);
  155. }
  156. /*
  157. * This can't go in hw/core/cpu.c because that file is compiled only
  158. * once for both user-mode and system builds.
  159. */
  160. static const Property cpu_common_props[] = {
  161. #ifdef CONFIG_USER_ONLY
  162. /*
  163. * Create a property for the user-only object, so users can
  164. * adjust prctl(PR_SET_UNALIGN) from the command-line.
  165. * Has no effect if the target does not support the feature.
  166. */
  167. DEFINE_PROP_BOOL("prctl-unalign-sigbus", CPUState,
  168. prctl_unalign_sigbus, false),
  169. #else
  170. /*
  171. * Create a memory property for system CPU object, so users can
  172. * wire up its memory. The default if no link is set up is to use
  173. * the system address space.
  174. */
  175. DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
  176. MemoryRegion *),
  177. #endif
  178. };
  179. #ifndef CONFIG_USER_ONLY
  180. static bool cpu_get_start_powered_off(Object *obj, Error **errp)
  181. {
  182. CPUState *cpu = CPU(obj);
  183. return cpu->start_powered_off;
  184. }
  185. static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
  186. {
  187. CPUState *cpu = CPU(obj);
  188. cpu->start_powered_off = value;
  189. }
  190. #endif
  191. void cpu_class_init_props(DeviceClass *dc)
  192. {
  193. #ifndef CONFIG_USER_ONLY
  194. ObjectClass *oc = OBJECT_CLASS(dc);
  195. /*
  196. * We can't use DEFINE_PROP_BOOL in the Property array for this
  197. * property, because we want this to be settable after realize.
  198. */
  199. object_class_property_add_bool(oc, "start-powered-off",
  200. cpu_get_start_powered_off,
  201. cpu_set_start_powered_off);
  202. #endif
  203. device_class_set_props(dc, cpu_common_props);
  204. }
  205. void cpu_exec_initfn(CPUState *cpu)
  206. {
  207. #ifndef CONFIG_USER_ONLY
  208. cpu->memory = get_system_memory();
  209. object_ref(OBJECT(cpu->memory));
  210. #endif
  211. }
  212. char *cpu_model_from_type(const char *typename)
  213. {
  214. const char *suffix = "-" CPU_RESOLVING_TYPE;
  215. if (!object_class_by_name(typename)) {
  216. return NULL;
  217. }
  218. if (g_str_has_suffix(typename, suffix)) {
  219. return g_strndup(typename, strlen(typename) - strlen(suffix));
  220. }
  221. return g_strdup(typename);
  222. }
  223. const char *parse_cpu_option(const char *cpu_option)
  224. {
  225. ObjectClass *oc;
  226. CPUClass *cc;
  227. gchar **model_pieces;
  228. const char *cpu_type;
  229. model_pieces = g_strsplit(cpu_option, ",", 2);
  230. if (!model_pieces[0]) {
  231. error_report("-cpu option cannot be empty");
  232. exit(1);
  233. }
  234. oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
  235. if (oc == NULL) {
  236. error_report("unable to find CPU model '%s'", model_pieces[0]);
  237. g_strfreev(model_pieces);
  238. exit(EXIT_FAILURE);
  239. }
  240. cpu_type = object_class_get_name(oc);
  241. cc = CPU_CLASS(oc);
  242. cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
  243. g_strfreev(model_pieces);
  244. return cpu_type;
  245. }
  246. #ifndef cpu_list
  247. static void cpu_list_entry(gpointer data, gpointer user_data)
  248. {
  249. CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
  250. const char *typename = object_class_get_name(OBJECT_CLASS(data));
  251. g_autofree char *model = cpu_model_from_type(typename);
  252. if (cc->deprecation_note) {
  253. qemu_printf(" %s (deprecated)\n", model);
  254. } else {
  255. qemu_printf(" %s\n", model);
  256. }
  257. }
  258. static void cpu_list(void)
  259. {
  260. GSList *list;
  261. list = object_class_get_list_sorted(TYPE_CPU, false);
  262. qemu_printf("Available CPUs:\n");
  263. g_slist_foreach(list, cpu_list_entry, NULL);
  264. g_slist_free(list);
  265. }
  266. #endif
  267. void list_cpus(void)
  268. {
  269. cpu_list();
  270. }
  271. /* enable or disable single step mode. EXCP_DEBUG is returned by the
  272. CPU loop after each instruction */
  273. void cpu_single_step(CPUState *cpu, int enabled)
  274. {
  275. if (cpu->singlestep_enabled != enabled) {
  276. cpu->singlestep_enabled = enabled;
  277. #if !defined(CONFIG_USER_ONLY)
  278. const AccelOpsClass *ops = cpus_get_accel();
  279. if (ops->update_guest_debug) {
  280. ops->update_guest_debug(cpu);
  281. }
  282. #endif
  283. trace_breakpoint_singlestep(cpu->cpu_index, enabled);
  284. }
  285. }
  286. void cpu_abort(CPUState *cpu, const char *fmt, ...)
  287. {
  288. va_list ap;
  289. va_list ap2;
  290. va_start(ap, fmt);
  291. va_copy(ap2, ap);
  292. fprintf(stderr, "qemu: fatal: ");
  293. vfprintf(stderr, fmt, ap);
  294. fprintf(stderr, "\n");
  295. cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  296. if (qemu_log_separate()) {
  297. FILE *logfile = qemu_log_trylock();
  298. if (logfile) {
  299. fprintf(logfile, "qemu: fatal: ");
  300. vfprintf(logfile, fmt, ap2);
  301. fprintf(logfile, "\n");
  302. cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  303. qemu_log_unlock(logfile);
  304. }
  305. }
  306. va_end(ap2);
  307. va_end(ap);
  308. replay_finish();
  309. #if defined(CONFIG_USER_ONLY)
  310. {
  311. struct sigaction act;
  312. sigfillset(&act.sa_mask);
  313. act.sa_handler = SIG_DFL;
  314. act.sa_flags = 0;
  315. sigaction(SIGABRT, &act, NULL);
  316. }
  317. #endif
  318. abort();
  319. }
  320. /* physical memory access (slow version, mainly for debug) */
  321. #if defined(CONFIG_USER_ONLY)
  322. int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
  323. void *ptr, size_t len, bool is_write)
  324. {
  325. int flags;
  326. vaddr l, page;
  327. void * p;
  328. uint8_t *buf = ptr;
  329. ssize_t written;
  330. int ret = -1;
  331. int fd = -1;
  332. while (len > 0) {
  333. page = addr & TARGET_PAGE_MASK;
  334. l = (page + TARGET_PAGE_SIZE) - addr;
  335. if (l > len)
  336. l = len;
  337. flags = page_get_flags(page);
  338. if (!(flags & PAGE_VALID)) {
  339. goto out_close;
  340. }
  341. if (is_write) {
  342. if (flags & PAGE_WRITE) {
  343. /* XXX: this code should not depend on lock_user */
  344. p = lock_user(VERIFY_WRITE, addr, l, 0);
  345. if (!p) {
  346. goto out_close;
  347. }
  348. memcpy(p, buf, l);
  349. unlock_user(p, addr, l);
  350. } else {
  351. /* Bypass the host page protection using ptrace. */
  352. if (fd == -1) {
  353. fd = open("/proc/self/mem", O_WRONLY);
  354. if (fd == -1) {
  355. goto out;
  356. }
  357. }
  358. /*
  359. * If there is a TranslationBlock and we weren't bypassing the
  360. * host page protection, the memcpy() above would SEGV,
  361. * ultimately leading to page_unprotect(). So invalidate the
  362. * translations manually. Both invalidation and pwrite() must
  363. * be under mmap_lock() in order to prevent the creation of
  364. * another TranslationBlock in between.
  365. */
  366. mmap_lock();
  367. tb_invalidate_phys_range(addr, addr + l - 1);
  368. written = pwrite(fd, buf, l,
  369. (off_t)(uintptr_t)g2h_untagged(addr));
  370. mmap_unlock();
  371. if (written != l) {
  372. goto out_close;
  373. }
  374. }
  375. } else if (flags & PAGE_READ) {
  376. /* XXX: this code should not depend on lock_user */
  377. p = lock_user(VERIFY_READ, addr, l, 1);
  378. if (!p) {
  379. goto out_close;
  380. }
  381. memcpy(buf, p, l);
  382. unlock_user(p, addr, 0);
  383. } else {
  384. /* Bypass the host page protection using ptrace. */
  385. if (fd == -1) {
  386. fd = open("/proc/self/mem", O_RDONLY);
  387. if (fd == -1) {
  388. goto out;
  389. }
  390. }
  391. if (pread(fd, buf, l,
  392. (off_t)(uintptr_t)g2h_untagged(addr)) != l) {
  393. goto out_close;
  394. }
  395. }
  396. len -= l;
  397. buf += l;
  398. addr += l;
  399. }
  400. ret = 0;
  401. out_close:
  402. if (fd != -1) {
  403. close(fd);
  404. }
  405. out:
  406. return ret;
  407. }
  408. #endif
  409. bool target_words_bigendian(void)
  410. {
  411. return TARGET_BIG_ENDIAN;
  412. }
  413. const char *target_name(void)
  414. {
  415. return TARGET_NAME;
  416. }