xref: /qemu/target/s390x/cpu_models.c (revision f917eed3)
1 /*
2  * CPU models for s390x
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 "internal.h"
16 #include "kvm_s390x.h"
17 #include "sysemu/kvm.h"
18 #include "sysemu/tcg.h"
19 #include "qapi/error.h"
20 #include "qapi/visitor.h"
21 #include "qemu/error-report.h"
22 #include "qemu/module.h"
23 #include "qemu/qemu-print.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qobject-input-visitor.h"
26 #include "qapi/qmp/qdict.h"
27 #ifndef CONFIG_USER_ONLY
28 #include "sysemu/arch_init.h"
29 #include "hw/pci/pci.h"
30 #endif
31 #include "qapi/qapi-commands-machine-target.h"
32 #include "hw/s390x/pv.h"
33 
34 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
35     {                                                                    \
36         .name = _name,                                                   \
37         .type = _type,                                                   \
38         .gen = _gen,                                                     \
39         .ec_ga = _ec_ga,                                                 \
40         .mha_pow = _mha_pow,                                             \
41         .hmfai = _hmfai,                                                 \
42         .desc = _desc,                                                   \
43         .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE },  \
44         .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT },  \
45         .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL },  \
46     }
47 
48 /*
49  * CPU definition list in order of release. Up to generation 14 base features
50  * of a following release have been a superset of the previous release. With
51  * generation 15 one base feature and one optional feature have been deprecated.
52  */
53 static S390CPUDef s390_cpu_defs[] = {
54     CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"),
55     CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"),
56     CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"),
57     CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"),
58     CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"),
59     CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"),
60     CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"),
61     CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"),
62     CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"),
63     CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"),
64     CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"),
65     CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"),
66     CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"),
67     CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"),
68     CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"),
69     CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"),
70     CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"),
71     CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"),
72     CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"),
73     CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"),
74     CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"),
75     CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"),
76     CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"),
77     CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"),
78     CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"),
79     CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"),
80     CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"),
81     CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"),
82     CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"),
83     CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"),
84     CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"),
85     CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"),
86     CPUDEF_INIT(0x3906, 14, 2, 47, 0x08000000U, "z14.2", "IBM z14 GA2"),
87     CPUDEF_INIT(0x3907, 14, 1, 47, 0x08000000U, "z14ZR1", "IBM z14 Model ZR1 GA1"),
88     CPUDEF_INIT(0x8561, 15, 1, 47, 0x08000000U, "gen15a", "IBM z15 GA1"),
89     CPUDEF_INIT(0x8562, 15, 1, 47, 0x08000000U, "gen15b", "IBM 8562 GA1"),
90 };
91 
92 #define QEMU_MAX_CPU_TYPE 0x2964
93 #define QEMU_MAX_CPU_GEN 13
94 #define QEMU_MAX_CPU_EC_GA 2
95 static const S390FeatInit qemu_max_cpu_feat_init = { S390_FEAT_LIST_QEMU_MAX };
96 static S390FeatBitmap qemu_max_cpu_feat;
97 
98 /* features part of a base model but not relevant for finding a base model */
99 S390FeatBitmap ignored_base_feat;
100 
101 void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat)
102 {
103     const S390CPUDef *def;
104 
105     def = s390_find_cpu_def(0, gen, ec_ga, NULL);
106     clear_bit(feat, (unsigned long *)&def->default_feat);
107 }
108 
109 void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat)
110 {
111     int i;
112 
113     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
114         const S390CPUDef *def = &s390_cpu_defs[i];
115 
116         if (def->gen < gen) {
117             continue;
118         }
119         if (def->gen == gen && def->ec_ga < ec_ga) {
120             continue;
121         }
122 
123         clear_bit(feat, (unsigned long *)&def->default_feat);
124     }
125 }
126 
127 void s390_cpudef_group_featoff_greater(uint8_t gen, uint8_t ec_ga,
128                                        S390FeatGroup group)
129 {
130     const S390FeatGroupDef *group_def = s390_feat_group_def(group);
131     S390FeatBitmap group_def_off;
132     int i;
133 
134     bitmap_complement(group_def_off, group_def->feat, S390_FEAT_MAX);
135 
136     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
137         const S390CPUDef *cpu_def = &s390_cpu_defs[i];
138 
139         if (cpu_def->gen < gen) {
140             continue;
141         }
142         if (cpu_def->gen == gen && cpu_def->ec_ga < ec_ga) {
143             continue;
144         }
145 
146         bitmap_and((unsigned long *)&cpu_def->default_feat,
147                    cpu_def->default_feat, group_def_off, S390_FEAT_MAX);
148     }
149 }
150 
151 uint32_t s390_get_hmfai(void)
152 {
153     static S390CPU *cpu;
154 
155     if (!cpu) {
156         cpu = S390_CPU(qemu_get_cpu(0));
157     }
158 
159     if (!cpu || !cpu->model) {
160         return 0;
161     }
162     return cpu->model->def->hmfai;
163 }
164 
165 uint8_t s390_get_mha_pow(void)
166 {
167     static S390CPU *cpu;
168 
169     if (!cpu) {
170         cpu = S390_CPU(qemu_get_cpu(0));
171     }
172 
173     if (!cpu || !cpu->model) {
174         return 0;
175     }
176     return cpu->model->def->mha_pow;
177 }
178 
179 uint32_t s390_get_ibc_val(void)
180 {
181     uint16_t unblocked_ibc, lowest_ibc;
182     static S390CPU *cpu;
183 
184     if (!cpu) {
185         cpu = S390_CPU(qemu_get_cpu(0));
186     }
187 
188     if (!cpu || !cpu->model) {
189         return 0;
190     }
191     unblocked_ibc = s390_ibc_from_cpu_model(cpu->model);
192     lowest_ibc = cpu->model->lowest_ibc;
193     /* the lowest_ibc always has to be <= unblocked_ibc */
194     if (!lowest_ibc || lowest_ibc > unblocked_ibc) {
195         return 0;
196     }
197     return ((uint32_t) lowest_ibc << 16) | unblocked_ibc;
198 }
199 
200 void s390_get_feat_block(S390FeatType type, uint8_t *data)
201 {
202     static S390CPU *cpu;
203 
204     if (!cpu) {
205         cpu = S390_CPU(qemu_get_cpu(0));
206     }
207 
208     if (!cpu || !cpu->model) {
209         return;
210     }
211     s390_fill_feat_block(cpu->model->features, type, data);
212 }
213 
214 bool s390_has_feat(S390Feat feat)
215 {
216     static S390CPU *cpu;
217 
218     if (!cpu) {
219         cpu = S390_CPU(qemu_get_cpu(0));
220     }
221 
222     if (!cpu || !cpu->model) {
223 #ifdef CONFIG_KVM
224         if (kvm_enabled()) {
225             if (feat == S390_FEAT_VECTOR) {
226                 return kvm_check_extension(kvm_state,
227                                            KVM_CAP_S390_VECTOR_REGISTERS);
228             }
229             if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) {
230                 return kvm_s390_get_ri();
231             }
232             if (feat == S390_FEAT_MSA_EXT_3) {
233                 return true;
234             }
235         }
236 #endif
237         if (feat == S390_FEAT_ZPCI) {
238             return true;
239         }
240         return 0;
241     }
242     if (feat == S390_FEAT_DIAG_318 && s390_is_pv()) {
243         return false;
244     }
245     return test_bit(feat, cpu->model->features);
246 }
247 
248 uint8_t s390_get_gen_for_cpu_type(uint16_t type)
249 {
250     int i;
251 
252     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
253         if (s390_cpu_defs[i].type == type) {
254             return s390_cpu_defs[i].gen;
255         }
256     }
257     return 0;
258 }
259 
260 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
261                                     S390FeatBitmap features)
262 {
263     const S390CPUDef *last_compatible = NULL;
264     const S390CPUDef *matching_cpu_type = NULL;
265     int i;
266 
267     if (!gen) {
268         ec_ga = 0;
269     }
270     if (!gen && type) {
271         gen = s390_get_gen_for_cpu_type(type);
272     }
273 
274     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
275         const S390CPUDef *def = &s390_cpu_defs[i];
276         S390FeatBitmap missing;
277 
278         /* don't even try newer generations if we know the generation */
279         if (gen) {
280             if (def->gen > gen) {
281                 break;
282             } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) {
283                 break;
284             }
285         }
286 
287         if (features) {
288             /* see if the model satisfies the minimum features */
289             bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX);
290             /*
291              * Ignore certain features that are in the base model, but not
292              * relevant for the search (esp. MSA subfunctions).
293              */
294             bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX);
295             if (!bitmap_empty(missing, S390_FEAT_MAX)) {
296                 break;
297             }
298         }
299 
300         /* stop the search if we found the exact model */
301         if (def->type == type && def->ec_ga == ec_ga) {
302             return def;
303         }
304         /* remember if we've at least seen one with the same cpu type */
305         if (def->type == type) {
306             matching_cpu_type = def;
307         }
308         last_compatible = def;
309     }
310     /* prefer the model with the same cpu type, esp. don't take the BC for EC */
311     if (matching_cpu_type) {
312         return matching_cpu_type;
313     }
314     return last_compatible;
315 }
316 
317 static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
318 {
319     const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
320     char *name = g_strdup(object_class_get_name((ObjectClass *)data));
321     const char *details = "";
322 
323     if (scc->is_static) {
324         details = "(static, migration-safe)";
325     } else if (scc->is_migration_safe) {
326         details = "(migration-safe)";
327     }
328 
329     /* strip off the -s390x-cpu */
330     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
331     qemu_printf("s390 %-15s %-35s %s\n", name, scc->desc, details);
332     g_free(name);
333 }
334 
335 static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
336 {
337     const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
338     const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
339     const char *name_a = object_class_get_name((ObjectClass *)a);
340     const char *name_b = object_class_get_name((ObjectClass *)b);
341 
342     /*
343      * Move qemu, host and max to the top of the list, qemu first, host second,
344      * max third.
345      */
346     if (name_a[0] == 'q') {
347         return -1;
348     } else if (name_b[0] == 'q') {
349         return 1;
350     } else if (name_a[0] == 'h') {
351         return -1;
352     } else if (name_b[0] == 'h') {
353         return 1;
354     } else if (name_a[0] == 'm') {
355         return -1;
356     } else if (name_b[0] == 'm') {
357         return 1;
358     }
359 
360     /* keep the same order we have in our table (sorted by release date) */
361     if (cc_a->cpu_def != cc_b->cpu_def) {
362         return cc_a->cpu_def - cc_b->cpu_def;
363     }
364 
365     /* exact same definition - list base model first */
366     return cc_a->is_static ? -1 : 1;
367 }
368 
369 void s390_cpu_list(void)
370 {
371     S390FeatGroup group;
372     S390Feat feat;
373     GSList *list;
374 
375     list = object_class_get_list(TYPE_S390_CPU, false);
376     list = g_slist_sort(list, s390_cpu_list_compare);
377     g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
378     g_slist_free(list);
379 
380     qemu_printf("\nRecognized feature flags:\n");
381     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
382         const S390FeatDef *def = s390_feat_def(feat);
383 
384         qemu_printf("%-20s %-50s\n", def->name, def->desc);
385     }
386 
387     qemu_printf("\nRecognized feature groups:\n");
388     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
389         const S390FeatGroupDef *def = s390_feat_group_def(group);
390 
391         qemu_printf("%-20s %-50s\n", def->name, def->desc);
392     }
393 }
394 
395 static S390CPUModel *get_max_cpu_model(Error **errp);
396 
397 #ifndef CONFIG_USER_ONLY
398 static void list_add_feat(const char *name, void *opaque);
399 
400 static void check_unavailable_features(const S390CPUModel *max_model,
401                                        const S390CPUModel *model,
402                                        strList **unavailable)
403 {
404     S390FeatBitmap missing;
405 
406     /* check general model compatibility */
407     if (max_model->def->gen < model->def->gen ||
408         (max_model->def->gen == model->def->gen &&
409          max_model->def->ec_ga < model->def->ec_ga)) {
410         list_add_feat("type", unavailable);
411     }
412 
413     /* detect missing features if any to properly report them */
414     bitmap_andnot(missing, model->features, max_model->features,
415                   S390_FEAT_MAX);
416     if (!bitmap_empty(missing, S390_FEAT_MAX)) {
417         s390_feat_bitmap_to_ascii(missing, unavailable, list_add_feat);
418     }
419 }
420 
421 struct CpuDefinitionInfoListData {
422     CpuDefinitionInfoList *list;
423     S390CPUModel *model;
424 };
425 
426 static void create_cpu_model_list(ObjectClass *klass, void *opaque)
427 {
428     struct CpuDefinitionInfoListData *cpu_list_data = opaque;
429     CpuDefinitionInfoList **cpu_list = &cpu_list_data->list;
430     CpuDefinitionInfo *info;
431     char *name = g_strdup(object_class_get_name(klass));
432     S390CPUClass *scc = S390_CPU_CLASS(klass);
433 
434     /* strip off the -s390x-cpu */
435     g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
436     info = g_new0(CpuDefinitionInfo, 1);
437     info->name = name;
438     info->has_migration_safe = true;
439     info->migration_safe = scc->is_migration_safe;
440     info->q_static = scc->is_static;
441     info->q_typename = g_strdup(object_class_get_name(klass));
442     /* check for unavailable features */
443     if (cpu_list_data->model) {
444         Object *obj;
445         S390CPU *sc;
446         obj = object_new_with_class(klass);
447         sc = S390_CPU(obj);
448         if (sc->model) {
449             info->has_unavailable_features = true;
450             check_unavailable_features(cpu_list_data->model, sc->model,
451                                        &info->unavailable_features);
452         }
453         object_unref(obj);
454     }
455 
456     QAPI_LIST_PREPEND(*cpu_list, info);
457 }
458 
459 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
460 {
461     struct CpuDefinitionInfoListData list_data = {
462         .list = NULL,
463     };
464 
465     list_data.model = get_max_cpu_model(NULL);
466 
467     object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false,
468                          &list_data);
469 
470     return list_data.list;
471 }
472 
473 static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
474                                 Error **errp)
475 {
476     Error *err = NULL;
477     const QDict *qdict = NULL;
478     const QDictEntry *e;
479     Visitor *visitor;
480     ObjectClass *oc;
481     S390CPU *cpu;
482     Object *obj;
483 
484     if (info->props) {
485         qdict = qobject_to(QDict, info->props);
486         if (!qdict) {
487             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
488             return;
489         }
490     }
491 
492     oc = cpu_class_by_name(TYPE_S390_CPU, info->name);
493     if (!oc) {
494         error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name);
495         return;
496     }
497     if (S390_CPU_CLASS(oc)->kvm_required && !kvm_enabled()) {
498         error_setg(errp, "The CPU definition '%s' requires KVM", info->name);
499         return;
500     }
501     obj = object_new_with_class(oc);
502     cpu = S390_CPU(obj);
503 
504     if (!cpu->model) {
505         error_setg(errp, "Details about the host CPU model are not available, "
506                          "it cannot be used.");
507         object_unref(obj);
508         return;
509     }
510 
511     if (qdict) {
512         visitor = qobject_input_visitor_new(info->props);
513         if (!visit_start_struct(visitor, NULL, NULL, 0, errp)) {
514             visit_free(visitor);
515             object_unref(obj);
516             return;
517         }
518         for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
519             if (!object_property_set(obj, e->key, visitor, &err)) {
520                 break;
521             }
522         }
523         if (!err) {
524             visit_check_struct(visitor, &err);
525         }
526         visit_end_struct(visitor, NULL);
527         visit_free(visitor);
528         if (err) {
529             error_propagate(errp, err);
530             object_unref(obj);
531             return;
532         }
533     }
534 
535     /* copy the model and throw the cpu away */
536     memcpy(model, cpu->model, sizeof(*model));
537     object_unref(obj);
538 }
539 
540 static void qdict_add_disabled_feat(const char *name, void *opaque)
541 {
542     qdict_put_bool(opaque, name, false);
543 }
544 
545 static void qdict_add_enabled_feat(const char *name, void *opaque)
546 {
547     qdict_put_bool(opaque, name, true);
548 }
549 
550 /* convert S390CPUDef into a static CpuModelInfo */
551 static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model,
552                                 bool delta_changes)
553 {
554     QDict *qdict = qdict_new();
555     S390FeatBitmap bitmap;
556 
557     /* always fallback to the static base model */
558     info->name = g_strdup_printf("%s-base", model->def->name);
559 
560     if (delta_changes) {
561         /* features deleted from the base feature set */
562         bitmap_andnot(bitmap, model->def->base_feat, model->features,
563                       S390_FEAT_MAX);
564         if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
565             s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
566         }
567 
568         /* features added to the base feature set */
569         bitmap_andnot(bitmap, model->features, model->def->base_feat,
570                       S390_FEAT_MAX);
571         if (!bitmap_empty(bitmap, S390_FEAT_MAX)) {
572             s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_enabled_feat);
573         }
574     } else {
575         /* expand all features */
576         s390_feat_bitmap_to_ascii(model->features, qdict,
577                                   qdict_add_enabled_feat);
578         bitmap_complement(bitmap, model->features, S390_FEAT_MAX);
579         s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat);
580     }
581 
582     if (!qdict_size(qdict)) {
583         qobject_unref(qdict);
584     } else {
585         info->props = QOBJECT(qdict);
586         info->has_props = true;
587     }
588 }
589 
590 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
591                                                       CpuModelInfo *model,
592                                                       Error **errp)
593 {
594     Error *err = NULL;
595     CpuModelExpansionInfo *expansion_info = NULL;
596     S390CPUModel s390_model;
597     bool delta_changes = false;
598 
599     /* convert it to our internal representation */
600     cpu_model_from_info(&s390_model, model, &err);
601     if (err) {
602         error_propagate(errp, err);
603         return NULL;
604     }
605 
606     if (type == CPU_MODEL_EXPANSION_TYPE_STATIC) {
607         delta_changes = true;
608     } else if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
609         error_setg(errp, "The requested expansion type is not supported.");
610         return NULL;
611     }
612 
613     /* convert it back to a static representation */
614     expansion_info = g_new0(CpuModelExpansionInfo, 1);
615     expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
616     cpu_info_from_model(expansion_info->model, &s390_model, delta_changes);
617     return expansion_info;
618 }
619 
620 static void list_add_feat(const char *name, void *opaque)
621 {
622     strList **last = (strList **) opaque;
623 
624     QAPI_LIST_PREPEND(*last, g_strdup(name));
625 }
626 
627 CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa,
628                                                      CpuModelInfo *infob,
629                                                      Error **errp)
630 {
631     Error *err = NULL;
632     CpuModelCompareResult feat_result, gen_result;
633     CpuModelCompareInfo *compare_info;
634     S390FeatBitmap missing, added;
635     S390CPUModel modela, modelb;
636 
637     /* convert both models to our internal representation */
638     cpu_model_from_info(&modela, infoa, &err);
639     if (err) {
640         error_propagate(errp, err);
641         return NULL;
642     }
643     cpu_model_from_info(&modelb, infob, &err);
644     if (err) {
645         error_propagate(errp, err);
646         return NULL;
647     }
648     compare_info = g_new0(CpuModelCompareInfo, 1);
649 
650     /* check the cpu generation and ga level */
651     if (modela.def->gen == modelb.def->gen) {
652         if (modela.def->ec_ga == modelb.def->ec_ga) {
653             /* ec and corresponding bc are identical */
654             gen_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
655         } else if (modela.def->ec_ga < modelb.def->ec_ga) {
656             gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
657         } else {
658             gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
659         }
660     } else if (modela.def->gen < modelb.def->gen) {
661         gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
662     } else {
663         gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
664     }
665     if (gen_result != CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
666         /* both models cannot be made identical */
667         list_add_feat("type", &compare_info->responsible_properties);
668     }
669 
670     /* check the feature set */
671     if (bitmap_equal(modela.features, modelb.features, S390_FEAT_MAX)) {
672         feat_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL;
673     } else {
674         bitmap_andnot(missing, modela.features, modelb.features, S390_FEAT_MAX);
675         s390_feat_bitmap_to_ascii(missing,
676                                   &compare_info->responsible_properties,
677                                   list_add_feat);
678         bitmap_andnot(added, modelb.features, modela.features, S390_FEAT_MAX);
679         s390_feat_bitmap_to_ascii(added, &compare_info->responsible_properties,
680                                   list_add_feat);
681         if (bitmap_empty(missing, S390_FEAT_MAX)) {
682             feat_result = CPU_MODEL_COMPARE_RESULT_SUBSET;
683         } else if (bitmap_empty(added, S390_FEAT_MAX)) {
684             feat_result = CPU_MODEL_COMPARE_RESULT_SUPERSET;
685         } else {
686             feat_result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
687         }
688     }
689 
690     /* combine the results */
691     if (gen_result == feat_result) {
692         compare_info->result = gen_result;
693     } else if (feat_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
694         compare_info->result = gen_result;
695     } else if (gen_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) {
696         compare_info->result = feat_result;
697     } else {
698         compare_info->result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE;
699     }
700     return compare_info;
701 }
702 
703 CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa,
704                                                     CpuModelInfo *infob,
705                                                     Error **errp)
706 {
707     Error *err = NULL;
708     CpuModelBaselineInfo *baseline_info;
709     S390CPUModel modela, modelb, model;
710     uint16_t cpu_type;
711     uint8_t max_gen_ga;
712     uint8_t max_gen;
713 
714     /* convert both models to our internal representation */
715     cpu_model_from_info(&modela, infoa, &err);
716     if (err) {
717         error_propagate(errp, err);
718         return NULL;
719     }
720 
721     cpu_model_from_info(&modelb, infob, &err);
722     if (err) {
723         error_propagate(errp, err);
724         return NULL;
725     }
726 
727     /* features both models support */
728     bitmap_and(model.features, modela.features, modelb.features, S390_FEAT_MAX);
729 
730     /* detect the maximum model not regarding features */
731     if (modela.def->gen == modelb.def->gen) {
732         if (modela.def->type == modelb.def->type) {
733             cpu_type = modela.def->type;
734         } else {
735             cpu_type = 0;
736         }
737         max_gen = modela.def->gen;
738         max_gen_ga = MIN(modela.def->ec_ga, modelb.def->ec_ga);
739     } else if (modela.def->gen > modelb.def->gen) {
740         cpu_type = modelb.def->type;
741         max_gen = modelb.def->gen;
742         max_gen_ga = modelb.def->ec_ga;
743     } else {
744         cpu_type = modela.def->type;
745         max_gen = modela.def->gen;
746         max_gen_ga = modela.def->ec_ga;
747     }
748 
749     model.def = s390_find_cpu_def(cpu_type, max_gen, max_gen_ga,
750                                   model.features);
751 
752     /* models without early base features (esan3) are bad */
753     if (!model.def) {
754         error_setg(errp, "No compatible CPU model could be created as"
755                    " important base features are disabled");
756         return NULL;
757     }
758 
759     /* strip off features not part of the max model */
760     bitmap_and(model.features, model.features, model.def->full_feat,
761                S390_FEAT_MAX);
762 
763     baseline_info = g_new0(CpuModelBaselineInfo, 1);
764     baseline_info->model = g_malloc0(sizeof(*baseline_info->model));
765     cpu_info_from_model(baseline_info->model, &model, true);
766     return baseline_info;
767 }
768 #endif
769 
770 static void check_consistency(const S390CPUModel *model)
771 {
772     static int dep[][2] = {
773         { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH },
774         { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH },
775         { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH },
776         { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT },
777         { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH},
778         { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT },
779         { S390_FEAT_DFP_FAST, S390_FEAT_DFP },
780         { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 },
781         { S390_FEAT_EDAT_2, S390_FEAT_EDAT},
782         { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 },
783         { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 },
784         { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 },
785         { S390_FEAT_SIE_CMMA, S390_FEAT_CMM },
786         { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS },
787         { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT },
788         { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 },
789         { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_3 },
790         { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_4 },
791         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
792         { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR },
793         { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR },
794         { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
795         { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP },
796         { S390_FEAT_CMM_NT, S390_FEAT_CMM },
797         { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 },
798         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST },
799         { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING },
800         { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 },
801         { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA },
802         { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA },
803         { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA },
804         { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA },
805         { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA },
806         { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA },
807         { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA },
808         { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA },
809         { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA },
810         { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA },
811         { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA },
812         { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA },
813         { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 },
814         { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 },
815         { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 },
816         { S390_FEAT_AP_QUERY_CONFIG_INFO, S390_FEAT_AP },
817         { S390_FEAT_AP_FACILITIES_TEST, S390_FEAT_AP },
818         { S390_FEAT_PTFF_QSIE, S390_FEAT_MULTIPLE_EPOCH },
819         { S390_FEAT_PTFF_QTOUE, S390_FEAT_MULTIPLE_EPOCH },
820         { S390_FEAT_PTFF_STOE, S390_FEAT_MULTIPLE_EPOCH },
821         { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH },
822         { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP },
823         { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB },
824     };
825     int i;
826 
827     for (i = 0; i < ARRAY_SIZE(dep); i++) {
828         if (test_bit(dep[i][0], model->features) &&
829             !test_bit(dep[i][1], model->features)) {
830             warn_report("\'%s\' requires \'%s\'.",
831                         s390_feat_def(dep[i][0])->name,
832                         s390_feat_def(dep[i][1])->name);
833         }
834     }
835 }
836 
837 static void error_prepend_missing_feat(const char *name, void *opaque)
838 {
839     error_prepend((Error **) opaque, "%s ", name);
840 }
841 
842 static void check_compatibility(const S390CPUModel *max_model,
843                                 const S390CPUModel *model, Error **errp)
844 {
845     S390FeatBitmap missing;
846 
847     if (model->def->gen > max_model->def->gen) {
848         error_setg(errp, "Selected CPU generation is too new. Maximum "
849                    "supported model in the configuration: \'%s\'",
850                    max_model->def->name);
851         return;
852     } else if (model->def->gen == max_model->def->gen &&
853                model->def->ec_ga > max_model->def->ec_ga) {
854         error_setg(errp, "Selected CPU GA level is too new. Maximum "
855                    "supported model in the configuration: \'%s\'",
856                    max_model->def->name);
857         return;
858     }
859 
860     /* detect the missing features to properly report them */
861     bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX);
862     if (bitmap_empty(missing, S390_FEAT_MAX)) {
863         return;
864     }
865 
866     error_setg(errp, " ");
867     s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat);
868     error_prepend(errp, "Some features requested in the CPU model are not "
869                   "available in the configuration: ");
870 }
871 
872 static S390CPUModel *get_max_cpu_model(Error **errp)
873 {
874     Error *err = NULL;
875     static S390CPUModel max_model;
876     static bool cached;
877 
878     if (cached) {
879         return &max_model;
880     }
881 
882     if (kvm_enabled()) {
883         kvm_s390_get_host_cpu_model(&max_model, &err);
884     } else {
885         max_model.def = s390_find_cpu_def(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
886                                           QEMU_MAX_CPU_EC_GA, NULL);
887         bitmap_copy(max_model.features, qemu_max_cpu_feat, S390_FEAT_MAX);
888     }
889     if (err) {
890         error_propagate(errp, err);
891         return NULL;
892     }
893     cached = true;
894     return &max_model;
895 }
896 
897 static inline void apply_cpu_model(const S390CPUModel *model, Error **errp)
898 {
899 #ifndef CONFIG_USER_ONLY
900     Error *err = NULL;
901     static S390CPUModel applied_model;
902     static bool applied;
903 
904     /*
905      * We have the same model for all VCPUs. KVM can only be configured before
906      * any VCPUs are defined in KVM.
907      */
908     if (applied) {
909         if (model && memcmp(&applied_model, model, sizeof(S390CPUModel))) {
910             error_setg(errp, "Mixed CPU models are not supported on s390x.");
911         }
912         return;
913     }
914 
915     if (kvm_enabled()) {
916         kvm_s390_apply_cpu_model(model, &err);
917         if (err) {
918             error_propagate(errp, err);
919             return;
920         }
921     }
922 
923     applied = true;
924     if (model) {
925         applied_model = *model;
926     }
927 #endif
928 }
929 
930 void s390_realize_cpu_model(CPUState *cs, Error **errp)
931 {
932     Error *err = NULL;
933     S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
934     S390CPU *cpu = S390_CPU(cs);
935     const S390CPUModel *max_model;
936 
937     if (xcc->kvm_required && !kvm_enabled()) {
938         error_setg(errp, "CPU definition requires KVM");
939         return;
940     }
941 
942     if (!cpu->model) {
943         /* no host model support -> perform compatibility stuff */
944         apply_cpu_model(NULL, errp);
945         return;
946     }
947 
948     max_model = get_max_cpu_model(errp);
949     if (!max_model) {
950         error_prepend(errp, "CPU models are not available: ");
951         return;
952     }
953 
954     /* copy over properties that can vary */
955     cpu->model->lowest_ibc = max_model->lowest_ibc;
956     cpu->model->cpu_id = max_model->cpu_id;
957     cpu->model->cpu_id_format = max_model->cpu_id_format;
958     cpu->model->cpu_ver = max_model->cpu_ver;
959 
960     check_consistency(cpu->model);
961     check_compatibility(max_model, cpu->model, &err);
962     if (err) {
963         error_propagate(errp, err);
964         return;
965     }
966 
967     apply_cpu_model(cpu->model, errp);
968 
969 #if !defined(CONFIG_USER_ONLY)
970     cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model);
971     if (tcg_enabled()) {
972         /* basic mode, write the cpu address into the first 4 bit of the ID */
973         cpu->env.cpuid = deposit64(cpu->env.cpuid, 54, 4, cpu->env.core_id);
974     }
975 #endif
976 }
977 
978 static void get_feature(Object *obj, Visitor *v, const char *name,
979                         void *opaque, Error **errp)
980 {
981     S390Feat feat = (S390Feat) (uintptr_t) opaque;
982     S390CPU *cpu = S390_CPU(obj);
983     bool value;
984 
985     if (!cpu->model) {
986         error_setg(errp, "Details about the host CPU model are not available, "
987                          "features cannot be queried.");
988         return;
989     }
990 
991     value = test_bit(feat, cpu->model->features);
992     visit_type_bool(v, name, &value, errp);
993 }
994 
995 static void set_feature(Object *obj, Visitor *v, const char *name,
996                         void *opaque, Error **errp)
997 {
998     S390Feat feat = (S390Feat) (uintptr_t) opaque;
999     DeviceState *dev = DEVICE(obj);
1000     S390CPU *cpu = S390_CPU(obj);
1001     bool value;
1002 
1003     if (dev->realized) {
1004         error_setg(errp, "Attempt to set property '%s' on '%s' after "
1005                    "it was realized", name, object_get_typename(obj));
1006         return;
1007     } else if (!cpu->model) {
1008         error_setg(errp, "Details about the host CPU model are not available, "
1009                          "features cannot be changed.");
1010         return;
1011     }
1012 
1013     if (!visit_type_bool(v, name, &value, errp)) {
1014         return;
1015     }
1016     if (value) {
1017         if (!test_bit(feat, cpu->model->def->full_feat)) {
1018             error_setg(errp, "Feature '%s' is not available for CPU model '%s',"
1019                        " it was introduced with later models.",
1020                        name, cpu->model->def->name);
1021             return;
1022         }
1023         set_bit(feat, cpu->model->features);
1024     } else {
1025         clear_bit(feat, cpu->model->features);
1026     }
1027 }
1028 
1029 static void get_feature_group(Object *obj, Visitor *v, const char *name,
1030                               void *opaque, Error **errp)
1031 {
1032     S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
1033     const S390FeatGroupDef *def = s390_feat_group_def(group);
1034     S390CPU *cpu = S390_CPU(obj);
1035     S390FeatBitmap tmp;
1036     bool value;
1037 
1038     if (!cpu->model) {
1039         error_setg(errp, "Details about the host CPU model are not available, "
1040                          "features cannot be queried.");
1041         return;
1042     }
1043 
1044     /* a group is enabled if all features are enabled */
1045     bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX);
1046     value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX);
1047     visit_type_bool(v, name, &value, errp);
1048 }
1049 
1050 static void set_feature_group(Object *obj, Visitor *v, const char *name,
1051                               void *opaque, Error **errp)
1052 {
1053     S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque;
1054     const S390FeatGroupDef *def = s390_feat_group_def(group);
1055     DeviceState *dev = DEVICE(obj);
1056     S390CPU *cpu = S390_CPU(obj);
1057     bool value;
1058 
1059     if (dev->realized) {
1060         error_setg(errp, "Attempt to set property '%s' on '%s' after "
1061                    "it was realized", name, object_get_typename(obj));
1062         return;
1063     } else if (!cpu->model) {
1064         error_setg(errp, "Details about the host CPU model are not available, "
1065                          "features cannot be changed.");
1066         return;
1067     }
1068 
1069     if (!visit_type_bool(v, name, &value, errp)) {
1070         return;
1071     }
1072     if (value) {
1073         /* groups are added in one shot, so an intersect is sufficient */
1074         if (!bitmap_intersects(def->feat, cpu->model->def->full_feat,
1075                                S390_FEAT_MAX)) {
1076             error_setg(errp, "Group '%s' is not available for CPU model '%s',"
1077                        " it was introduced with later models.",
1078                        name, cpu->model->def->name);
1079             return;
1080         }
1081         bitmap_or(cpu->model->features, cpu->model->features, def->feat,
1082                   S390_FEAT_MAX);
1083     } else {
1084         bitmap_andnot(cpu->model->features, cpu->model->features, def->feat,
1085                       S390_FEAT_MAX);
1086     }
1087 }
1088 
1089 static void s390_cpu_model_initfn(Object *obj)
1090 {
1091     S390CPU *cpu = S390_CPU(obj);
1092     S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu);
1093 
1094     cpu->model = g_malloc0(sizeof(*cpu->model));
1095     /* copy the model, so we can modify it */
1096     cpu->model->def = xcc->cpu_def;
1097     if (xcc->is_static) {
1098         /* base model - features will never change */
1099         bitmap_copy(cpu->model->features, cpu->model->def->base_feat,
1100                     S390_FEAT_MAX);
1101     } else {
1102         /* latest model - features can change */
1103         bitmap_copy(cpu->model->features,
1104                     cpu->model->def->default_feat, S390_FEAT_MAX);
1105     }
1106 }
1107 
1108 static S390CPUDef s390_qemu_cpu_def;
1109 static S390CPUModel s390_qemu_cpu_model;
1110 
1111 /* Set the qemu CPU model (on machine initialization). Must not be called
1112  * once CPUs have been created.
1113  */
1114 void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
1115                              const S390FeatInit feat_init)
1116 {
1117     const S390CPUDef *def = s390_find_cpu_def(type, gen, ec_ga, NULL);
1118 
1119     g_assert(def);
1120     g_assert(QTAILQ_EMPTY_RCU(&cpus));
1121 
1122     /* TCG emulates some features that can usually not be enabled with
1123      * the emulated machine generation. Make sure they can be enabled
1124      * when using the QEMU model by adding them to full_feat. We have
1125      * to copy the definition to do that.
1126      */
1127     memcpy(&s390_qemu_cpu_def, def, sizeof(s390_qemu_cpu_def));
1128     bitmap_or(s390_qemu_cpu_def.full_feat, s390_qemu_cpu_def.full_feat,
1129               qemu_max_cpu_feat, S390_FEAT_MAX);
1130 
1131     /* build the CPU model */
1132     s390_qemu_cpu_model.def = &s390_qemu_cpu_def;
1133     bitmap_zero(s390_qemu_cpu_model.features, S390_FEAT_MAX);
1134     s390_init_feat_bitmap(feat_init, s390_qemu_cpu_model.features);
1135 }
1136 
1137 static void s390_qemu_cpu_model_initfn(Object *obj)
1138 {
1139     S390CPU *cpu = S390_CPU(obj);
1140 
1141     cpu->model = g_malloc0(sizeof(*cpu->model));
1142     /* copy the CPU model so we can modify it */
1143     memcpy(cpu->model, &s390_qemu_cpu_model, sizeof(*cpu->model));
1144 }
1145 
1146 static void s390_max_cpu_model_initfn(Object *obj)
1147 {
1148     const S390CPUModel *max_model;
1149     S390CPU *cpu = S390_CPU(obj);
1150     Error *local_err = NULL;
1151 
1152     if (kvm_enabled() && !kvm_s390_cpu_models_supported()) {
1153         /* "max" and "host" always work, even without CPU model support */
1154         return;
1155     }
1156 
1157     max_model = get_max_cpu_model(&local_err);
1158     if (local_err) {
1159         /* we expect errors only under KVM, when actually querying the kernel */
1160         g_assert(kvm_enabled());
1161         error_report_err(local_err);
1162         /* fallback to unsupported CPU models */
1163         return;
1164     }
1165 
1166     cpu->model = g_new(S390CPUModel, 1);
1167     /* copy the CPU model so we can modify it */
1168     memcpy(cpu->model, max_model, sizeof(*cpu->model));
1169 }
1170 
1171 static void s390_cpu_model_finalize(Object *obj)
1172 {
1173     S390CPU *cpu = S390_CPU(obj);
1174 
1175     g_free(cpu->model);
1176     cpu->model = NULL;
1177 }
1178 
1179 static bool get_is_migration_safe(Object *obj, Error **errp)
1180 {
1181     return S390_CPU_GET_CLASS(obj)->is_migration_safe;
1182 }
1183 
1184 static bool get_is_static(Object *obj, Error **errp)
1185 {
1186     return S390_CPU_GET_CLASS(obj)->is_static;
1187 }
1188 
1189 static char *get_description(Object *obj, Error **errp)
1190 {
1191     return g_strdup(S390_CPU_GET_CLASS(obj)->desc);
1192 }
1193 
1194 void s390_cpu_model_class_register_props(ObjectClass *oc)
1195 {
1196     S390FeatGroup group;
1197     S390Feat feat;
1198 
1199     object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe,
1200                                    NULL);
1201     object_class_property_add_bool(oc, "static", get_is_static,
1202                                    NULL);
1203     object_class_property_add_str(oc, "description", get_description, NULL);
1204 
1205     for (feat = 0; feat < S390_FEAT_MAX; feat++) {
1206         const S390FeatDef *def = s390_feat_def(feat);
1207         object_class_property_add(oc, def->name, "bool", get_feature,
1208                                   set_feature, NULL, (void *) feat);
1209         object_class_property_set_description(oc, def->name, def->desc);
1210     }
1211     for (group = 0; group < S390_FEAT_GROUP_MAX; group++) {
1212         const S390FeatGroupDef *def = s390_feat_group_def(group);
1213         object_class_property_add(oc, def->name, "bool", get_feature_group,
1214                                   set_feature_group, NULL, (void *) group);
1215         object_class_property_set_description(oc, def->name, def->desc);
1216     }
1217 }
1218 
1219 #ifdef CONFIG_KVM
1220 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data)
1221 {
1222     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1223 
1224     xcc->kvm_required = true;
1225     xcc->desc = "KVM only: All recognized features";
1226 }
1227 #endif
1228 
1229 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data)
1230 {
1231     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1232 
1233     /* all base models are migration safe */
1234     xcc->cpu_def = (const S390CPUDef *) data;
1235     xcc->is_migration_safe = true;
1236     xcc->is_static = true;
1237     xcc->desc = xcc->cpu_def->desc;
1238 }
1239 
1240 static void s390_cpu_model_class_init(ObjectClass *oc, void *data)
1241 {
1242     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1243 
1244     /* model that can change between QEMU versions */
1245     xcc->cpu_def = (const S390CPUDef *) data;
1246     xcc->is_migration_safe = true;
1247     xcc->desc = xcc->cpu_def->desc;
1248 }
1249 
1250 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data)
1251 {
1252     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1253 
1254     xcc->is_migration_safe = true;
1255     xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s",
1256                                 qemu_hw_version());
1257 }
1258 
1259 static void s390_max_cpu_model_class_init(ObjectClass *oc, void *data)
1260 {
1261     S390CPUClass *xcc = S390_CPU_CLASS(oc);
1262 
1263     /*
1264      * The "max" model is neither static nor migration safe. Under KVM
1265      * it represents the "host" model. Under TCG it represents some kind of
1266      * "qemu" CPU model without compat handling and maybe with some additional
1267      * CPU features that are not yet unlocked in the "qemu" model.
1268      */
1269     xcc->desc =
1270         "Enables all features supported by the accelerator in the current host";
1271 }
1272 
1273 /* Generate type name for a cpu model. Caller has to free the string. */
1274 static char *s390_cpu_type_name(const char *model_name)
1275 {
1276     return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name);
1277 }
1278 
1279 /* Generate type name for a base cpu model. Caller has to free the string. */
1280 static char *s390_base_cpu_type_name(const char *model_name)
1281 {
1282     return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name);
1283 }
1284 
1285 ObjectClass *s390_cpu_class_by_name(const char *name)
1286 {
1287     char *typename = s390_cpu_type_name(name);
1288     ObjectClass *oc;
1289 
1290     oc = object_class_by_name(typename);
1291     g_free(typename);
1292     return oc;
1293 }
1294 
1295 static const TypeInfo qemu_s390_cpu_type_info = {
1296     .name = S390_CPU_TYPE_NAME("qemu"),
1297     .parent = TYPE_S390_CPU,
1298     .instance_init = s390_qemu_cpu_model_initfn,
1299     .instance_finalize = s390_cpu_model_finalize,
1300     .class_init = s390_qemu_cpu_model_class_init,
1301 };
1302 
1303 static const TypeInfo max_s390_cpu_type_info = {
1304     .name = S390_CPU_TYPE_NAME("max"),
1305     .parent = TYPE_S390_CPU,
1306     .instance_init = s390_max_cpu_model_initfn,
1307     .instance_finalize = s390_cpu_model_finalize,
1308     .class_init = s390_max_cpu_model_class_init,
1309 };
1310 
1311 #ifdef CONFIG_KVM
1312 static const TypeInfo host_s390_cpu_type_info = {
1313     .name = S390_CPU_TYPE_NAME("host"),
1314     .parent = S390_CPU_TYPE_NAME("max"),
1315     .class_init = s390_host_cpu_model_class_init,
1316 };
1317 #endif
1318 
1319 static void init_ignored_base_feat(void)
1320 {
1321     static const int feats[] = {
1322          /* MSA subfunctions that could not be available on certain machines */
1323          S390_FEAT_KMAC_DEA,
1324          S390_FEAT_KMAC_TDEA_128,
1325          S390_FEAT_KMAC_TDEA_192,
1326          S390_FEAT_KMC_DEA,
1327          S390_FEAT_KMC_TDEA_128,
1328          S390_FEAT_KMC_TDEA_192,
1329          S390_FEAT_KM_DEA,
1330          S390_FEAT_KM_TDEA_128,
1331          S390_FEAT_KM_TDEA_192,
1332          S390_FEAT_KIMD_SHA_1,
1333          S390_FEAT_KLMD_SHA_1,
1334          /* CSSKE is deprecated on newer generations */
1335          S390_FEAT_CONDITIONAL_SSKE,
1336     };
1337     int i;
1338 
1339     for (i = 0; i < ARRAY_SIZE(feats); i++) {
1340         set_bit(feats[i], ignored_base_feat);
1341     }
1342 }
1343 
1344 static void register_types(void)
1345 {
1346     static const S390FeatInit qemu_latest_init = { S390_FEAT_LIST_QEMU_LATEST };
1347     int i;
1348 
1349     init_ignored_base_feat();
1350 
1351     /* init all bitmaps from gnerated data initially */
1352     s390_init_feat_bitmap(qemu_max_cpu_feat_init, qemu_max_cpu_feat);
1353     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1354         s390_init_feat_bitmap(s390_cpu_defs[i].base_init,
1355                               s390_cpu_defs[i].base_feat);
1356         s390_init_feat_bitmap(s390_cpu_defs[i].default_init,
1357                               s390_cpu_defs[i].default_feat);
1358         s390_init_feat_bitmap(s390_cpu_defs[i].full_init,
1359                               s390_cpu_defs[i].full_feat);
1360     }
1361 
1362     /* initialize the qemu model with latest definition */
1363     s390_set_qemu_cpu_model(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN,
1364                             QEMU_MAX_CPU_EC_GA, qemu_latest_init);
1365 
1366     for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) {
1367         char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name);
1368         TypeInfo ti_base = {
1369             .name = base_name,
1370             .parent = TYPE_S390_CPU,
1371             .instance_init = s390_cpu_model_initfn,
1372             .instance_finalize = s390_cpu_model_finalize,
1373             .class_init = s390_base_cpu_model_class_init,
1374             .class_data = (void *) &s390_cpu_defs[i],
1375         };
1376         char *name = s390_cpu_type_name(s390_cpu_defs[i].name);
1377         TypeInfo ti = {
1378             .name = name,
1379             .parent = TYPE_S390_CPU,
1380             .instance_init = s390_cpu_model_initfn,
1381             .instance_finalize = s390_cpu_model_finalize,
1382             .class_init = s390_cpu_model_class_init,
1383             .class_data = (void *) &s390_cpu_defs[i],
1384         };
1385 
1386         type_register_static(&ti_base);
1387         type_register_static(&ti);
1388         g_free(base_name);
1389         g_free(name);
1390     }
1391 
1392     type_register_static(&qemu_s390_cpu_type_info);
1393     type_register_static(&max_s390_cpu_type_info);
1394 #ifdef CONFIG_KVM
1395     type_register_static(&host_s390_cpu_type_info);
1396 #endif
1397 }
1398 
1399 type_init(register_types)
1400