cpu-target.c 11 KB

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