machine-qmp-cmds.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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-commands-machine.h"
  14. #include "qapi/qmp/qerror.h"
  15. #include "qemu/main-loop.h"
  16. #include "sysemu/hostmem.h"
  17. #include "sysemu/hw_accel.h"
  18. #include "sysemu/numa.h"
  19. #include "sysemu/runstate.h"
  20. #include "sysemu/sysemu.h"
  21. CpuInfoList *qmp_query_cpus(Error **errp)
  22. {
  23. MachineState *ms = MACHINE(qdev_get_machine());
  24. MachineClass *mc = MACHINE_GET_CLASS(ms);
  25. CpuInfoList *head = NULL, *cur_item = NULL;
  26. CPUState *cpu;
  27. CPU_FOREACH(cpu) {
  28. CpuInfoList *info;
  29. #if defined(TARGET_I386)
  30. X86CPU *x86_cpu = X86_CPU(cpu);
  31. CPUX86State *env = &x86_cpu->env;
  32. #elif defined(TARGET_PPC)
  33. PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
  34. CPUPPCState *env = &ppc_cpu->env;
  35. #elif defined(TARGET_SPARC)
  36. SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
  37. CPUSPARCState *env = &sparc_cpu->env;
  38. #elif defined(TARGET_RISCV)
  39. RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
  40. CPURISCVState *env = &riscv_cpu->env;
  41. #elif defined(TARGET_MIPS)
  42. MIPSCPU *mips_cpu = MIPS_CPU(cpu);
  43. CPUMIPSState *env = &mips_cpu->env;
  44. #elif defined(TARGET_TRICORE)
  45. TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
  46. CPUTriCoreState *env = &tricore_cpu->env;
  47. #elif defined(TARGET_S390X)
  48. S390CPU *s390_cpu = S390_CPU(cpu);
  49. CPUS390XState *env = &s390_cpu->env;
  50. #endif
  51. cpu_synchronize_state(cpu);
  52. info = g_malloc0(sizeof(*info));
  53. info->value = g_malloc0(sizeof(*info->value));
  54. info->value->CPU = cpu->cpu_index;
  55. info->value->current = (cpu == first_cpu);
  56. info->value->halted = cpu->halted;
  57. info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
  58. info->value->thread_id = cpu->thread_id;
  59. #if defined(TARGET_I386)
  60. info->value->arch = CPU_INFO_ARCH_X86;
  61. info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
  62. #elif defined(TARGET_PPC)
  63. info->value->arch = CPU_INFO_ARCH_PPC;
  64. info->value->u.ppc.nip = env->nip;
  65. #elif defined(TARGET_SPARC)
  66. info->value->arch = CPU_INFO_ARCH_SPARC;
  67. info->value->u.q_sparc.pc = env->pc;
  68. info->value->u.q_sparc.npc = env->npc;
  69. #elif defined(TARGET_MIPS)
  70. info->value->arch = CPU_INFO_ARCH_MIPS;
  71. info->value->u.q_mips.PC = env->active_tc.PC;
  72. #elif defined(TARGET_TRICORE)
  73. info->value->arch = CPU_INFO_ARCH_TRICORE;
  74. info->value->u.tricore.PC = env->PC;
  75. #elif defined(TARGET_S390X)
  76. info->value->arch = CPU_INFO_ARCH_S390;
  77. info->value->u.s390.cpu_state = env->cpu_state;
  78. #elif defined(TARGET_RISCV)
  79. info->value->arch = CPU_INFO_ARCH_RISCV;
  80. info->value->u.riscv.pc = env->pc;
  81. #else
  82. info->value->arch = CPU_INFO_ARCH_OTHER;
  83. #endif
  84. info->value->has_props = !!mc->cpu_index_to_instance_props;
  85. if (info->value->has_props) {
  86. CpuInstanceProperties *props;
  87. props = g_malloc0(sizeof(*props));
  88. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  89. info->value->props = props;
  90. }
  91. /* XXX: waiting for the qapi to support GSList */
  92. if (!cur_item) {
  93. head = cur_item = info;
  94. } else {
  95. cur_item->next = info;
  96. cur_item = info;
  97. }
  98. }
  99. return head;
  100. }
  101. static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
  102. {
  103. /*
  104. * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
  105. * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
  106. */
  107. switch (target) {
  108. case SYS_EMU_TARGET_I386:
  109. case SYS_EMU_TARGET_X86_64:
  110. return CPU_INFO_ARCH_X86;
  111. case SYS_EMU_TARGET_PPC:
  112. case SYS_EMU_TARGET_PPC64:
  113. return CPU_INFO_ARCH_PPC;
  114. case SYS_EMU_TARGET_SPARC:
  115. case SYS_EMU_TARGET_SPARC64:
  116. return CPU_INFO_ARCH_SPARC;
  117. case SYS_EMU_TARGET_MIPS:
  118. case SYS_EMU_TARGET_MIPSEL:
  119. case SYS_EMU_TARGET_MIPS64:
  120. case SYS_EMU_TARGET_MIPS64EL:
  121. return CPU_INFO_ARCH_MIPS;
  122. case SYS_EMU_TARGET_TRICORE:
  123. return CPU_INFO_ARCH_TRICORE;
  124. case SYS_EMU_TARGET_S390X:
  125. return CPU_INFO_ARCH_S390;
  126. case SYS_EMU_TARGET_RISCV32:
  127. case SYS_EMU_TARGET_RISCV64:
  128. return CPU_INFO_ARCH_RISCV;
  129. default:
  130. return CPU_INFO_ARCH_OTHER;
  131. }
  132. }
  133. static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
  134. {
  135. #ifdef TARGET_S390X
  136. S390CPU *s390_cpu = S390_CPU(cpu);
  137. CPUS390XState *env = &s390_cpu->env;
  138. info->cpu_state = env->cpu_state;
  139. #else
  140. abort();
  141. #endif
  142. }
  143. /*
  144. * fast means: we NEVER interrupt vCPU threads to retrieve
  145. * information from KVM.
  146. */
  147. CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
  148. {
  149. MachineState *ms = MACHINE(qdev_get_machine());
  150. MachineClass *mc = MACHINE_GET_CLASS(ms);
  151. CpuInfoFastList *head = NULL, *cur_item = NULL;
  152. SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
  153. -1, &error_abort);
  154. CPUState *cpu;
  155. CPU_FOREACH(cpu) {
  156. CpuInfoFastList *info = g_malloc0(sizeof(*info));
  157. info->value = g_malloc0(sizeof(*info->value));
  158. info->value->cpu_index = cpu->cpu_index;
  159. info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
  160. info->value->thread_id = cpu->thread_id;
  161. info->value->has_props = !!mc->cpu_index_to_instance_props;
  162. if (info->value->has_props) {
  163. CpuInstanceProperties *props;
  164. props = g_malloc0(sizeof(*props));
  165. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  166. info->value->props = props;
  167. }
  168. info->value->arch = sysemu_target_to_cpuinfo_arch(target);
  169. info->value->target = target;
  170. if (target == SYS_EMU_TARGET_S390X) {
  171. cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
  172. }
  173. if (!cur_item) {
  174. head = cur_item = info;
  175. } else {
  176. cur_item->next = info;
  177. cur_item = info;
  178. }
  179. }
  180. return head;
  181. }
  182. MachineInfoList *qmp_query_machines(Error **errp)
  183. {
  184. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  185. MachineInfoList *mach_list = NULL;
  186. for (el = machines; el; el = el->next) {
  187. MachineClass *mc = el->data;
  188. MachineInfoList *entry;
  189. MachineInfo *info;
  190. info = g_malloc0(sizeof(*info));
  191. if (mc->is_default) {
  192. info->has_is_default = true;
  193. info->is_default = true;
  194. }
  195. if (mc->alias) {
  196. info->has_alias = true;
  197. info->alias = g_strdup(mc->alias);
  198. }
  199. info->name = g_strdup(mc->name);
  200. info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
  201. info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
  202. info->numa_mem_supported = mc->numa_mem_supported;
  203. info->deprecated = !!mc->deprecation_reason;
  204. if (mc->default_cpu_type) {
  205. info->default_cpu_type = g_strdup(mc->default_cpu_type);
  206. info->has_default_cpu_type = true;
  207. }
  208. entry = g_malloc0(sizeof(*entry));
  209. entry->value = info;
  210. entry->next = mach_list;
  211. mach_list = entry;
  212. }
  213. g_slist_free(machines);
  214. return mach_list;
  215. }
  216. CurrentMachineParams *qmp_query_current_machine(Error **errp)
  217. {
  218. CurrentMachineParams *params = g_malloc0(sizeof(*params));
  219. params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
  220. return params;
  221. }
  222. TargetInfo *qmp_query_target(Error **errp)
  223. {
  224. TargetInfo *info = g_malloc0(sizeof(*info));
  225. info->arch = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME, -1,
  226. &error_abort);
  227. return info;
  228. }
  229. HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
  230. {
  231. MachineState *ms = MACHINE(qdev_get_machine());
  232. MachineClass *mc = MACHINE_GET_CLASS(ms);
  233. if (!mc->has_hotpluggable_cpus) {
  234. error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
  235. return NULL;
  236. }
  237. return machine_query_hotpluggable_cpus(ms);
  238. }
  239. void qmp_cpu_add(int64_t id, Error **errp)
  240. {
  241. MachineClass *mc;
  242. mc = MACHINE_GET_CLASS(current_machine);
  243. if (mc->hot_add_cpu) {
  244. mc->hot_add_cpu(current_machine, id, errp);
  245. } else {
  246. error_setg(errp, "Not supported");
  247. }
  248. }
  249. void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
  250. {
  251. if (!runstate_check(RUN_STATE_PRECONFIG)) {
  252. error_setg(errp, "The command is permitted only in '%s' state",
  253. RunState_str(RUN_STATE_PRECONFIG));
  254. return;
  255. }
  256. set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
  257. }
  258. static int query_memdev(Object *obj, void *opaque)
  259. {
  260. MemdevList **list = opaque;
  261. MemdevList *m = NULL;
  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 = 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. object_property_get_uint16List(obj, "host-nodes",
  281. &m->value->host_nodes,
  282. &error_abort);
  283. m->next = *list;
  284. *list = m;
  285. }
  286. return 0;
  287. }
  288. MemdevList *qmp_query_memdev(Error **errp)
  289. {
  290. Object *obj = object_get_objects_root();
  291. MemdevList *list = NULL;
  292. object_child_foreach(obj, query_memdev, &list);
  293. return list;
  294. }