| 1 | /* |
| 2 | * CPU models for s390x - System-only |
| 3 | * |
| 4 | * Copyright 2016 IBM Corp. |
| 5 | * |
| 6 | * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com> |
| 7 | * |
| 8 | * This work is licensed under the terms of the GNU GPL, version 2 or (at |
| 9 | * your option) any later version. See the COPYING file in the top-level |
| 10 | * directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "cpu.h" |
| 15 | #include "s390x-internal.h" |
| 16 | #include "kvm/kvm_s390x.h" |
| 17 | #include "system/kvm.h" |
| 18 | #include "qapi/error.h" |
| 19 | #include "qapi/visitor.h" |
| 20 | #include "qapi/qobject-input-visitor.h" |
| 21 | #include "qobject/qdict.h" |
| 22 | #include "qapi/qapi-commands-machine.h" |
| 23 | |
| 24 | static void list_add_feat(const char *name, void *opaque); |
| 25 | |
| 26 | static void check_unavailable_features(const S390CPUModel *max_model, |
| 27 | const S390CPUModel *model, |
| 28 | strList **unavailable) |
| 29 | { |
| 30 | S390FeatBitmap missing; |
| 31 | |
| 32 | /* check general model compatibility */ |
| 33 | if (max_model->def->gen < model->def->gen || |
| 34 | (max_model->def->gen == model->def->gen && |
| 35 | max_model->def->ec_ga < model->def->ec_ga)) { |
| 36 | list_add_feat("type", unavailable); |
| 37 | } |
| 38 | |
| 39 | /* detect missing features if any to properly report them */ |
| 40 | bitmap_andnot(missing, model->features, max_model->features, |
| 41 | S390_FEAT_MAX); |
| 42 | if (!bitmap_empty(missing, S390_FEAT_MAX)) { |
| 43 | s390_feat_bitmap_to_ascii(missing, unavailable, list_add_feat); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | struct CpuDefinitionInfoListData { |
| 48 | CpuDefinitionInfoList *list; |
| 49 | S390CPUModel *model; |
| 50 | }; |
| 51 | |
| 52 | static void create_cpu_model_list(ObjectClass *klass, void *opaque) |
| 53 | { |
| 54 | struct CpuDefinitionInfoListData *cpu_list_data = opaque; |
| 55 | CpuDefinitionInfoList **cpu_list = &cpu_list_data->list; |
| 56 | CpuDefinitionInfo *info; |
| 57 | char *name = g_strdup(object_class_get_name(klass)); |
| 58 | S390CPUClass *scc = S390_CPU_CLASS(klass); |
| 59 | |
| 60 | /* strip off the -s390x-cpu */ |
| 61 | g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0; |
| 62 | info = g_new0(CpuDefinitionInfo, 1); |
| 63 | info->name = name; |
| 64 | info->has_migration_safe = true; |
| 65 | info->migration_safe = scc->is_migration_safe; |
| 66 | info->q_static = scc->is_static; |
| 67 | info->q_typename = g_strdup(object_class_get_name(klass)); |
| 68 | /* check for unavailable features */ |
| 69 | if (cpu_list_data->model) { |
| 70 | Object *obj; |
| 71 | S390CPU *sc; |
| 72 | obj = object_new_with_class(klass); |
| 73 | sc = S390_CPU(obj); |
| 74 | if (sc->model) { |
| 75 | info->has_unavailable_features = true; |
| 76 | check_unavailable_features(cpu_list_data->model, sc->model, |
| 77 | &info->unavailable_features); |
| 78 | } |
| 79 | object_unref(obj); |
| 80 | } |
| 81 | |
| 82 | QAPI_LIST_PREPEND(*cpu_list, info); |
| 83 | } |
| 84 | |
| 85 | CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp) |
| 86 | { |
| 87 | struct CpuDefinitionInfoListData list_data = { |
| 88 | .list = NULL, |
| 89 | }; |
| 90 | |
| 91 | list_data.model = get_max_cpu_model(NULL); |
| 92 | |
| 93 | object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false, |
| 94 | &list_data); |
| 95 | |
| 96 | return list_data.list; |
| 97 | } |
| 98 | |
| 99 | static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info, |
| 100 | const char *info_arg_name, Error **errp) |
| 101 | { |
| 102 | Error *err = NULL; |
| 103 | const QDict *qdict; |
| 104 | const QDictEntry *e; |
| 105 | Visitor *visitor; |
| 106 | ObjectClass *oc; |
| 107 | S390CPU *cpu; |
| 108 | Object *obj; |
| 109 | |
| 110 | oc = cpu_class_by_name(TYPE_S390_CPU, info->name); |
| 111 | if (!oc) { |
| 112 | error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name); |
| 113 | return; |
| 114 | } |
| 115 | if (S390_CPU_CLASS(oc)->kvm_required && !kvm_enabled()) { |
| 116 | error_setg(errp, "The CPU definition '%s' requires KVM", info->name); |
| 117 | return; |
| 118 | } |
| 119 | obj = object_new_with_class(oc); |
| 120 | cpu = S390_CPU(obj); |
| 121 | |
| 122 | if (!cpu->model) { |
| 123 | error_setg(errp, "Details about the host CPU model are not available, " |
| 124 | "it cannot be used."); |
| 125 | object_unref(obj); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | if (info->props) { |
| 130 | g_autofree const char *props_name = g_strdup_printf("%s.props", |
| 131 | info_arg_name); |
| 132 | |
| 133 | visitor = qobject_input_visitor_new(info->props); |
| 134 | if (!visit_start_struct(visitor, props_name, NULL, 0, errp)) { |
| 135 | visit_free(visitor); |
| 136 | object_unref(obj); |
| 137 | return; |
| 138 | } |
| 139 | qdict = qobject_to(QDict, info->props); |
| 140 | for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) { |
| 141 | if (!object_property_set(obj, e->key, visitor, &err)) { |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | if (!err) { |
| 146 | visit_check_struct(visitor, &err); |
| 147 | } |
| 148 | visit_end_struct(visitor, NULL); |
| 149 | visit_free(visitor); |
| 150 | if (err) { |
| 151 | error_propagate(errp, err); |
| 152 | object_unref(obj); |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* copy the model and throw the cpu away */ |
| 158 | memcpy(model, cpu->model, sizeof(*model)); |
| 159 | object_unref(obj); |
| 160 | } |
| 161 | |
| 162 | static void qdict_add_disabled_feat(const char *name, void *opaque) |
| 163 | { |
| 164 | qdict_put_bool(opaque, name, false); |
| 165 | } |
| 166 | |
| 167 | static void qdict_add_enabled_feat(const char *name, void *opaque) |
| 168 | { |
| 169 | qdict_put_bool(opaque, name, true); |
| 170 | } |
| 171 | |
| 172 | /* convert S390CPUDef into a static CpuModelInfo */ |
| 173 | static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model, |
| 174 | bool delta_changes) |
| 175 | { |
| 176 | QDict *qdict = qdict_new(); |
| 177 | S390FeatBitmap bitmap; |
| 178 | |
| 179 | /* always fallback to the static base model */ |
| 180 | info->name = g_strdup_printf("%s-base", model->def->name); |
| 181 | |
| 182 | if (delta_changes) { |
| 183 | /* features deleted from the base feature set */ |
| 184 | bitmap_andnot(bitmap, model->def->base_feat, model->features, |
| 185 | S390_FEAT_MAX); |
| 186 | if (!bitmap_empty(bitmap, S390_FEAT_MAX)) { |
| 187 | s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat); |
| 188 | } |
| 189 | |
| 190 | /* features added to the base feature set */ |
| 191 | bitmap_andnot(bitmap, model->features, model->def->base_feat, |
| 192 | S390_FEAT_MAX); |
| 193 | if (!bitmap_empty(bitmap, S390_FEAT_MAX)) { |
| 194 | s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_enabled_feat); |
| 195 | } |
| 196 | } else { |
| 197 | /* expand all features */ |
| 198 | s390_feat_bitmap_to_ascii(model->features, qdict, |
| 199 | qdict_add_enabled_feat); |
| 200 | bitmap_complement(bitmap, model->features, S390_FEAT_MAX); |
| 201 | s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat); |
| 202 | } |
| 203 | |
| 204 | if (!qdict_size(qdict)) { |
| 205 | qobject_unref(qdict); |
| 206 | } else { |
| 207 | info->props = QOBJECT(qdict); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type, |
| 212 | CpuModelInfo *model, |
| 213 | Error **errp) |
| 214 | { |
| 215 | Error *err = NULL; |
| 216 | CpuModelExpansionInfo *expansion_info = NULL; |
| 217 | S390CPUModel s390_model; |
| 218 | bool delta_changes = false; |
| 219 | S390FeatBitmap deprecated_feats; |
| 220 | |
| 221 | /* convert it to our internal representation */ |
| 222 | cpu_model_from_info(&s390_model, model, "model", &err); |
| 223 | if (err) { |
| 224 | error_propagate(errp, err); |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | if (type == CPU_MODEL_EXPANSION_TYPE_STATIC) { |
| 229 | delta_changes = true; |
| 230 | } else if (type != CPU_MODEL_EXPANSION_TYPE_FULL) { |
| 231 | error_setg(errp, "The requested expansion type is not supported."); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | /* convert it back to a static representation */ |
| 236 | expansion_info = g_new0(CpuModelExpansionInfo, 1); |
| 237 | expansion_info->model = g_malloc0(sizeof(*expansion_info->model)); |
| 238 | cpu_info_from_model(expansion_info->model, &s390_model, delta_changes); |
| 239 | |
| 240 | /* populate list of deprecated features */ |
| 241 | bitmap_zero(deprecated_feats, S390_FEAT_MAX); |
| 242 | s390_get_deprecated_features(deprecated_feats); |
| 243 | |
| 244 | if (delta_changes) { |
| 245 | /* |
| 246 | * Only populate deprecated features that are a |
| 247 | * subset of the features enabled on the CPU model. |
| 248 | */ |
| 249 | bitmap_and(deprecated_feats, deprecated_feats, |
| 250 | s390_model.features, S390_FEAT_MAX); |
| 251 | } |
| 252 | |
| 253 | s390_feat_bitmap_to_ascii(deprecated_feats, |
| 254 | &expansion_info->deprecated_props, list_add_feat); |
| 255 | |
| 256 | expansion_info->has_deprecated_props = !!expansion_info->deprecated_props; |
| 257 | |
| 258 | return expansion_info; |
| 259 | } |
| 260 | |
| 261 | static void list_add_feat(const char *name, void *opaque) |
| 262 | { |
| 263 | strList **last = (strList **) opaque; |
| 264 | |
| 265 | QAPI_LIST_PREPEND(*last, g_strdup(name)); |
| 266 | } |
| 267 | |
| 268 | CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa, |
| 269 | CpuModelInfo *infob, |
| 270 | Error **errp) |
| 271 | { |
| 272 | Error *err = NULL; |
| 273 | CpuModelCompareResult feat_result, gen_result; |
| 274 | CpuModelCompareInfo *compare_info; |
| 275 | S390FeatBitmap missing, added; |
| 276 | S390CPUModel modela, modelb; |
| 277 | |
| 278 | /* convert both models to our internal representation */ |
| 279 | cpu_model_from_info(&modela, infoa, "modela", &err); |
| 280 | if (err) { |
| 281 | error_propagate(errp, err); |
| 282 | return NULL; |
| 283 | } |
| 284 | cpu_model_from_info(&modelb, infob, "modelb", &err); |
| 285 | if (err) { |
| 286 | error_propagate(errp, err); |
| 287 | return NULL; |
| 288 | } |
| 289 | compare_info = g_new0(CpuModelCompareInfo, 1); |
| 290 | |
| 291 | /* check the cpu generation and ga level */ |
| 292 | if (modela.def->gen == modelb.def->gen) { |
| 293 | if (modela.def->ec_ga == modelb.def->ec_ga) { |
| 294 | /* ec and corresponding bc are identical */ |
| 295 | gen_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL; |
| 296 | } else if (modela.def->ec_ga < modelb.def->ec_ga) { |
| 297 | gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET; |
| 298 | } else { |
| 299 | gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET; |
| 300 | } |
| 301 | } else if (modela.def->gen < modelb.def->gen) { |
| 302 | gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET; |
| 303 | } else { |
| 304 | gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET; |
| 305 | } |
| 306 | if (gen_result != CPU_MODEL_COMPARE_RESULT_IDENTICAL) { |
| 307 | /* both models cannot be made identical */ |
| 308 | list_add_feat("type", &compare_info->responsible_properties); |
| 309 | } |
| 310 | |
| 311 | /* check the feature set */ |
| 312 | if (bitmap_equal(modela.features, modelb.features, S390_FEAT_MAX)) { |
| 313 | feat_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL; |
| 314 | } else { |
| 315 | bitmap_andnot(missing, modela.features, modelb.features, S390_FEAT_MAX); |
| 316 | s390_feat_bitmap_to_ascii(missing, |
| 317 | &compare_info->responsible_properties, |
| 318 | list_add_feat); |
| 319 | bitmap_andnot(added, modelb.features, modela.features, S390_FEAT_MAX); |
| 320 | s390_feat_bitmap_to_ascii(added, &compare_info->responsible_properties, |
| 321 | list_add_feat); |
| 322 | if (bitmap_empty(missing, S390_FEAT_MAX)) { |
| 323 | feat_result = CPU_MODEL_COMPARE_RESULT_SUBSET; |
| 324 | } else if (bitmap_empty(added, S390_FEAT_MAX)) { |
| 325 | feat_result = CPU_MODEL_COMPARE_RESULT_SUPERSET; |
| 326 | } else { |
| 327 | feat_result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* combine the results */ |
| 332 | if (gen_result == feat_result) { |
| 333 | compare_info->result = gen_result; |
| 334 | } else if (feat_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) { |
| 335 | compare_info->result = gen_result; |
| 336 | } else if (gen_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) { |
| 337 | compare_info->result = feat_result; |
| 338 | } else { |
| 339 | compare_info->result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE; |
| 340 | } |
| 341 | return compare_info; |
| 342 | } |
| 343 | |
| 344 | CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa, |
| 345 | CpuModelInfo *infob, |
| 346 | Error **errp) |
| 347 | { |
| 348 | Error *err = NULL; |
| 349 | CpuModelBaselineInfo *baseline_info; |
| 350 | S390CPUModel modela, modelb, model; |
| 351 | uint16_t cpu_type; |
| 352 | uint8_t max_gen_ga; |
| 353 | uint8_t max_gen; |
| 354 | |
| 355 | /* convert both models to our internal representation */ |
| 356 | cpu_model_from_info(&modela, infoa, "modela", &err); |
| 357 | if (err) { |
| 358 | error_propagate(errp, err); |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | cpu_model_from_info(&modelb, infob, "modelb", &err); |
| 363 | if (err) { |
| 364 | error_propagate(errp, err); |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | /* features both models support */ |
| 369 | bitmap_and(model.features, modela.features, modelb.features, S390_FEAT_MAX); |
| 370 | |
| 371 | /* detect the maximum model not regarding features */ |
| 372 | if (modela.def->gen == modelb.def->gen) { |
| 373 | if (modela.def->type == modelb.def->type) { |
| 374 | cpu_type = modela.def->type; |
| 375 | } else { |
| 376 | cpu_type = 0; |
| 377 | } |
| 378 | max_gen = modela.def->gen; |
| 379 | max_gen_ga = MIN(modela.def->ec_ga, modelb.def->ec_ga); |
| 380 | } else if (modela.def->gen > modelb.def->gen) { |
| 381 | cpu_type = modelb.def->type; |
| 382 | max_gen = modelb.def->gen; |
| 383 | max_gen_ga = modelb.def->ec_ga; |
| 384 | } else { |
| 385 | cpu_type = modela.def->type; |
| 386 | max_gen = modela.def->gen; |
| 387 | max_gen_ga = modela.def->ec_ga; |
| 388 | } |
| 389 | |
| 390 | model.def = s390_find_cpu_def(cpu_type, max_gen, max_gen_ga, |
| 391 | model.features); |
| 392 | |
| 393 | /* models without early base features (esan3) are bad */ |
| 394 | if (!model.def) { |
| 395 | error_setg(errp, "No compatible CPU model could be created as" |
| 396 | " important base features are disabled"); |
| 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | /* strip off features not part of the max model */ |
| 401 | bitmap_and(model.features, model.features, model.def->full_feat, |
| 402 | S390_FEAT_MAX); |
| 403 | |
| 404 | baseline_info = g_new0(CpuModelBaselineInfo, 1); |
| 405 | baseline_info->model = g_malloc0(sizeof(*baseline_info->model)); |
| 406 | cpu_info_from_model(baseline_info->model, &model, true); |
| 407 | return baseline_info; |
| 408 | } |
| 409 | |
| 410 | void apply_cpu_model(const S390CPUModel *model, Error **errp) |
| 411 | { |
| 412 | static S390CPUModel applied_model; |
| 413 | static bool applied; |
| 414 | |
| 415 | /* |
| 416 | * We have the same model for all VCPUs. KVM can only be configured before |
| 417 | * any VCPUs are defined in KVM. |
| 418 | */ |
| 419 | if (applied) { |
| 420 | if (model && memcmp(&applied_model, model, sizeof(S390CPUModel))) { |
| 421 | error_setg(errp, "Mixed CPU models are not supported on s390x."); |
| 422 | } |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | if (kvm_enabled()) { |
| 427 | if (!kvm_s390_apply_cpu_model(model, errp)) { |
| 428 | return; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | applied = true; |
| 433 | if (model) { |
| 434 | applied_model = *model; |
| 435 | } |
| 436 | } |