cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 "hw/qdev-core.h"
  23. #include "hw/qdev-properties.h"
  24. #include "qemu/error-report.h"
  25. #include "migration/vmstate.h"
  26. #ifdef CONFIG_USER_ONLY
  27. #include "qemu.h"
  28. #else
  29. #include "hw/core/sysemu-cpu-ops.h"
  30. #include "exec/address-spaces.h"
  31. #endif
  32. #include "sysemu/cpus.h"
  33. #include "sysemu/tcg.h"
  34. #include "exec/replay-core.h"
  35. #include "exec/cpu-common.h"
  36. #include "exec/exec-all.h"
  37. #include "exec/tb-flush.h"
  38. #include "exec/translate-all.h"
  39. #include "exec/log.h"
  40. #include "hw/core/accel-cpu.h"
  41. #include "trace/trace-root.h"
  42. #include "qemu/accel.h"
  43. #include "qemu/plugin.h"
  44. uintptr_t qemu_host_page_size;
  45. intptr_t qemu_host_page_mask;
  46. #ifndef CONFIG_USER_ONLY
  47. static int cpu_common_post_load(void *opaque, int version_id)
  48. {
  49. CPUState *cpu = opaque;
  50. /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
  51. version_id is increased. */
  52. cpu->interrupt_request &= ~0x01;
  53. tlb_flush(cpu);
  54. /* loadvm has just updated the content of RAM, bypassing the
  55. * usual mechanisms that ensure we flush TBs for writes to
  56. * memory we've translated code from. So we must flush all TBs,
  57. * which will now be stale.
  58. */
  59. tb_flush(cpu);
  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 = (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 = (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 = (VMStateField[]) {
  105. VMSTATE_UINT32(halted, CPUState),
  106. VMSTATE_UINT32(interrupt_request, CPUState),
  107. VMSTATE_END_OF_LIST()
  108. },
  109. .subsections = (const VMStateDescription*[]) {
  110. &vmstate_cpu_common_exception_index,
  111. &vmstate_cpu_common_crash_occurred,
  112. NULL
  113. }
  114. };
  115. #endif
  116. void cpu_exec_realizefn(CPUState *cpu, Error **errp)
  117. {
  118. /* cache the cpu class for the hotpath */
  119. cpu->cc = CPU_GET_CLASS(cpu);
  120. if (!accel_cpu_realizefn(cpu, errp)) {
  121. return;
  122. }
  123. /* NB: errp parameter is unused currently */
  124. if (tcg_enabled()) {
  125. tcg_exec_realizefn(cpu, errp);
  126. }
  127. /* Wait until cpu initialization complete before exposing cpu. */
  128. cpu_list_add(cpu);
  129. /* Plugin initialization must wait until cpu_index assigned. */
  130. if (tcg_enabled()) {
  131. qemu_plugin_vcpu_init_hook(cpu);
  132. }
  133. #ifdef CONFIG_USER_ONLY
  134. assert(qdev_get_vmsd(DEVICE(cpu)) == NULL ||
  135. qdev_get_vmsd(DEVICE(cpu))->unmigratable);
  136. #else
  137. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  138. vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
  139. }
  140. if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
  141. vmstate_register(NULL, cpu->cpu_index, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
  142. }
  143. #endif /* CONFIG_USER_ONLY */
  144. }
  145. void cpu_exec_unrealizefn(CPUState *cpu)
  146. {
  147. #ifndef CONFIG_USER_ONLY
  148. CPUClass *cc = CPU_GET_CLASS(cpu);
  149. if (cc->sysemu_ops->legacy_vmsd != NULL) {
  150. vmstate_unregister(NULL, cc->sysemu_ops->legacy_vmsd, cpu);
  151. }
  152. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  153. vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
  154. }
  155. #endif
  156. /* Call the plugin hook before clearing cpu->cpu_index in cpu_list_remove */
  157. if (tcg_enabled()) {
  158. qemu_plugin_vcpu_exit_hook(cpu);
  159. }
  160. cpu_list_remove(cpu);
  161. /*
  162. * Now that the vCPU has been removed from the RCU list, we can call
  163. * tcg_exec_unrealizefn, which may free fields using call_rcu.
  164. */
  165. if (tcg_enabled()) {
  166. tcg_exec_unrealizefn(cpu);
  167. }
  168. }
  169. /*
  170. * This can't go in hw/core/cpu.c because that file is compiled only
  171. * once for both user-mode and system builds.
  172. */
  173. static Property cpu_common_props[] = {
  174. #ifdef CONFIG_USER_ONLY
  175. /*
  176. * Create a property for the user-only object, so users can
  177. * adjust prctl(PR_SET_UNALIGN) from the command-line.
  178. * Has no effect if the target does not support the feature.
  179. */
  180. DEFINE_PROP_BOOL("prctl-unalign-sigbus", CPUState,
  181. prctl_unalign_sigbus, false),
  182. #else
  183. /*
  184. * Create a memory property for softmmu CPU object, so users can
  185. * wire up its memory. The default if no link is set up is to use
  186. * the system address space.
  187. */
  188. DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
  189. MemoryRegion *),
  190. #endif
  191. DEFINE_PROP_END_OF_LIST(),
  192. };
  193. static bool cpu_get_start_powered_off(Object *obj, Error **errp)
  194. {
  195. CPUState *cpu = CPU(obj);
  196. return cpu->start_powered_off;
  197. }
  198. static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
  199. {
  200. CPUState *cpu = CPU(obj);
  201. cpu->start_powered_off = value;
  202. }
  203. void cpu_class_init_props(DeviceClass *dc)
  204. {
  205. ObjectClass *oc = OBJECT_CLASS(dc);
  206. device_class_set_props(dc, cpu_common_props);
  207. /*
  208. * We can't use DEFINE_PROP_BOOL in the Property array for this
  209. * property, because we want this to be settable after realize.
  210. */
  211. object_class_property_add_bool(oc, "start-powered-off",
  212. cpu_get_start_powered_off,
  213. cpu_set_start_powered_off);
  214. }
  215. void cpu_exec_initfn(CPUState *cpu)
  216. {
  217. cpu->as = NULL;
  218. cpu->num_ases = 0;
  219. #ifndef CONFIG_USER_ONLY
  220. cpu->thread_id = qemu_get_thread_id();
  221. cpu->memory = get_system_memory();
  222. object_ref(OBJECT(cpu->memory));
  223. #endif
  224. }
  225. const char *parse_cpu_option(const char *cpu_option)
  226. {
  227. ObjectClass *oc;
  228. CPUClass *cc;
  229. gchar **model_pieces;
  230. const char *cpu_type;
  231. model_pieces = g_strsplit(cpu_option, ",", 2);
  232. if (!model_pieces[0]) {
  233. error_report("-cpu option cannot be empty");
  234. exit(1);
  235. }
  236. oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
  237. if (oc == NULL) {
  238. error_report("unable to find CPU model '%s'", model_pieces[0]);
  239. g_strfreev(model_pieces);
  240. exit(EXIT_FAILURE);
  241. }
  242. cpu_type = object_class_get_name(oc);
  243. cc = CPU_CLASS(oc);
  244. cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
  245. g_strfreev(model_pieces);
  246. return cpu_type;
  247. }
  248. void list_cpus(void)
  249. {
  250. /* XXX: implement xxx_cpu_list for targets that still miss it */
  251. #if defined(cpu_list)
  252. cpu_list();
  253. #endif
  254. }
  255. #if defined(CONFIG_USER_ONLY)
  256. void tb_invalidate_phys_addr(hwaddr addr)
  257. {
  258. mmap_lock();
  259. tb_invalidate_phys_page(addr);
  260. mmap_unlock();
  261. }
  262. #else
  263. void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr, MemTxAttrs attrs)
  264. {
  265. ram_addr_t ram_addr;
  266. MemoryRegion *mr;
  267. hwaddr l = 1;
  268. if (!tcg_enabled()) {
  269. return;
  270. }
  271. RCU_READ_LOCK_GUARD();
  272. mr = address_space_translate(as, addr, &addr, &l, false, attrs);
  273. if (!(memory_region_is_ram(mr)
  274. || memory_region_is_romd(mr))) {
  275. return;
  276. }
  277. ram_addr = memory_region_get_ram_addr(mr) + addr;
  278. tb_invalidate_phys_page(ram_addr);
  279. }
  280. #endif
  281. /* enable or disable single step mode. EXCP_DEBUG is returned by the
  282. CPU loop after each instruction */
  283. void cpu_single_step(CPUState *cpu, int enabled)
  284. {
  285. if (cpu->singlestep_enabled != enabled) {
  286. cpu->singlestep_enabled = enabled;
  287. #if !defined(CONFIG_USER_ONLY)
  288. const AccelOpsClass *ops = cpus_get_accel();
  289. if (ops->update_guest_debug) {
  290. ops->update_guest_debug(cpu);
  291. }
  292. #endif
  293. trace_breakpoint_singlestep(cpu->cpu_index, enabled);
  294. }
  295. }
  296. void cpu_abort(CPUState *cpu, const char *fmt, ...)
  297. {
  298. va_list ap;
  299. va_list ap2;
  300. va_start(ap, fmt);
  301. va_copy(ap2, ap);
  302. fprintf(stderr, "qemu: fatal: ");
  303. vfprintf(stderr, fmt, ap);
  304. fprintf(stderr, "\n");
  305. cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  306. if (qemu_log_separate()) {
  307. FILE *logfile = qemu_log_trylock();
  308. if (logfile) {
  309. fprintf(logfile, "qemu: fatal: ");
  310. vfprintf(logfile, fmt, ap2);
  311. fprintf(logfile, "\n");
  312. cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  313. qemu_log_unlock(logfile);
  314. }
  315. }
  316. va_end(ap2);
  317. va_end(ap);
  318. replay_finish();
  319. #if defined(CONFIG_USER_ONLY)
  320. {
  321. struct sigaction act;
  322. sigfillset(&act.sa_mask);
  323. act.sa_handler = SIG_DFL;
  324. act.sa_flags = 0;
  325. sigaction(SIGABRT, &act, NULL);
  326. }
  327. #endif
  328. abort();
  329. }
  330. /* physical memory access (slow version, mainly for debug) */
  331. #if defined(CONFIG_USER_ONLY)
  332. int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
  333. void *ptr, size_t len, bool is_write)
  334. {
  335. int flags;
  336. vaddr l, page;
  337. void * p;
  338. uint8_t *buf = ptr;
  339. while (len > 0) {
  340. page = addr & TARGET_PAGE_MASK;
  341. l = (page + TARGET_PAGE_SIZE) - addr;
  342. if (l > len)
  343. l = len;
  344. flags = page_get_flags(page);
  345. if (!(flags & PAGE_VALID))
  346. return -1;
  347. if (is_write) {
  348. if (!(flags & PAGE_WRITE))
  349. return -1;
  350. /* XXX: this code should not depend on lock_user */
  351. if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
  352. return -1;
  353. memcpy(p, buf, l);
  354. unlock_user(p, addr, l);
  355. } else {
  356. if (!(flags & PAGE_READ))
  357. return -1;
  358. /* XXX: this code should not depend on lock_user */
  359. if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
  360. return -1;
  361. memcpy(buf, p, l);
  362. unlock_user(p, addr, 0);
  363. }
  364. len -= l;
  365. buf += l;
  366. addr += l;
  367. }
  368. return 0;
  369. }
  370. #endif
  371. bool target_words_bigendian(void)
  372. {
  373. #if TARGET_BIG_ENDIAN
  374. return true;
  375. #else
  376. return false;
  377. #endif
  378. }
  379. const char *target_name(void)
  380. {
  381. return TARGET_NAME;
  382. }
  383. void page_size_init(void)
  384. {
  385. /* NOTE: we can always suppose that qemu_host_page_size >=
  386. TARGET_PAGE_SIZE */
  387. if (qemu_host_page_size == 0) {
  388. qemu_host_page_size = qemu_real_host_page_size();
  389. }
  390. if (qemu_host_page_size < TARGET_PAGE_SIZE) {
  391. qemu_host_page_size = TARGET_PAGE_SIZE;
  392. }
  393. qemu_host_page_mask = -(intptr_t)qemu_host_page_size;
  394. }