machine-qmp-cmds.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * QMP commands related to machines and CPUs
  3. *
  4. * Copyright (C) 2014 Red Hat Inc
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "cpu.h"
  11. #include "hw/boards.h"
  12. #include "qapi/error.h"
  13. #include "qapi/qapi-builtin-visit.h"
  14. #include "qapi/qapi-commands-machine.h"
  15. #include "qapi/qmp/qerror.h"
  16. #include "qapi/qmp/qobject.h"
  17. #include "qapi/qobject-input-visitor.h"
  18. #include "qemu/main-loop.h"
  19. #include "qom/qom-qobject.h"
  20. #include "sysemu/hostmem.h"
  21. #include "sysemu/hw_accel.h"
  22. #include "sysemu/numa.h"
  23. #include "sysemu/runstate.h"
  24. #include "sysemu/sysemu.h"
  25. CpuInfoList *qmp_query_cpus(Error **errp)
  26. {
  27. MachineState *ms = MACHINE(qdev_get_machine());
  28. MachineClass *mc = MACHINE_GET_CLASS(ms);
  29. CpuInfoList *head = NULL, **tail = &head;
  30. CPUState *cpu;
  31. CPU_FOREACH(cpu) {
  32. CpuInfo *value;
  33. #if defined(TARGET_I386)
  34. X86CPU *x86_cpu = X86_CPU(cpu);
  35. CPUX86State *env = &x86_cpu->env;
  36. #elif defined(TARGET_PPC)
  37. PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
  38. CPUPPCState *env = &ppc_cpu->env;
  39. #elif defined(TARGET_SPARC)
  40. SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
  41. CPUSPARCState *env = &sparc_cpu->env;
  42. #elif defined(TARGET_RISCV)
  43. RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
  44. CPURISCVState *env = &riscv_cpu->env;
  45. #elif defined(TARGET_MIPS)
  46. MIPSCPU *mips_cpu = MIPS_CPU(cpu);
  47. CPUMIPSState *env = &mips_cpu->env;
  48. #elif defined(TARGET_TRICORE)
  49. TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
  50. CPUTriCoreState *env = &tricore_cpu->env;
  51. #elif defined(TARGET_S390X)
  52. S390CPU *s390_cpu = S390_CPU(cpu);
  53. CPUS390XState *env = &s390_cpu->env;
  54. #endif
  55. cpu_synchronize_state(cpu);
  56. value = g_malloc0(sizeof(*value));
  57. value->CPU = cpu->cpu_index;
  58. value->current = (cpu == first_cpu);
  59. value->halted = cpu->halted;
  60. value->qom_path = object_get_canonical_path(OBJECT(cpu));
  61. value->thread_id = cpu->thread_id;
  62. #if defined(TARGET_I386)
  63. value->arch = CPU_INFO_ARCH_X86;
  64. value->u.x86.pc = env->eip + env->segs[R_CS].base;
  65. #elif defined(TARGET_PPC)
  66. value->arch = CPU_INFO_ARCH_PPC;
  67. value->u.ppc.nip = env->nip;
  68. #elif defined(TARGET_SPARC)
  69. value->arch = CPU_INFO_ARCH_SPARC;
  70. value->u.q_sparc.pc = env->pc;
  71. value->u.q_sparc.npc = env->npc;
  72. #elif defined(TARGET_MIPS)
  73. value->arch = CPU_INFO_ARCH_MIPS;
  74. value->u.q_mips.PC = env->active_tc.PC;
  75. #elif defined(TARGET_TRICORE)
  76. value->arch = CPU_INFO_ARCH_TRICORE;
  77. value->u.tricore.PC = env->PC;
  78. #elif defined(TARGET_S390X)
  79. value->arch = CPU_INFO_ARCH_S390;
  80. value->u.s390.cpu_state = env->cpu_state;
  81. #elif defined(TARGET_RISCV)
  82. value->arch = CPU_INFO_ARCH_RISCV;
  83. value->u.riscv.pc = env->pc;
  84. #else
  85. value->arch = CPU_INFO_ARCH_OTHER;
  86. #endif
  87. value->has_props = !!mc->cpu_index_to_instance_props;
  88. if (value->has_props) {
  89. CpuInstanceProperties *props;
  90. props = g_malloc0(sizeof(*props));
  91. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  92. value->props = props;
  93. }
  94. QAPI_LIST_APPEND(tail, value);
  95. }
  96. return head;
  97. }
  98. static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
  99. {
  100. /*
  101. * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
  102. * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
  103. */
  104. switch (target) {
  105. case SYS_EMU_TARGET_I386:
  106. case SYS_EMU_TARGET_X86_64:
  107. return CPU_INFO_ARCH_X86;
  108. case SYS_EMU_TARGET_PPC:
  109. case SYS_EMU_TARGET_PPC64:
  110. return CPU_INFO_ARCH_PPC;
  111. case SYS_EMU_TARGET_SPARC:
  112. case SYS_EMU_TARGET_SPARC64:
  113. return CPU_INFO_ARCH_SPARC;
  114. case SYS_EMU_TARGET_MIPS:
  115. case SYS_EMU_TARGET_MIPSEL:
  116. case SYS_EMU_TARGET_MIPS64:
  117. case SYS_EMU_TARGET_MIPS64EL:
  118. return CPU_INFO_ARCH_MIPS;
  119. case SYS_EMU_TARGET_TRICORE:
  120. return CPU_INFO_ARCH_TRICORE;
  121. case SYS_EMU_TARGET_S390X:
  122. return CPU_INFO_ARCH_S390;
  123. case SYS_EMU_TARGET_RISCV32:
  124. case SYS_EMU_TARGET_RISCV64:
  125. return CPU_INFO_ARCH_RISCV;
  126. default:
  127. return CPU_INFO_ARCH_OTHER;
  128. }
  129. }
  130. static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
  131. {
  132. #ifdef TARGET_S390X
  133. S390CPU *s390_cpu = S390_CPU(cpu);
  134. CPUS390XState *env = &s390_cpu->env;
  135. info->cpu_state = env->cpu_state;
  136. #else
  137. abort();
  138. #endif
  139. }
  140. /*
  141. * fast means: we NEVER interrupt vCPU threads to retrieve
  142. * information from KVM.
  143. */
  144. CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
  145. {
  146. MachineState *ms = MACHINE(qdev_get_machine());
  147. MachineClass *mc = MACHINE_GET_CLASS(ms);
  148. CpuInfoFastList *head = NULL, **tail = &head;
  149. SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
  150. -1, &error_abort);
  151. CPUState *cpu;
  152. CPU_FOREACH(cpu) {
  153. CpuInfoFast *value = g_malloc0(sizeof(*value));
  154. value->cpu_index = cpu->cpu_index;
  155. value->qom_path = object_get_canonical_path(OBJECT(cpu));
  156. value->thread_id = cpu->thread_id;
  157. value->has_props = !!mc->cpu_index_to_instance_props;
  158. if (value->has_props) {
  159. CpuInstanceProperties *props;
  160. props = g_malloc0(sizeof(*props));
  161. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  162. value->props = props;
  163. }
  164. value->arch = sysemu_target_to_cpuinfo_arch(target);
  165. value->target = target;
  166. if (target == SYS_EMU_TARGET_S390X) {
  167. cpustate_to_cpuinfo_s390(&value->u.s390x, cpu);
  168. }
  169. QAPI_LIST_APPEND(tail, value);
  170. }
  171. return head;
  172. }
  173. MachineInfoList *qmp_query_machines(Error **errp)
  174. {
  175. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  176. MachineInfoList *mach_list = NULL;
  177. for (el = machines; el; el = el->next) {
  178. MachineClass *mc = el->data;
  179. MachineInfo *info;
  180. info = g_malloc0(sizeof(*info));
  181. if (mc->is_default) {
  182. info->has_is_default = true;
  183. info->is_default = true;
  184. }
  185. if (mc->alias) {
  186. info->has_alias = true;
  187. info->alias = g_strdup(mc->alias);
  188. }
  189. info->name = g_strdup(mc->name);
  190. info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
  191. info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
  192. info->numa_mem_supported = mc->numa_mem_supported;
  193. info->deprecated = !!mc->deprecation_reason;
  194. if (mc->default_cpu_type) {
  195. info->default_cpu_type = g_strdup(mc->default_cpu_type);
  196. info->has_default_cpu_type = true;
  197. }
  198. if (mc->default_ram_id) {
  199. info->default_ram_id = g_strdup(mc->default_ram_id);
  200. info->has_default_ram_id = true;
  201. }
  202. QAPI_LIST_PREPEND(mach_list, info);
  203. }
  204. g_slist_free(machines);
  205. return mach_list;
  206. }
  207. CurrentMachineParams *qmp_query_current_machine(Error **errp)
  208. {
  209. CurrentMachineParams *params = g_malloc0(sizeof(*params));
  210. params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
  211. return params;
  212. }
  213. TargetInfo *qmp_query_target(Error **errp)
  214. {
  215. TargetInfo *info = g_malloc0(sizeof(*info));
  216. info->arch = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME, -1,
  217. &error_abort);
  218. return info;
  219. }
  220. HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
  221. {
  222. MachineState *ms = MACHINE(qdev_get_machine());
  223. MachineClass *mc = MACHINE_GET_CLASS(ms);
  224. if (!mc->has_hotpluggable_cpus) {
  225. error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
  226. return NULL;
  227. }
  228. return machine_query_hotpluggable_cpus(ms);
  229. }
  230. void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
  231. {
  232. if (phase_check(PHASE_MACHINE_INITIALIZED)) {
  233. error_setg(errp, "The command is permitted only before the machine has been created");
  234. return;
  235. }
  236. set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
  237. }
  238. static int query_memdev(Object *obj, void *opaque)
  239. {
  240. MemdevList **list = opaque;
  241. Memdev *m;
  242. QObject *host_nodes;
  243. Visitor *v;
  244. if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
  245. m = g_malloc0(sizeof(*m));
  246. m->id = g_strdup(object_get_canonical_path_component(obj));
  247. m->has_id = !!m->id;
  248. m->size = object_property_get_uint(obj, "size", &error_abort);
  249. m->merge = object_property_get_bool(obj, "merge", &error_abort);
  250. m->dump = object_property_get_bool(obj, "dump", &error_abort);
  251. m->prealloc = object_property_get_bool(obj, "prealloc", &error_abort);
  252. m->policy = object_property_get_enum(obj, "policy", "HostMemPolicy",
  253. &error_abort);
  254. host_nodes = object_property_get_qobject(obj,
  255. "host-nodes",
  256. &error_abort);
  257. v = qobject_input_visitor_new(host_nodes);
  258. visit_type_uint16List(v, NULL, &m->host_nodes, &error_abort);
  259. visit_free(v);
  260. qobject_unref(host_nodes);
  261. QAPI_LIST_PREPEND(*list, m);
  262. }
  263. return 0;
  264. }
  265. MemdevList *qmp_query_memdev(Error **errp)
  266. {
  267. Object *obj = object_get_objects_root();
  268. MemdevList *list = NULL;
  269. object_child_foreach(obj, query_memdev, &list);
  270. return list;
  271. }