2
0

cpu-target.c 13 KB

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