2
0

cpu-system.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * QEMU S/390 CPU - System-only code
  3. *
  4. * Copyright (c) 2009 Ulrich Hecht
  5. * Copyright (c) 2011 Alexander Graf
  6. * Copyright (c) 2012 SUSE LINUX Products GmbH
  7. * Copyright (c) 2012 IBM Corp.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/error-report.h"
  24. #include "qapi/error.h"
  25. #include "cpu.h"
  26. #include "s390x-internal.h"
  27. #include "kvm/kvm_s390x.h"
  28. #include "system/kvm.h"
  29. #include "system/reset.h"
  30. #include "qemu/timer.h"
  31. #include "trace.h"
  32. #include "qapi/qapi-visit-run-state.h"
  33. #include "system/hw_accel.h"
  34. #include "target/s390x/kvm/pv.h"
  35. #include "hw/boards.h"
  36. #include "system/system.h"
  37. #include "system/tcg.h"
  38. #include "hw/core/sysemu-cpu-ops.h"
  39. bool s390_cpu_has_work(CPUState *cs)
  40. {
  41. S390CPU *cpu = S390_CPU(cs);
  42. /* STOPPED cpus can never wake up */
  43. if (s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD &&
  44. s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) {
  45. return false;
  46. }
  47. if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
  48. return false;
  49. }
  50. return s390_cpu_has_int(cpu);
  51. }
  52. /* S390CPUClass::load_normal() */
  53. static void s390_cpu_load_normal(CPUState *s)
  54. {
  55. S390CPU *cpu = S390_CPU(s);
  56. uint64_t spsw;
  57. if (!s390_is_pv()) {
  58. spsw = ldq_phys(s->as, 0);
  59. cpu->env.psw.mask = spsw & PSW_MASK_SHORT_CTRL;
  60. /*
  61. * Invert short psw indication, so SIE will report a specification
  62. * exception if it was not set.
  63. */
  64. cpu->env.psw.mask ^= PSW_MASK_SHORTPSW;
  65. cpu->env.psw.addr = spsw & PSW_MASK_SHORT_ADDR;
  66. } else {
  67. /*
  68. * Firmware requires us to set the load state before we set
  69. * the cpu to operating on protected guests.
  70. */
  71. s390_cpu_set_state(S390_CPU_STATE_LOAD, cpu);
  72. }
  73. s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
  74. }
  75. void s390_cpu_machine_reset_cb(void *opaque)
  76. {
  77. S390CPU *cpu = opaque;
  78. run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
  79. }
  80. static GuestPanicInformation *s390_cpu_get_crash_info(CPUState *cs)
  81. {
  82. GuestPanicInformation *panic_info;
  83. S390CPU *cpu = S390_CPU(cs);
  84. cpu_synchronize_state(cs);
  85. panic_info = g_new0(GuestPanicInformation, 1);
  86. panic_info->type = GUEST_PANIC_INFORMATION_TYPE_S390;
  87. panic_info->u.s390.core = cpu->env.core_id;
  88. panic_info->u.s390.psw_mask = cpu->env.psw.mask;
  89. panic_info->u.s390.psw_addr = cpu->env.psw.addr;
  90. panic_info->u.s390.reason = cpu->env.crash_reason;
  91. return panic_info;
  92. }
  93. static void s390_cpu_get_crash_info_qom(Object *obj, Visitor *v,
  94. const char *name, void *opaque,
  95. Error **errp)
  96. {
  97. CPUState *cs = CPU(obj);
  98. GuestPanicInformation *panic_info;
  99. if (!cs->crash_occurred) {
  100. error_setg(errp, "No crash occurred");
  101. return;
  102. }
  103. panic_info = s390_cpu_get_crash_info(cs);
  104. visit_type_GuestPanicInformation(v, "crash-information", &panic_info,
  105. errp);
  106. qapi_free_GuestPanicInformation(panic_info);
  107. }
  108. void s390_cpu_system_init(Object *obj)
  109. {
  110. CPUState *cs = CPU(obj);
  111. S390CPU *cpu = S390_CPU(obj);
  112. cs->start_powered_off = true;
  113. object_property_add(obj, "crash-information", "GuestPanicInformation",
  114. s390_cpu_get_crash_info_qom, NULL, NULL, NULL);
  115. cpu->env.tod_timer =
  116. timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
  117. cpu->env.cpu_timer =
  118. timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
  119. s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
  120. }
  121. bool s390_cpu_system_realize(DeviceState *dev, Error **errp)
  122. {
  123. S390CPU *cpu = S390_CPU(dev);
  124. MachineState *ms = MACHINE(qdev_get_machine());
  125. unsigned int max_cpus = ms->smp.max_cpus;
  126. if (cpu->env.core_id >= max_cpus) {
  127. error_setg(errp, "Unable to add CPU with core-id: %" PRIu32
  128. ", maximum core-id: %d", cpu->env.core_id,
  129. max_cpus - 1);
  130. return false;
  131. }
  132. if (cpu_exists(cpu->env.core_id)) {
  133. error_setg(errp, "Unable to add CPU with core-id: %" PRIu32
  134. ", it already exists", cpu->env.core_id);
  135. return false;
  136. }
  137. /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
  138. CPU(cpu)->cpu_index = cpu->env.core_id;
  139. return true;
  140. }
  141. void s390_cpu_finalize(Object *obj)
  142. {
  143. S390CPU *cpu = S390_CPU(obj);
  144. timer_free(cpu->env.tod_timer);
  145. timer_free(cpu->env.cpu_timer);
  146. qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
  147. g_free(cpu->irqstate);
  148. }
  149. static const struct SysemuCPUOps s390_sysemu_ops = {
  150. .has_work = s390_cpu_has_work,
  151. .get_phys_page_debug = s390_cpu_get_phys_page_debug,
  152. .get_crash_info = s390_cpu_get_crash_info,
  153. .write_elf64_note = s390_cpu_write_elf64_note,
  154. .legacy_vmsd = &vmstate_s390_cpu,
  155. };
  156. void s390_cpu_system_class_init(CPUClass *cc)
  157. {
  158. S390CPUClass *scc = S390_CPU_CLASS(cc);
  159. scc->load_normal = s390_cpu_load_normal;
  160. cc->sysemu_ops = &s390_sysemu_ops;
  161. }
  162. static bool disabled_wait(CPUState *cpu)
  163. {
  164. return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
  165. (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
  166. }
  167. static unsigned s390_count_running_cpus(void)
  168. {
  169. CPUState *cpu;
  170. int nr_running = 0;
  171. CPU_FOREACH(cpu) {
  172. uint8_t state = S390_CPU(cpu)->env.cpu_state;
  173. if (state == S390_CPU_STATE_OPERATING ||
  174. state == S390_CPU_STATE_LOAD) {
  175. if (!disabled_wait(cpu)) {
  176. nr_running++;
  177. }
  178. }
  179. }
  180. return nr_running;
  181. }
  182. unsigned int s390_cpu_halt(S390CPU *cpu)
  183. {
  184. CPUState *cs = CPU(cpu);
  185. trace_cpu_halt(cs->cpu_index);
  186. if (!cs->halted) {
  187. cs->halted = 1;
  188. cs->exception_index = EXCP_HLT;
  189. }
  190. return s390_count_running_cpus();
  191. }
  192. void s390_cpu_unhalt(S390CPU *cpu)
  193. {
  194. CPUState *cs = CPU(cpu);
  195. trace_cpu_unhalt(cs->cpu_index);
  196. if (cs->halted) {
  197. cs->halted = 0;
  198. cs->exception_index = -1;
  199. }
  200. }
  201. unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
  202. {
  203. trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
  204. switch (cpu_state) {
  205. case S390_CPU_STATE_STOPPED:
  206. case S390_CPU_STATE_CHECK_STOP:
  207. /* halt the cpu for common infrastructure */
  208. s390_cpu_halt(cpu);
  209. break;
  210. case S390_CPU_STATE_OPERATING:
  211. case S390_CPU_STATE_LOAD:
  212. /*
  213. * Starting a CPU with a PSW WAIT bit set:
  214. * KVM: handles this internally and triggers another WAIT exit.
  215. * TCG: will actually try to continue to run. Don't unhalt, will
  216. * be done when the CPU actually has work (an interrupt).
  217. */
  218. if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) {
  219. s390_cpu_unhalt(cpu);
  220. }
  221. break;
  222. default:
  223. error_report("Requested CPU state is not a valid S390 CPU state: %u",
  224. cpu_state);
  225. exit(1);
  226. }
  227. if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
  228. kvm_s390_set_cpu_state(cpu, cpu_state);
  229. }
  230. cpu->env.cpu_state = cpu_state;
  231. return s390_count_running_cpus();
  232. }
  233. void s390_cmma_reset(void)
  234. {
  235. if (kvm_enabled()) {
  236. kvm_s390_cmma_reset();
  237. }
  238. }
  239. int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
  240. int vq, bool assign)
  241. {
  242. if (kvm_enabled()) {
  243. return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign);
  244. } else {
  245. return 0;
  246. }
  247. }
  248. void s390_crypto_reset(void)
  249. {
  250. if (kvm_enabled()) {
  251. kvm_s390_crypto_reset();
  252. }
  253. }
  254. void s390_enable_css_support(S390CPU *cpu)
  255. {
  256. if (kvm_enabled()) {
  257. kvm_s390_enable_css_support(cpu);
  258. }
  259. }
  260. void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg)
  261. {
  262. if (kvm_enabled()) {
  263. kvm_s390_set_diag318(cs, arg.host_ulong);
  264. }
  265. }
  266. void s390_cpu_topology_set_changed(bool changed)
  267. {
  268. int ret;
  269. if (kvm_enabled()) {
  270. ret = kvm_s390_topology_set_mtcr(changed);
  271. if (ret) {
  272. error_report("Failed to set Modified Topology Change Report: %s",
  273. strerror(-ret));
  274. }
  275. }
  276. }