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-builtin-visit.h"
15 #include "qapi/qapi-commands-machine.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qapi/qmp/qobject.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "qemu/main-loop.h"
20 #include "qom/qom-qobject.h"
21 #include "sysemu/hostmem.h"
22 #include "sysemu/hw_accel.h"
23 #include "sysemu/numa.h"
24 #include "sysemu/runstate.h"
25 #include "sysemu/sysemu.h"
27 CpuInfoList
*qmp_query_cpus(Error
**errp
)
29 MachineState
*ms
= MACHINE(qdev_get_machine());
30 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
31 CpuInfoList
*head
= NULL
, *cur_item
= NULL
;
36 #if defined(TARGET_I386)
37 X86CPU
*x86_cpu
= X86_CPU(cpu
);
38 CPUX86State
*env
= &x86_cpu
->env
;
39 #elif defined(TARGET_PPC)
40 PowerPCCPU
*ppc_cpu
= POWERPC_CPU(cpu
);
41 CPUPPCState
*env
= &ppc_cpu
->env
;
42 #elif defined(TARGET_SPARC)
43 SPARCCPU
*sparc_cpu
= SPARC_CPU(cpu
);
44 CPUSPARCState
*env
= &sparc_cpu
->env
;
45 #elif defined(TARGET_RISCV)
46 RISCVCPU
*riscv_cpu
= RISCV_CPU(cpu
);
47 CPURISCVState
*env
= &riscv_cpu
->env
;
48 #elif defined(TARGET_MIPS)
49 MIPSCPU
*mips_cpu
= MIPS_CPU(cpu
);
50 CPUMIPSState
*env
= &mips_cpu
->env
;
51 #elif defined(TARGET_TRICORE)
52 TriCoreCPU
*tricore_cpu
= TRICORE_CPU(cpu
);
53 CPUTriCoreState
*env
= &tricore_cpu
->env
;
54 #elif defined(TARGET_S390X)
55 S390CPU
*s390_cpu
= S390_CPU(cpu
);
56 CPUS390XState
*env
= &s390_cpu
->env
;
59 cpu_synchronize_state(cpu
);
61 info
= g_malloc0(sizeof(*info
));
62 info
->value
= g_malloc0(sizeof(*info
->value
));
63 info
->value
->CPU
= cpu
->cpu_index
;
64 info
->value
->current
= (cpu
== first_cpu
);
65 info
->value
->halted
= cpu
->halted
;
66 info
->value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
67 info
->value
->thread_id
= cpu
->thread_id
;
68 #if defined(TARGET_I386)
69 info
->value
->arch
= CPU_INFO_ARCH_X86
;
70 info
->value
->u
.x86
.pc
= env
->eip
+ env
->segs
[R_CS
].base
;
71 #elif defined(TARGET_PPC)
72 info
->value
->arch
= CPU_INFO_ARCH_PPC
;
73 info
->value
->u
.ppc
.nip
= env
->nip
;
74 #elif defined(TARGET_SPARC)
75 info
->value
->arch
= CPU_INFO_ARCH_SPARC
;
76 info
->value
->u
.q_sparc
.pc
= env
->pc
;
77 info
->value
->u
.q_sparc
.npc
= env
->npc
;
78 #elif defined(TARGET_MIPS)
79 info
->value
->arch
= CPU_INFO_ARCH_MIPS
;
80 info
->value
->u
.q_mips
.PC
= env
->active_tc
.PC
;
81 #elif defined(TARGET_TRICORE)
82 info
->value
->arch
= CPU_INFO_ARCH_TRICORE
;
83 info
->value
->u
.tricore
.PC
= env
->PC
;
84 #elif defined(TARGET_S390X)
85 info
->value
->arch
= CPU_INFO_ARCH_S390
;
86 info
->value
->u
.s390
.cpu_state
= env
->cpu_state
;
87 #elif defined(TARGET_RISCV)
88 info
->value
->arch
= CPU_INFO_ARCH_RISCV
;
89 info
->value
->u
.riscv
.pc
= env
->pc
;
91 info
->value
->arch
= CPU_INFO_ARCH_OTHER
;
93 info
->value
->has_props
= !!mc
->cpu_index_to_instance_props
;
94 if (info
->value
->has_props
) {
95 CpuInstanceProperties
*props
;
96 props
= g_malloc0(sizeof(*props
));
97 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
98 info
->value
->props
= props
;
101 /* XXX: waiting for the qapi to support GSList */
103 head
= cur_item
= info
;
105 cur_item
->next
= info
;
113 static CpuInfoArch
sysemu_target_to_cpuinfo_arch(SysEmuTarget target
)
116 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
117 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
120 case SYS_EMU_TARGET_I386
:
121 case SYS_EMU_TARGET_X86_64
:
122 return CPU_INFO_ARCH_X86
;
124 case SYS_EMU_TARGET_PPC
:
125 case SYS_EMU_TARGET_PPC64
:
126 return CPU_INFO_ARCH_PPC
;
128 case SYS_EMU_TARGET_SPARC
:
129 case SYS_EMU_TARGET_SPARC64
:
130 return CPU_INFO_ARCH_SPARC
;
132 case SYS_EMU_TARGET_MIPS
:
133 case SYS_EMU_TARGET_MIPSEL
:
134 case SYS_EMU_TARGET_MIPS64
:
135 case SYS_EMU_TARGET_MIPS64EL
:
136 return CPU_INFO_ARCH_MIPS
;
138 case SYS_EMU_TARGET_TRICORE
:
139 return CPU_INFO_ARCH_TRICORE
;
141 case SYS_EMU_TARGET_S390X
:
142 return CPU_INFO_ARCH_S390
;
144 case SYS_EMU_TARGET_RISCV32
:
145 case SYS_EMU_TARGET_RISCV64
:
146 return CPU_INFO_ARCH_RISCV
;
149 return CPU_INFO_ARCH_OTHER
;
153 static void cpustate_to_cpuinfo_s390(CpuInfoS390
*info
, const CPUState
*cpu
)
156 S390CPU
*s390_cpu
= S390_CPU(cpu
);
157 CPUS390XState
*env
= &s390_cpu
->env
;
159 info
->cpu_state
= env
->cpu_state
;
166 * fast means: we NEVER interrupt vCPU threads to retrieve
167 * information from KVM.
169 CpuInfoFastList
*qmp_query_cpus_fast(Error
**errp
)
171 MachineState
*ms
= MACHINE(qdev_get_machine());
172 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
173 CpuInfoFastList
*head
= NULL
, *cur_item
= NULL
;
174 SysEmuTarget target
= qapi_enum_parse(&SysEmuTarget_lookup
, TARGET_NAME
,
179 CpuInfoFastList
*info
= g_malloc0(sizeof(*info
));
180 info
->value
= g_malloc0(sizeof(*info
->value
));
182 info
->value
->cpu_index
= cpu
->cpu_index
;
183 info
->value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
184 info
->value
->thread_id
= cpu
->thread_id
;
186 info
->value
->has_props
= !!mc
->cpu_index_to_instance_props
;
187 if (info
->value
->has_props
) {
188 CpuInstanceProperties
*props
;
189 props
= g_malloc0(sizeof(*props
));
190 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
191 info
->value
->props
= props
;
194 info
->value
->arch
= sysemu_target_to_cpuinfo_arch(target
);
195 info
->value
->target
= target
;
196 if (target
== SYS_EMU_TARGET_S390X
) {
197 cpustate_to_cpuinfo_s390(&info
->value
->u
.s390x
, cpu
);
201 head
= cur_item
= info
;
203 cur_item
->next
= info
;
211 MachineInfoList
*qmp_query_machines(Error
**errp
)
213 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
214 MachineInfoList
*mach_list
= NULL
;
216 for (el
= machines
; el
; el
= el
->next
) {
217 MachineClass
*mc
= el
->data
;
218 MachineInfoList
*entry
;
221 info
= g_malloc0(sizeof(*info
));
222 if (mc
->is_default
) {
223 info
->has_is_default
= true;
224 info
->is_default
= true;
228 info
->has_alias
= true;
229 info
->alias
= g_strdup(mc
->alias
);
232 info
->name
= g_strdup(mc
->name
);
233 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
234 info
->hotpluggable_cpus
= mc
->has_hotpluggable_cpus
;
235 info
->numa_mem_supported
= mc
->numa_mem_supported
;
236 info
->deprecated
= !!mc
->deprecation_reason
;
237 if (mc
->default_cpu_type
) {
238 info
->default_cpu_type
= g_strdup(mc
->default_cpu_type
);
239 info
->has_default_cpu_type
= true;
242 entry
= g_malloc0(sizeof(*entry
));
244 entry
->next
= mach_list
;
248 g_slist_free(machines
);
252 CurrentMachineParams
*qmp_query_current_machine(Error
**errp
)
254 CurrentMachineParams
*params
= g_malloc0(sizeof(*params
));
255 params
->wakeup_suspend_support
= qemu_wakeup_suspend_enabled();
260 TargetInfo
*qmp_query_target(Error
**errp
)
262 TargetInfo
*info
= g_malloc0(sizeof(*info
));
264 info
->arch
= qapi_enum_parse(&SysEmuTarget_lookup
, TARGET_NAME
, -1,
270 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
272 MachineState
*ms
= MACHINE(qdev_get_machine());
273 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
275 if (!mc
->has_hotpluggable_cpus
) {
276 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
280 return machine_query_hotpluggable_cpus(ms
);
283 void qmp_cpu_add(int64_t id
, Error
**errp
)
287 mc
= MACHINE_GET_CLASS(current_machine
);
288 if (mc
->hot_add_cpu
) {
289 mc
->hot_add_cpu(current_machine
, id
, errp
);
291 error_setg(errp
, "Not supported");
295 void qmp_set_numa_node(NumaOptions
*cmd
, Error
**errp
)
297 if (!runstate_check(RUN_STATE_PRECONFIG
)) {
298 error_setg(errp
, "The command is permitted only in '%s' state",
299 RunState_str(RUN_STATE_PRECONFIG
));
303 set_numa_options(MACHINE(qdev_get_machine()), cmd
, errp
);
306 static int query_memdev(Object
*obj
, void *opaque
)
308 MemdevList
**list
= opaque
;
309 MemdevList
*m
= NULL
;
313 if (object_dynamic_cast(obj
, TYPE_MEMORY_BACKEND
)) {
314 m
= g_malloc0(sizeof(*m
));
316 m
->value
= g_malloc0(sizeof(*m
->value
));
318 m
->value
->id
= object_get_canonical_path_component(obj
);
319 m
->value
->has_id
= !!m
->value
->id
;
321 m
->value
->size
= object_property_get_uint(obj
, "size",
323 m
->value
->merge
= object_property_get_bool(obj
, "merge",
325 m
->value
->dump
= object_property_get_bool(obj
, "dump",
327 m
->value
->prealloc
= object_property_get_bool(obj
,
330 m
->value
->policy
= object_property_get_enum(obj
,
334 host_nodes
= object_property_get_qobject(obj
,
337 v
= qobject_input_visitor_new(host_nodes
);
338 visit_type_uint16List(v
, NULL
, &m
->value
->host_nodes
, &error_abort
);
340 qobject_unref(host_nodes
);
349 MemdevList
*qmp_query_memdev(Error
**errp
)
351 Object
*obj
= object_get_objects_root();
352 MemdevList
*list
= NULL
;
354 object_child_foreach(obj
, query_memdev
, &list
);