2
0

machine-qmp-cmds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. entry = g_malloc0(sizeof(*entry));
  213. entry->value = info;
  214. entry->next = mach_list;
  215. mach_list = entry;
  216. }
  217. g_slist_free(machines);
  218. return mach_list;
  219. }
  220. CurrentMachineParams *qmp_query_current_machine(Error **errp)
  221. {
  222. CurrentMachineParams *params = g_malloc0(sizeof(*params));
  223. params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
  224. return params;
  225. }
  226. TargetInfo *qmp_query_target(Error **errp)
  227. {
  228. TargetInfo *info = g_malloc0(sizeof(*info));
  229. info->arch = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME, -1,
  230. &error_abort);
  231. return info;
  232. }
  233. HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
  234. {
  235. MachineState *ms = MACHINE(qdev_get_machine());
  236. MachineClass *mc = MACHINE_GET_CLASS(ms);
  237. if (!mc->has_hotpluggable_cpus) {
  238. error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
  239. return NULL;
  240. }
  241. return machine_query_hotpluggable_cpus(ms);
  242. }
  243. void qmp_cpu_add(int64_t id, Error **errp)
  244. {
  245. MachineClass *mc;
  246. mc = MACHINE_GET_CLASS(current_machine);
  247. if (mc->hot_add_cpu) {
  248. mc->hot_add_cpu(current_machine, id, errp);
  249. } else {
  250. error_setg(errp, "Not supported");
  251. }
  252. }
  253. void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
  254. {
  255. if (!runstate_check(RUN_STATE_PRECONFIG)) {
  256. error_setg(errp, "The command is permitted only in '%s' state",
  257. RunState_str(RUN_STATE_PRECONFIG));
  258. return;
  259. }
  260. set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
  261. }
  262. static int query_memdev(Object *obj, void *opaque)
  263. {
  264. MemdevList **list = opaque;
  265. MemdevList *m = NULL;
  266. QObject *host_nodes;
  267. Visitor *v;
  268. if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
  269. m = g_malloc0(sizeof(*m));
  270. m->value = g_malloc0(sizeof(*m->value));
  271. m->value->id = g_strdup(object_get_canonical_path_component(obj));
  272. m->value->has_id = !!m->value->id;
  273. m->value->size = object_property_get_uint(obj, "size",
  274. &error_abort);
  275. m->value->merge = object_property_get_bool(obj, "merge",
  276. &error_abort);
  277. m->value->dump = object_property_get_bool(obj, "dump",
  278. &error_abort);
  279. m->value->prealloc = object_property_get_bool(obj,
  280. "prealloc",
  281. &error_abort);
  282. m->value->policy = object_property_get_enum(obj,
  283. "policy",
  284. "HostMemPolicy",
  285. &error_abort);
  286. host_nodes = object_property_get_qobject(obj,
  287. "host-nodes",
  288. &error_abort);
  289. v = qobject_input_visitor_new(host_nodes);
  290. visit_type_uint16List(v, NULL, &m->value->host_nodes, &error_abort);
  291. visit_free(v);
  292. qobject_unref(host_nodes);
  293. m->next = *list;
  294. *list = m;
  295. }
  296. return 0;
  297. }
  298. MemdevList *qmp_query_memdev(Error **errp)
  299. {
  300. Object *obj = object_get_objects_root();
  301. MemdevList *list = NULL;
  302. object_child_foreach(obj, query_memdev, &list);
  303. return list;
  304. }