2
0

machine-qmp-cmds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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, *cur_item = NULL;
  30. CPUState *cpu;
  31. CPU_FOREACH(cpu) {
  32. CpuInfoList *info;
  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. info = g_malloc0(sizeof(*info));
  57. info->value = g_malloc0(sizeof(*info->value));
  58. info->value->CPU = cpu->cpu_index;
  59. info->value->current = (cpu == first_cpu);
  60. info->value->halted = cpu->halted;
  61. info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
  62. info->value->thread_id = cpu->thread_id;
  63. #if defined(TARGET_I386)
  64. info->value->arch = CPU_INFO_ARCH_X86;
  65. info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
  66. #elif defined(TARGET_PPC)
  67. info->value->arch = CPU_INFO_ARCH_PPC;
  68. info->value->u.ppc.nip = env->nip;
  69. #elif defined(TARGET_SPARC)
  70. info->value->arch = CPU_INFO_ARCH_SPARC;
  71. info->value->u.q_sparc.pc = env->pc;
  72. info->value->u.q_sparc.npc = env->npc;
  73. #elif defined(TARGET_MIPS)
  74. info->value->arch = CPU_INFO_ARCH_MIPS;
  75. info->value->u.q_mips.PC = env->active_tc.PC;
  76. #elif defined(TARGET_TRICORE)
  77. info->value->arch = CPU_INFO_ARCH_TRICORE;
  78. info->value->u.tricore.PC = env->PC;
  79. #elif defined(TARGET_S390X)
  80. info->value->arch = CPU_INFO_ARCH_S390;
  81. info->value->u.s390.cpu_state = env->cpu_state;
  82. #elif defined(TARGET_RISCV)
  83. info->value->arch = CPU_INFO_ARCH_RISCV;
  84. info->value->u.riscv.pc = env->pc;
  85. #else
  86. info->value->arch = CPU_INFO_ARCH_OTHER;
  87. #endif
  88. info->value->has_props = !!mc->cpu_index_to_instance_props;
  89. if (info->value->has_props) {
  90. CpuInstanceProperties *props;
  91. props = g_malloc0(sizeof(*props));
  92. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  93. info->value->props = props;
  94. }
  95. /* XXX: waiting for the qapi to support GSList */
  96. if (!cur_item) {
  97. head = cur_item = info;
  98. } else {
  99. cur_item->next = info;
  100. cur_item = info;
  101. }
  102. }
  103. return head;
  104. }
  105. static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
  106. {
  107. /*
  108. * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
  109. * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
  110. */
  111. switch (target) {
  112. case SYS_EMU_TARGET_I386:
  113. case SYS_EMU_TARGET_X86_64:
  114. return CPU_INFO_ARCH_X86;
  115. case SYS_EMU_TARGET_PPC:
  116. case SYS_EMU_TARGET_PPC64:
  117. return CPU_INFO_ARCH_PPC;
  118. case SYS_EMU_TARGET_SPARC:
  119. case SYS_EMU_TARGET_SPARC64:
  120. return CPU_INFO_ARCH_SPARC;
  121. case SYS_EMU_TARGET_MIPS:
  122. case SYS_EMU_TARGET_MIPSEL:
  123. case SYS_EMU_TARGET_MIPS64:
  124. case SYS_EMU_TARGET_MIPS64EL:
  125. return CPU_INFO_ARCH_MIPS;
  126. case SYS_EMU_TARGET_TRICORE:
  127. return CPU_INFO_ARCH_TRICORE;
  128. case SYS_EMU_TARGET_S390X:
  129. return CPU_INFO_ARCH_S390;
  130. case SYS_EMU_TARGET_RISCV32:
  131. case SYS_EMU_TARGET_RISCV64:
  132. return CPU_INFO_ARCH_RISCV;
  133. default:
  134. return CPU_INFO_ARCH_OTHER;
  135. }
  136. }
  137. static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
  138. {
  139. #ifdef TARGET_S390X
  140. S390CPU *s390_cpu = S390_CPU(cpu);
  141. CPUS390XState *env = &s390_cpu->env;
  142. info->cpu_state = env->cpu_state;
  143. #else
  144. abort();
  145. #endif
  146. }
  147. /*
  148. * fast means: we NEVER interrupt vCPU threads to retrieve
  149. * information from KVM.
  150. */
  151. CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
  152. {
  153. MachineState *ms = MACHINE(qdev_get_machine());
  154. MachineClass *mc = MACHINE_GET_CLASS(ms);
  155. CpuInfoFastList *head = NULL, *cur_item = NULL;
  156. SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
  157. -1, &error_abort);
  158. CPUState *cpu;
  159. CPU_FOREACH(cpu) {
  160. CpuInfoFastList *info = g_malloc0(sizeof(*info));
  161. info->value = g_malloc0(sizeof(*info->value));
  162. info->value->cpu_index = cpu->cpu_index;
  163. info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
  164. info->value->thread_id = cpu->thread_id;
  165. info->value->has_props = !!mc->cpu_index_to_instance_props;
  166. if (info->value->has_props) {
  167. CpuInstanceProperties *props;
  168. props = g_malloc0(sizeof(*props));
  169. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  170. info->value->props = props;
  171. }
  172. info->value->arch = sysemu_target_to_cpuinfo_arch(target);
  173. info->value->target = target;
  174. if (target == SYS_EMU_TARGET_S390X) {
  175. cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
  176. }
  177. if (!cur_item) {
  178. head = cur_item = info;
  179. } else {
  180. cur_item->next = info;
  181. cur_item = info;
  182. }
  183. }
  184. return head;
  185. }
  186. MachineInfoList *qmp_query_machines(Error **errp)
  187. {
  188. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  189. MachineInfoList *mach_list = NULL;
  190. for (el = machines; el; el = el->next) {
  191. MachineClass *mc = el->data;
  192. MachineInfoList *entry;
  193. MachineInfo *info;
  194. info = g_malloc0(sizeof(*info));
  195. if (mc->is_default) {
  196. info->has_is_default = true;
  197. info->is_default = true;
  198. }
  199. if (mc->alias) {
  200. info->has_alias = true;
  201. info->alias = g_strdup(mc->alias);
  202. }
  203. info->name = g_strdup(mc->name);
  204. info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
  205. info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
  206. info->numa_mem_supported = mc->numa_mem_supported;
  207. info->deprecated = !!mc->deprecation_reason;
  208. if (mc->default_cpu_type) {
  209. info->default_cpu_type = g_strdup(mc->default_cpu_type);
  210. info->has_default_cpu_type = true;
  211. }
  212. if (mc->default_ram_id) {
  213. info->default_ram_id = g_strdup(mc->default_ram_id);
  214. info->has_default_ram_id = true;
  215. }
  216. entry = g_malloc0(sizeof(*entry));
  217. entry->value = info;
  218. entry->next = mach_list;
  219. mach_list = entry;
  220. }
  221. g_slist_free(machines);
  222. return mach_list;
  223. }
  224. CurrentMachineParams *qmp_query_current_machine(Error **errp)
  225. {
  226. CurrentMachineParams *params = g_malloc0(sizeof(*params));
  227. params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
  228. return params;
  229. }
  230. TargetInfo *qmp_query_target(Error **errp)
  231. {
  232. TargetInfo *info = g_malloc0(sizeof(*info));
  233. info->arch = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME, -1,
  234. &error_abort);
  235. return info;
  236. }
  237. HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
  238. {
  239. MachineState *ms = MACHINE(qdev_get_machine());
  240. MachineClass *mc = MACHINE_GET_CLASS(ms);
  241. if (!mc->has_hotpluggable_cpus) {
  242. error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
  243. return NULL;
  244. }
  245. return machine_query_hotpluggable_cpus(ms);
  246. }
  247. void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
  248. {
  249. if (!runstate_check(RUN_STATE_PRECONFIG)) {
  250. error_setg(errp, "The command is permitted only in '%s' state",
  251. RunState_str(RUN_STATE_PRECONFIG));
  252. return;
  253. }
  254. set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
  255. }
  256. static int query_memdev(Object *obj, void *opaque)
  257. {
  258. MemdevList **list = opaque;
  259. MemdevList *m = NULL;
  260. QObject *host_nodes;
  261. Visitor *v;
  262. if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
  263. m = g_malloc0(sizeof(*m));
  264. m->value = g_malloc0(sizeof(*m->value));
  265. m->value->id = g_strdup(object_get_canonical_path_component(obj));
  266. m->value->has_id = !!m->value->id;
  267. m->value->size = object_property_get_uint(obj, "size",
  268. &error_abort);
  269. m->value->merge = object_property_get_bool(obj, "merge",
  270. &error_abort);
  271. m->value->dump = object_property_get_bool(obj, "dump",
  272. &error_abort);
  273. m->value->prealloc = object_property_get_bool(obj,
  274. "prealloc",
  275. &error_abort);
  276. m->value->policy = object_property_get_enum(obj,
  277. "policy",
  278. "HostMemPolicy",
  279. &error_abort);
  280. host_nodes = object_property_get_qobject(obj,
  281. "host-nodes",
  282. &error_abort);
  283. v = qobject_input_visitor_new(host_nodes);
  284. visit_type_uint16List(v, NULL, &m->value->host_nodes, &error_abort);
  285. visit_free(v);
  286. qobject_unref(host_nodes);
  287. m->next = *list;
  288. *list = m;
  289. }
  290. return 0;
  291. }
  292. MemdevList *qmp_query_memdev(Error **errp)
  293. {
  294. Object *obj = object_get_objects_root();
  295. MemdevList *list = NULL;
  296. object_child_foreach(obj, query_memdev, &list);
  297. return list;
  298. }