cpu-target.c 11 KB

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