tcg-all.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * QEMU System Emulator, accelerator interfaces
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2014 Red Hat Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "system/tcg.h"
  27. #include "exec/replay-core.h"
  28. #include "system/cpu-timers.h"
  29. #include "tcg/startup.h"
  30. #include "tcg/oversized-guest.h"
  31. #include "qapi/error.h"
  32. #include "qemu/error-report.h"
  33. #include "qemu/accel.h"
  34. #include "qemu/atomic.h"
  35. #include "qapi/qapi-builtin-visit.h"
  36. #include "qemu/units.h"
  37. #if !defined(CONFIG_USER_ONLY)
  38. #include "hw/boards.h"
  39. #endif
  40. #include "internal-common.h"
  41. struct TCGState {
  42. AccelState parent_obj;
  43. bool mttcg_enabled;
  44. bool one_insn_per_tb;
  45. int splitwx_enabled;
  46. unsigned long tb_size;
  47. };
  48. typedef struct TCGState TCGState;
  49. #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
  50. DECLARE_INSTANCE_CHECKER(TCGState, TCG_STATE,
  51. TYPE_TCG_ACCEL)
  52. /*
  53. * We default to false if we know other options have been enabled
  54. * which are currently incompatible with MTTCG. Otherwise when each
  55. * guest (target) has been updated to support:
  56. * - atomic instructions
  57. * - memory ordering primitives (barriers)
  58. * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
  59. *
  60. * Once a guest architecture has been converted to the new primitives
  61. * there is one remaining limitation to check:
  62. * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
  63. */
  64. static bool default_mttcg_enabled(void)
  65. {
  66. if (icount_enabled() || TCG_OVERSIZED_GUEST) {
  67. return false;
  68. }
  69. #ifdef TARGET_SUPPORTS_MTTCG
  70. # ifndef TCG_GUEST_DEFAULT_MO
  71. # error "TARGET_SUPPORTS_MTTCG without TCG_GUEST_DEFAULT_MO"
  72. # endif
  73. return true;
  74. #else
  75. return false;
  76. #endif
  77. }
  78. static void tcg_accel_instance_init(Object *obj)
  79. {
  80. TCGState *s = TCG_STATE(obj);
  81. s->mttcg_enabled = default_mttcg_enabled();
  82. /* If debugging enabled, default "auto on", otherwise off. */
  83. #if defined(CONFIG_DEBUG_TCG) && !defined(CONFIG_USER_ONLY)
  84. s->splitwx_enabled = -1;
  85. #else
  86. s->splitwx_enabled = 0;
  87. #endif
  88. }
  89. bool mttcg_enabled;
  90. bool one_insn_per_tb;
  91. static int tcg_init_machine(MachineState *ms)
  92. {
  93. TCGState *s = TCG_STATE(current_accel());
  94. #ifdef CONFIG_USER_ONLY
  95. unsigned max_cpus = 1;
  96. #else
  97. unsigned max_cpus = ms->smp.max_cpus;
  98. #endif
  99. tcg_allowed = true;
  100. mttcg_enabled = s->mttcg_enabled;
  101. page_init();
  102. tb_htable_init();
  103. tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_cpus);
  104. #if defined(CONFIG_SOFTMMU)
  105. /*
  106. * There's no guest base to take into account, so go ahead and
  107. * initialize the prologue now.
  108. */
  109. tcg_prologue_init();
  110. #endif
  111. return 0;
  112. }
  113. static char *tcg_get_thread(Object *obj, Error **errp)
  114. {
  115. TCGState *s = TCG_STATE(obj);
  116. return g_strdup(s->mttcg_enabled ? "multi" : "single");
  117. }
  118. static void tcg_set_thread(Object *obj, const char *value, Error **errp)
  119. {
  120. TCGState *s = TCG_STATE(obj);
  121. if (strcmp(value, "multi") == 0) {
  122. if (TCG_OVERSIZED_GUEST) {
  123. error_setg(errp, "No MTTCG when guest word size > hosts");
  124. } else if (icount_enabled()) {
  125. error_setg(errp, "No MTTCG when icount is enabled");
  126. } else {
  127. #ifndef TARGET_SUPPORTS_MTTCG
  128. warn_report("Guest not yet converted to MTTCG - "
  129. "you may get unexpected results");
  130. #endif
  131. s->mttcg_enabled = true;
  132. }
  133. } else if (strcmp(value, "single") == 0) {
  134. s->mttcg_enabled = false;
  135. } else {
  136. error_setg(errp, "Invalid 'thread' setting %s", value);
  137. }
  138. }
  139. static void tcg_get_tb_size(Object *obj, Visitor *v,
  140. const char *name, void *opaque,
  141. Error **errp)
  142. {
  143. TCGState *s = TCG_STATE(obj);
  144. uint32_t value = s->tb_size;
  145. visit_type_uint32(v, name, &value, errp);
  146. }
  147. static void tcg_set_tb_size(Object *obj, Visitor *v,
  148. const char *name, void *opaque,
  149. Error **errp)
  150. {
  151. TCGState *s = TCG_STATE(obj);
  152. uint32_t value;
  153. if (!visit_type_uint32(v, name, &value, errp)) {
  154. return;
  155. }
  156. s->tb_size = value;
  157. }
  158. static bool tcg_get_splitwx(Object *obj, Error **errp)
  159. {
  160. TCGState *s = TCG_STATE(obj);
  161. return s->splitwx_enabled;
  162. }
  163. static void tcg_set_splitwx(Object *obj, bool value, Error **errp)
  164. {
  165. TCGState *s = TCG_STATE(obj);
  166. s->splitwx_enabled = value;
  167. }
  168. static bool tcg_get_one_insn_per_tb(Object *obj, Error **errp)
  169. {
  170. TCGState *s = TCG_STATE(obj);
  171. return s->one_insn_per_tb;
  172. }
  173. static void tcg_set_one_insn_per_tb(Object *obj, bool value, Error **errp)
  174. {
  175. TCGState *s = TCG_STATE(obj);
  176. s->one_insn_per_tb = value;
  177. /* Set the global also: this changes the behaviour */
  178. qatomic_set(&one_insn_per_tb, value);
  179. }
  180. static int tcg_gdbstub_supported_sstep_flags(void)
  181. {
  182. /*
  183. * In replay mode all events will come from the log and can't be
  184. * suppressed otherwise we would break determinism. However as those
  185. * events are tied to the number of executed instructions we won't see
  186. * them occurring every time we single step.
  187. */
  188. if (replay_mode != REPLAY_MODE_NONE) {
  189. return SSTEP_ENABLE;
  190. } else {
  191. return SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
  192. }
  193. }
  194. static void tcg_accel_class_init(ObjectClass *oc, void *data)
  195. {
  196. AccelClass *ac = ACCEL_CLASS(oc);
  197. ac->name = "tcg";
  198. ac->init_machine = tcg_init_machine;
  199. ac->cpu_common_realize = tcg_exec_realizefn;
  200. ac->cpu_common_unrealize = tcg_exec_unrealizefn;
  201. ac->allowed = &tcg_allowed;
  202. ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
  203. object_class_property_add_str(oc, "thread",
  204. tcg_get_thread,
  205. tcg_set_thread);
  206. object_class_property_add(oc, "tb-size", "int",
  207. tcg_get_tb_size, tcg_set_tb_size,
  208. NULL, NULL);
  209. object_class_property_set_description(oc, "tb-size",
  210. "TCG translation block cache size");
  211. object_class_property_add_bool(oc, "split-wx",
  212. tcg_get_splitwx, tcg_set_splitwx);
  213. object_class_property_set_description(oc, "split-wx",
  214. "Map jit pages into separate RW and RX regions");
  215. object_class_property_add_bool(oc, "one-insn-per-tb",
  216. tcg_get_one_insn_per_tb,
  217. tcg_set_one_insn_per_tb);
  218. object_class_property_set_description(oc, "one-insn-per-tb",
  219. "Only put one guest insn in each translation block");
  220. }
  221. static const TypeInfo tcg_accel_type = {
  222. .name = TYPE_TCG_ACCEL,
  223. .parent = TYPE_ACCEL,
  224. .instance_init = tcg_accel_instance_init,
  225. .class_init = tcg_accel_class_init,
  226. .instance_size = sizeof(TCGState),
  227. };
  228. module_obj(TYPE_TCG_ACCEL);
  229. static void register_accel_types(void)
  230. {
  231. type_register_static(&tcg_accel_type);
  232. }
  233. type_init(register_accel_types);