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