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 10 #include "qemu/osdep.h" 11 #include "hw/acpi/vmgenid.h" 12 #include "hw/boards.h" 13 #include "hw/intc/intc.h" 14 #include "hw/mem/memory-device.h" 15 #include "qapi/error.h" 16 #include "qapi/qapi-builtin-visit.h" 17 #include "qapi/qapi-commands-machine.h" 18 #include "qapi/qmp/qobject.h" 19 #include "qapi/qobject-input-visitor.h" 20 #include "qapi/type-helpers.h" 21 #include "qemu/uuid.h" 22 #include "qom/qom-qobject.h" 23 #include "sysemu/hostmem.h" 24 #include "sysemu/hw_accel.h" 25 #include "sysemu/numa.h" 26 #include "sysemu/runstate.h" 27 #include "sysemu/sysemu.h" 28 29 /* 30 * fast means: we NEVER interrupt vCPU threads to retrieve 31 * information from KVM. 32 */ 33 CpuInfoFastList *qmp_query_cpus_fast(Error **errp) 34 { 35 MachineState *ms = MACHINE(qdev_get_machine()); 36 MachineClass *mc = MACHINE_GET_CLASS(ms); 37 CpuInfoFastList *head = NULL, **tail = &head; 38 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), 39 -1, &error_abort); 40 CPUState *cpu; 41 42 CPU_FOREACH(cpu) { 43 CpuInfoFast *value = g_malloc0(sizeof(*value)); 44 45 value->cpu_index = cpu->cpu_index; 46 value->qom_path = object_get_canonical_path(OBJECT(cpu)); 47 value->thread_id = cpu->thread_id; 48 49 if (mc->cpu_index_to_instance_props) { 50 CpuInstanceProperties *props; 51 props = g_malloc0(sizeof(*props)); 52 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index); 53 value->props = props; 54 } 55 56 value->target = target; 57 if (cpu->cc->query_cpu_fast) { 58 cpu->cc->query_cpu_fast(cpu, value); 59 } 60 61 QAPI_LIST_APPEND(tail, value); 62 } 63 64 return head; 65 } 66 67 MachineInfoList *qmp_query_machines(Error **errp) 68 { 69 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false); 70 MachineInfoList *mach_list = NULL; 71 72 for (el = machines; el; el = el->next) { 73 MachineClass *mc = el->data; 74 MachineInfo *info; 75 76 info = g_malloc0(sizeof(*info)); 77 if (mc->is_default) { 78 info->has_is_default = true; 79 info->is_default = true; 80 } 81 82 if (mc->alias) { 83 info->alias = g_strdup(mc->alias); 84 } 85 86 info->name = g_strdup(mc->name); 87 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus; 88 info->hotpluggable_cpus = mc->has_hotpluggable_cpus; 89 info->numa_mem_supported = mc->numa_mem_supported; 90 info->deprecated = !!mc->deprecation_reason; 91 info->acpi = !!object_class_property_find(OBJECT_CLASS(mc), "acpi"); 92 if (mc->default_cpu_type) { 93 info->default_cpu_type = g_strdup(mc->default_cpu_type); 94 } 95 if (mc->default_ram_id) { 96 info->default_ram_id = g_strdup(mc->default_ram_id); 97 } 98 99 QAPI_LIST_PREPEND(mach_list, info); 100 } 101 102 g_slist_free(machines); 103 return mach_list; 104 } 105 106 CurrentMachineParams *qmp_query_current_machine(Error **errp) 107 { 108 CurrentMachineParams *params = g_malloc0(sizeof(*params)); 109 params->wakeup_suspend_support = qemu_wakeup_suspend_enabled(); 110 111 return params; 112 } 113 114 TargetInfo *qmp_query_target(Error **errp) 115 { 116 TargetInfo *info = g_malloc0(sizeof(*info)); 117 118 info->arch = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), -1, 119 &error_abort); 120 121 return info; 122 } 123 124 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp) 125 { 126 MachineState *ms = MACHINE(qdev_get_machine()); 127 MachineClass *mc = MACHINE_GET_CLASS(ms); 128 129 if (!mc->has_hotpluggable_cpus) { 130 error_setg(errp, "machine does not support hot-plugging CPUs"); 131 return NULL; 132 } 133 134 return machine_query_hotpluggable_cpus(ms); 135 } 136 137 void qmp_set_numa_node(NumaOptions *cmd, Error **errp) 138 { 139 if (phase_check(PHASE_MACHINE_INITIALIZED)) { 140 error_setg(errp, "The command is permitted only before the machine has been created"); 141 return; 142 } 143 144 set_numa_options(MACHINE(qdev_get_machine()), cmd, errp); 145 } 146 147 static int query_memdev(Object *obj, void *opaque) 148 { 149 Error *err = NULL; 150 MemdevList **list = opaque; 151 Memdev *m; 152 QObject *host_nodes; 153 Visitor *v; 154 155 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) { 156 m = g_malloc0(sizeof(*m)); 157 158 m->id = g_strdup(object_get_canonical_path_component(obj)); 159 160 m->size = object_property_get_uint(obj, "size", &error_abort); 161 m->merge = object_property_get_bool(obj, "merge", &error_abort); 162 m->dump = object_property_get_bool(obj, "dump", &error_abort); 163 m->prealloc = object_property_get_bool(obj, "prealloc", &error_abort); 164 m->share = object_property_get_bool(obj, "share", &error_abort); 165 m->reserve = object_property_get_bool(obj, "reserve", &err); 166 if (err) { 167 error_free_or_abort(&err); 168 } else { 169 m->has_reserve = true; 170 } 171 m->policy = object_property_get_enum(obj, "policy", "HostMemPolicy", 172 &error_abort); 173 host_nodes = object_property_get_qobject(obj, 174 "host-nodes", 175 &error_abort); 176 v = qobject_input_visitor_new(host_nodes); 177 visit_type_uint16List(v, NULL, &m->host_nodes, &error_abort); 178 visit_free(v); 179 qobject_unref(host_nodes); 180 181 QAPI_LIST_PREPEND(*list, m); 182 } 183 184 return 0; 185 } 186 187 MemdevList *qmp_query_memdev(Error **errp) 188 { 189 Object *obj = object_get_objects_root(); 190 MemdevList *list = NULL; 191 192 object_child_foreach(obj, query_memdev, &list); 193 return list; 194 } 195 196 HumanReadableText *qmp_x_query_numa(Error **errp) 197 { 198 g_autoptr(GString) buf = g_string_new(""); 199 int i, nb_numa_nodes; 200 NumaNodeMem *node_mem; 201 CpuInfoFastList *cpu_list, *cpu; 202 MachineState *ms = MACHINE(qdev_get_machine()); 203 204 nb_numa_nodes = ms->numa_state ? ms->numa_state->num_nodes : 0; 205 g_string_append_printf(buf, "%d nodes\n", nb_numa_nodes); 206 if (!nb_numa_nodes) { 207 goto done; 208 } 209 210 cpu_list = qmp_query_cpus_fast(&error_abort); 211 node_mem = g_new0(NumaNodeMem, nb_numa_nodes); 212 213 query_numa_node_mem(node_mem, ms); 214 for (i = 0; i < nb_numa_nodes; i++) { 215 g_string_append_printf(buf, "node %d cpus:", i); 216 for (cpu = cpu_list; cpu; cpu = cpu->next) { 217 if (cpu->value->props && cpu->value->props->has_node_id && 218 cpu->value->props->node_id == i) { 219 g_string_append_printf(buf, " %" PRIi64, cpu->value->cpu_index); 220 } 221 } 222 g_string_append_printf(buf, "\n"); 223 g_string_append_printf(buf, "node %d size: %" PRId64 " MB\n", i, 224 node_mem[i].node_mem >> 20); 225 g_string_append_printf(buf, "node %d plugged: %" PRId64 " MB\n", i, 226 node_mem[i].node_plugged_mem >> 20); 227 } 228 qapi_free_CpuInfoFastList(cpu_list); 229 g_free(node_mem); 230 231 done: 232 return human_readable_text_from_str(buf); 233 } 234 235 KvmInfo *qmp_query_kvm(Error **errp) 236 { 237 KvmInfo *info = g_malloc0(sizeof(*info)); 238 239 info->enabled = kvm_enabled(); 240 info->present = accel_find("kvm"); 241 242 return info; 243 } 244 245 UuidInfo *qmp_query_uuid(Error **errp) 246 { 247 UuidInfo *info = g_malloc0(sizeof(*info)); 248 249 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid); 250 return info; 251 } 252 253 void qmp_system_reset(Error **errp) 254 { 255 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET); 256 } 257 258 void qmp_system_powerdown(Error **errp) 259 { 260 qemu_system_powerdown_request(); 261 } 262 263 void qmp_system_wakeup(Error **errp) 264 { 265 if (!qemu_wakeup_suspend_enabled()) { 266 error_setg(errp, 267 "wake-up from suspend is not supported by this guest"); 268 return; 269 } 270 271 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp); 272 } 273 274 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp) 275 { 276 return qmp_memory_device_list(); 277 } 278 279 MemoryInfo *qmp_query_memory_size_summary(Error **errp) 280 { 281 MemoryInfo *mem_info = g_new0(MemoryInfo, 1); 282 MachineState *ms = MACHINE(qdev_get_machine()); 283 284 mem_info->base_memory = ms->ram_size; 285 286 mem_info->plugged_memory = get_plugged_memory_size(); 287 mem_info->has_plugged_memory = 288 mem_info->plugged_memory != (uint64_t)-1; 289 290 return mem_info; 291 } 292 293 HumanReadableText *qmp_x_query_ramblock(Error **errp) 294 { 295 g_autoptr(GString) buf = ram_block_format(); 296 297 return human_readable_text_from_str(buf); 298 } 299 300 static int qmp_x_query_irq_foreach(Object *obj, void *opaque) 301 { 302 InterruptStatsProvider *intc; 303 InterruptStatsProviderClass *k; 304 GString *buf = opaque; 305 306 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) { 307 intc = INTERRUPT_STATS_PROVIDER(obj); 308 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj); 309 uint64_t *irq_counts; 310 unsigned int nb_irqs, i; 311 if (k->get_statistics && 312 k->get_statistics(intc, &irq_counts, &nb_irqs)) { 313 if (nb_irqs > 0) { 314 g_string_append_printf(buf, "IRQ statistics for %s:\n", 315 object_get_typename(obj)); 316 for (i = 0; i < nb_irqs; i++) { 317 if (irq_counts[i] > 0) { 318 g_string_append_printf(buf, "%2d: %" PRId64 "\n", i, 319 irq_counts[i]); 320 } 321 } 322 } 323 } else { 324 g_string_append_printf(buf, 325 "IRQ statistics not available for %s.\n", 326 object_get_typename(obj)); 327 } 328 } 329 330 return 0; 331 } 332 333 HumanReadableText *qmp_x_query_irq(Error **errp) 334 { 335 g_autoptr(GString) buf = g_string_new(""); 336 337 object_child_foreach_recursive(object_get_root(), 338 qmp_x_query_irq_foreach, buf); 339 340 return human_readable_text_from_str(buf); 341 } 342 343 GuidInfo *qmp_query_vm_generation_id(Error **errp) 344 { 345 GuidInfo *info; 346 VmGenIdState *vms; 347 Object *obj = find_vmgenid_dev(); 348 349 if (!obj) { 350 error_setg(errp, "VM Generation ID device not found"); 351 return NULL; 352 } 353 vms = VMGENID(obj); 354 355 info = g_malloc0(sizeof(*info)); 356 info->guid = qemu_uuid_unparse_strdup(&vms->guid); 357 return info; 358 } 359