2 * QMP commands related to machines and CPUs
4 * Copyright (C) 2014 Red Hat Inc
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.
10 #include "qemu/osdep.h"
12 #include "hw/boards.h"
13 #include "qapi/error.h"
14 #include "qapi/qapi-commands-machine.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qemu/main-loop.h"
17 #include "sysemu/hostmem.h"
18 #include "sysemu/hw_accel.h"
19 #include "sysemu/numa.h"
20 #include "sysemu/sysemu.h"
22 CpuInfoList *qmp_query_cpus(Error **errp)
24 MachineState *ms = MACHINE(qdev_get_machine());
25 MachineClass *mc = MACHINE_GET_CLASS(ms);
26 CpuInfoList *head = NULL, *cur_item = NULL;
31 #if defined(TARGET_I386)
32 X86CPU *x86_cpu = X86_CPU(cpu);
33 CPUX86State *env = &x86_cpu->env;
34 #elif defined(TARGET_PPC)
35 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
36 CPUPPCState *env = &ppc_cpu->env;
37 #elif defined(TARGET_SPARC)
38 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
39 CPUSPARCState *env = &sparc_cpu->env;
40 #elif defined(TARGET_RISCV)
41 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
42 CPURISCVState *env = &riscv_cpu->env;
43 #elif defined(TARGET_MIPS)
44 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
45 CPUMIPSState *env = &mips_cpu->env;
46 #elif defined(TARGET_TRICORE)
47 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
48 CPUTriCoreState *env = &tricore_cpu->env;
49 #elif defined(TARGET_S390X)
50 S390CPU *s390_cpu = S390_CPU(cpu);
51 CPUS390XState *env = &s390_cpu->env;
54 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;
86 info->value->arch = CPU_INFO_ARCH_OTHER;
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;
96 /* XXX: waiting for the qapi to support GSList */
98 head = cur_item = info;
100 cur_item->next = info;
108 static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
111 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
112 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
115 case SYS_EMU_TARGET_I386:
116 case SYS_EMU_TARGET_X86_64:
117 return CPU_INFO_ARCH_X86;
119 case SYS_EMU_TARGET_PPC:
120 case SYS_EMU_TARGET_PPC64:
121 return CPU_INFO_ARCH_PPC;
123 case SYS_EMU_TARGET_SPARC:
124 case SYS_EMU_TARGET_SPARC64:
125 return CPU_INFO_ARCH_SPARC;
127 case SYS_EMU_TARGET_MIPS:
128 case SYS_EMU_TARGET_MIPSEL:
129 case SYS_EMU_TARGET_MIPS64:
130 case SYS_EMU_TARGET_MIPS64EL:
131 return CPU_INFO_ARCH_MIPS;
133 case SYS_EMU_TARGET_TRICORE:
134 return CPU_INFO_ARCH_TRICORE;
136 case SYS_EMU_TARGET_S390X:
137 return CPU_INFO_ARCH_S390;
139 case SYS_EMU_TARGET_RISCV32:
140 case SYS_EMU_TARGET_RISCV64:
141 return CPU_INFO_ARCH_RISCV;
144 return CPU_INFO_ARCH_OTHER;
148 static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
151 S390CPU *s390_cpu = S390_CPU(cpu);
152 CPUS390XState *env = &s390_cpu->env;
154 info->cpu_state = env->cpu_state;
161 * fast means: we NEVER interrupt vCPU threads to retrieve
162 * information from KVM.
164 CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
166 MachineState *ms = MACHINE(qdev_get_machine());
167 MachineClass *mc = MACHINE_GET_CLASS(ms);
168 CpuInfoFastList *head = NULL, *cur_item = NULL;
169 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
174 CpuInfoFastList *info = g_malloc0(sizeof(*info));
175 info->value = g_malloc0(sizeof(*info->value));
177 info->value->cpu_index = cpu->cpu_index;
178 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
179 info->value->thread_id = cpu->thread_id;
181 info->value->has_props = !!mc->cpu_index_to_instance_props;
182 if (info->value->has_props) {
183 CpuInstanceProperties *props;
184 props = g_malloc0(sizeof(*props));
185 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
186 info->value->props = props;
189 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
190 info->value->target = target;
191 if (target == SYS_EMU_TARGET_S390X) {
192 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
196 head = cur_item = info;
198 cur_item->next = info;
206 MachineInfoList *qmp_query_machines(Error **errp)
208 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
209 MachineInfoList *mach_list = NULL;
211 for (el = machines; el; el = el->next) {
212 MachineClass *mc = el->data;
213 MachineInfoList *entry;
216 info = g_malloc0(sizeof(*info));
217 if (mc->is_default) {
218 info->has_is_default = true;
219 info->is_default = true;
223 info->has_alias = true;
224 info->alias = g_strdup(mc->alias);
227 info->name = g_strdup(mc->name);
228 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
229 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
230 info->numa_mem_supported = mc->numa_mem_supported;
231 info->deprecated = !!mc->deprecation_reason;
233 entry = g_malloc0(sizeof(*entry));
235 entry->next = mach_list;
239 g_slist_free(machines);
243 CurrentMachineParams *qmp_query_current_machine(Error **errp)
245 CurrentMachineParams *params = g_malloc0(sizeof(*params));
246 params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
251 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
253 MachineState *ms = MACHINE(qdev_get_machine());
254 MachineClass *mc = MACHINE_GET_CLASS(ms);
256 if (!mc->has_hotpluggable_cpus) {
257 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
261 return machine_query_hotpluggable_cpus(ms);
264 void qmp_cpu_add(int64_t id, Error **errp)
268 mc = MACHINE_GET_CLASS(current_machine);
269 if (mc->hot_add_cpu) {
270 mc->hot_add_cpu(current_machine, id, errp);
272 error_setg(errp, "Not supported");
276 void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
278 if (!runstate_check(RUN_STATE_PRECONFIG)) {
279 error_setg(errp, "The command is permitted only in '%s' state",
280 RunState_str(RUN_STATE_PRECONFIG));
284 set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
287 static int query_memdev(Object *obj, void *opaque)
289 MemdevList **list = opaque;
290 MemdevList *m = NULL;
292 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
293 m = g_malloc0(sizeof(*m));
295 m->value = g_malloc0(sizeof(*m->value));
297 m->value->id = object_get_canonical_path_component(obj);
298 m->value->has_id = !!m->value->id;
300 m->value->size = object_property_get_uint(obj, "size",
302 m->value->merge = object_property_get_bool(obj, "merge",
304 m->value->dump = object_property_get_bool(obj, "dump",
306 m->value->prealloc = object_property_get_bool(obj,
309 m->value->policy = object_property_get_enum(obj,
313 object_property_get_uint16List(obj, "host-nodes",
314 &m->value->host_nodes,
324 MemdevList *qmp_query_memdev(Error **errp)
326 Object *obj = object_get_objects_root();
327 MemdevList *list = NULL;
329 object_child_foreach(obj, query_memdev, &list);