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