xref: /qemu/include/hw/boards.h (revision 30ee88622edfa962154222b4a674361488ed823b)
1 /* Declarations for use by board files for creating devices.  */
2 
3 #ifndef HW_BOARDS_H
4 #define HW_BOARDS_H
5 
6 #include "exec/memory.h"
7 #include "system/hostmem.h"
8 #include "system/blockdev.h"
9 #include "qapi/qapi-types-machine.h"
10 #include "qemu/module.h"
11 #include "qom/object.h"
12 #include "hw/core/cpu.h"
13 #include "hw/resettable.h"
14 
15 #define TYPE_MACHINE_SUFFIX "-machine"
16 
17 /* Machine class name that needs to be used for class-name-based machine
18  * type lookup to work.
19  */
20 #define MACHINE_TYPE_NAME(machinename) (machinename TYPE_MACHINE_SUFFIX)
21 
22 #define TYPE_MACHINE "machine"
23 #undef MACHINE  /* BSD defines it and QEMU does not use it */
24 OBJECT_DECLARE_TYPE(MachineState, MachineClass, MACHINE)
25 
26 extern MachineState *current_machine;
27 
28 /**
29  * machine_class_default_cpu_type: Return the machine default CPU type.
30  * @mc: Machine class
31  */
32 const char *machine_class_default_cpu_type(MachineClass *mc);
33 
34 void machine_add_audiodev_property(MachineClass *mc);
35 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp);
36 bool machine_usb(MachineState *machine);
37 int machine_phandle_start(MachineState *machine);
38 bool machine_dump_guest_core(MachineState *machine);
39 bool machine_mem_merge(MachineState *machine);
40 bool machine_require_guest_memfd(MachineState *machine);
41 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine);
42 void machine_set_cpu_numa_node(MachineState *machine,
43                                const CpuInstanceProperties *props,
44                                Error **errp);
45 void machine_parse_smp_config(MachineState *ms,
46                               const SMPConfiguration *config, Error **errp);
47 bool machine_parse_smp_cache(MachineState *ms,
48                              const SmpCachePropertiesList *caches,
49                              Error **errp);
50 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms);
51 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms);
52 CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms,
53                                               CacheLevelAndType cache);
54 void machine_set_cache_topo_level(MachineState *ms, CacheLevelAndType cache,
55                                   CpuTopologyLevel level);
56 bool machine_check_smp_cache(const MachineState *ms, Error **errp);
57 void machine_memory_devices_init(MachineState *ms, hwaddr base, uint64_t size);
58 
59 /**
60  * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices
61  * @mc: Machine class
62  * @type: type to allow (should be a subtype of TYPE_SYS_BUS_DEVICE)
63  *
64  * Add the QOM type @type to the list of devices of which are subtypes
65  * of TYPE_SYS_BUS_DEVICE but which are still permitted to be dynamically
66  * created (eg by the user on the command line with -device).
67  * By default if the user tries to create any devices on the command line
68  * that are subtypes of TYPE_SYS_BUS_DEVICE they will get an error message;
69  * for the special cases which are permitted for this machine model, the
70  * machine model class init code must call this function to add them
71  * to the list of specifically permitted devices.
72  */
73 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
74 
75 /**
76  * device_type_is_dynamic_sysbus: Check if type is an allowed sysbus device
77  * type for the machine class.
78  * @mc: Machine class
79  * @type: type to check (should be a subtype of TYPE_SYS_BUS_DEVICE)
80  *
81  * Returns: true if @type is a type in the machine's list of
82  * dynamically pluggable sysbus devices; otherwise false.
83  *
84  * Check if the QOM type @type is in the list of allowed sysbus device
85  * types (see machine_class_allowed_dynamic_sysbus_dev()).
86  * Note that if @type has a parent type in the list, it is allowed too.
87  */
88 bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type);
89 
90 /**
91  * device_is_dynamic_sysbus: test whether device is a dynamic sysbus device
92  * @mc: Machine class
93  * @dev: device to check
94  *
95  * Returns: true if @dev is a sysbus device on the machine's list
96  * of dynamically pluggable sysbus devices; otherwise false.
97  *
98  * This function checks whether @dev is a valid dynamic sysbus device,
99  * by first confirming that it is a sysbus device and then checking it
100  * against the list of permitted dynamic sysbus devices which has been
101  * set up by the machine using machine_class_allow_dynamic_sysbus_dev().
102  *
103  * It is valid to call this with something that is not a subclass of
104  * TYPE_SYS_BUS_DEVICE; the function will return false in this case.
105  * This allows hotplug callback functions to be written as:
106  *     if (device_is_dynamic_sysbus(mc, dev)) {
107  *         handle dynamic sysbus case;
108  *     } else if (some other kind of hotplug) {
109  *         handle that;
110  *     }
111  */
112 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev);
113 
114 /*
115  * Checks that backend isn't used, preps it for exclusive usage and
116  * returns migratable MemoryRegion provided by backend.
117  */
118 MemoryRegion *machine_consume_memdev(MachineState *machine,
119                                      HostMemoryBackend *backend);
120 
121 /**
122  * CPUArchId:
123  * @arch_id - architecture-dependent CPU ID of present or possible CPU
124  * @cpu - pointer to corresponding CPU object if it's present on NULL otherwise
125  * @type - QOM class name of possible @cpu object
126  * @props - CPU object properties, initialized by board
127  * #vcpus_count - number of threads provided by @cpu object
128  */
129 typedef struct CPUArchId {
130     uint64_t arch_id;
131     int64_t vcpus_count;
132     CpuInstanceProperties props;
133     CPUState *cpu;
134     const char *type;
135 } CPUArchId;
136 
137 /**
138  * CPUArchIdList:
139  * @len - number of @CPUArchId items in @cpus array
140  * @cpus - array of present or possible CPUs for current machine configuration
141  */
142 typedef struct {
143     int len;
144     CPUArchId cpus[];
145 } CPUArchIdList;
146 
147 /**
148  * SMPCompatProps:
149  * @prefer_sockets - whether sockets are preferred over cores in smp parsing
150  * @dies_supported - whether dies are supported by the machine
151  * @clusters_supported - whether clusters are supported by the machine
152  * @has_clusters - whether clusters are explicitly specified in the user
153  *                 provided SMP configuration
154  * @books_supported - whether books are supported by the machine
155  * @drawers_supported - whether drawers are supported by the machine
156  * @modules_supported - whether modules are supported by the machine
157  * @cache_supported - whether cache (l1d, l1i, l2 and l3) configuration are
158  *                    supported by the machine
159  */
160 typedef struct {
161     bool prefer_sockets;
162     bool dies_supported;
163     bool clusters_supported;
164     bool has_clusters;
165     bool books_supported;
166     bool drawers_supported;
167     bool modules_supported;
168     bool cache_supported[CACHE_LEVEL_AND_TYPE__MAX];
169 } SMPCompatProps;
170 
171 /**
172  * MachineClass:
173  * @deprecation_reason: If set, the machine is marked as deprecated. The
174  *    string should provide some clear information about what to use instead.
175  * @max_cpus: maximum number of CPUs supported. Default: 1
176  * @min_cpus: minimum number of CPUs supported. Default: 1
177  * @default_cpus: number of CPUs instantiated if none are specified. Default: 1
178  * @is_default:
179  *    If true QEMU will use this machine by default if no '-M' option is given.
180  * @get_hotplug_handler: this function is called during bus-less
181  *    device hotplug. If defined it returns pointer to an instance
182  *    of HotplugHandler object, which handles hotplug operation
183  *    for a given @dev. It may return NULL if @dev doesn't require
184  *    any actions to be performed by hotplug handler.
185  * @cpu_index_to_instance_props:
186  *    used to provide @cpu_index to socket/core/thread number mapping, allowing
187  *    legacy code to perform mapping from cpu_index to topology properties
188  *    Returns: tuple of socket/core/thread ids given cpu_index belongs to.
189  *    used to provide @cpu_index to socket number mapping, allowing
190  *    a machine to group CPU threads belonging to the same socket/package
191  *    Returns: socket number given cpu_index belongs to.
192  * @hw_version:
193  *    Value of QEMU_VERSION when the machine was added to QEMU.
194  *    Set only by old machines because they need to keep
195  *    compatibility on code that exposed QEMU_VERSION to guests in
196  *    the past (and now use qemu_hw_version()).
197  * @possible_cpu_arch_ids:
198  *    Returns an array of @CPUArchId architecture-dependent CPU IDs
199  *    which includes CPU IDs for present and possible to hotplug CPUs.
200  *    Caller is responsible for freeing returned list.
201  * @get_default_cpu_node_id:
202  *    returns default board specific node_id value for CPU slot specified by
203  *    index @idx in @ms->possible_cpus[]
204  * @has_hotpluggable_cpus:
205  *    If true, board supports CPUs creation with -device/device_add.
206  * @default_cpu_type:
207  *    specifies default CPU_TYPE, which will be used for parsing target
208  *    specific features and for creating CPUs if CPU name wasn't provided
209  *    explicitly at CLI
210  * @minimum_page_bits:
211  *    If non-zero, the board promises never to create a CPU with a page size
212  *    smaller than this, so QEMU can use a more efficient larger page
213  *    size than the target architecture's minimum. (Attempting to create
214  *    such a CPU will fail.) Note that changing this is a migration
215  *    compatibility break for the machine.
216  * @ignore_memory_transaction_failures:
217  *    If this is flag is true then the CPU will ignore memory transaction
218  *    failures which should cause the CPU to take an exception due to an
219  *    access to an unassigned physical address; the transaction will instead
220  *    return zero (for a read) or be ignored (for a write). This should be
221  *    set only by legacy board models which rely on the old RAZ/WI behaviour
222  *    for handling devices that QEMU does not yet model. New board models
223  *    should instead use "unimplemented-device" for all memory ranges where
224  *    the guest will attempt to probe for a device that QEMU doesn't
225  *    implement and a stub device is required.
226  * @kvm_type:
227  *    Return the type of KVM corresponding to the kvm-type string option or
228  *    computed based on other criteria such as the host kernel capabilities.
229  *    kvm-type may be NULL if it is not needed.
230  * @hvf_get_physical_address_range:
231  *    Returns the physical address range in bits to use for the HVF virtual
232  *    machine based on the current boards memory map. This may be NULL if it
233  *    is not needed.
234  * @numa_mem_supported:
235  *    true if '--numa node.mem' option is supported and false otherwise
236  * @hotplug_allowed:
237  *    If the hook is provided, then it'll be called for each device
238  *    hotplug to check whether the device hotplug is allowed.  Return
239  *    true to grant allowance or false to reject the hotplug.  When
240  *    false is returned, an error must be set to show the reason of
241  *    the rejection.  If the hook is not provided, all hotplug will be
242  *    allowed.
243  * @default_ram_id:
244  *    Specifies initial RAM MemoryRegion name to be used for default backend
245  *    creation if user explicitly hasn't specified backend with "memory-backend"
246  *    property.
247  *    It also will be used as a way to option into "-m" option support.
248  *    If it's not set by board, '-m' will be ignored and generic code will
249  *    not create default RAM MemoryRegion.
250  * @fixup_ram_size:
251  *    Amends user provided ram size (with -m option) using machine
252  *    specific algorithm. To be used by old machine types for compat
253  *    purposes only.
254  *    Applies only to default memory backend, i.e., explicit memory backend
255  *    wasn't used.
256  * @smbios_memory_device_size:
257  *    Default size of memory device,
258  *    SMBIOS 3.1.0 "7.18 Memory Device (Type 17)"
259  */
260 struct MachineClass {
261     /*< private >*/
262     ObjectClass parent_class;
263     /*< public >*/
264 
265     const char *family; /* NULL iff @name identifies a standalone machtype */
266     char *name;
267     const char *alias;
268     const char *desc;
269     const char *deprecation_reason;
270 
271     void (*init)(MachineState *state);
272     void (*reset)(MachineState *state, ResetType type);
273     void (*wakeup)(MachineState *state);
274     int (*kvm_type)(MachineState *machine, const char *arg);
275     int (*hvf_get_physical_address_range)(MachineState *machine);
276 
277     BlockInterfaceType block_default_type;
278     int units_per_default_bus;
279     int max_cpus;
280     int min_cpus;
281     int default_cpus;
282     unsigned int no_serial:1,
283         no_parallel:1,
284         no_floppy:1,
285         no_cdrom:1,
286         no_sdcard:1,
287         pci_allow_0_address:1,
288         legacy_fw_cfg_order:1;
289     bool is_default;
290     const char *default_machine_opts;
291     const char *default_boot_order;
292     const char *default_display;
293     const char *default_nic;
294     GPtrArray *compat_props;
295     const char *hw_version;
296     ram_addr_t default_ram_size;
297     const char *default_cpu_type;
298     bool default_kernel_irqchip_split;
299     bool option_rom_has_mr;
300     bool rom_file_has_mr;
301     int minimum_page_bits;
302     bool has_hotpluggable_cpus;
303     bool ignore_memory_transaction_failures;
304     int numa_mem_align_shift;
305     const char * const *valid_cpu_types;
306     strList *allowed_dynamic_sysbus_devices;
307     bool auto_enable_numa_with_memhp;
308     bool auto_enable_numa_with_memdev;
309     bool ignore_boot_device_suffixes;
310     bool smbus_no_migration_support;
311     bool nvdimm_supported;
312     bool numa_mem_supported;
313     bool auto_enable_numa;
314     bool cpu_cluster_has_numa_boundary;
315     SMPCompatProps smp_props;
316     const char *default_ram_id;
317 
318     HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
319                                            DeviceState *dev);
320     bool (*hotplug_allowed)(MachineState *state, DeviceState *dev,
321                             Error **errp);
322     CpuInstanceProperties (*cpu_index_to_instance_props)(MachineState *machine,
323                                                          unsigned cpu_index);
324     const CPUArchIdList *(*possible_cpu_arch_ids)(MachineState *machine);
325     int64_t (*get_default_cpu_node_id)(const MachineState *ms, int idx);
326     ram_addr_t (*fixup_ram_size)(ram_addr_t size);
327     uint64_t smbios_memory_device_size;
328     bool (*create_default_memdev)(MachineState *ms, const char *path,
329                                   Error **errp);
330 };
331 
332 /**
333  * DeviceMemoryState:
334  * @base: address in guest physical address space where the memory
335  * address space for memory devices starts
336  * @mr: memory region container for memory devices
337  * @as: address space for memory devices
338  * @listener: memory listener used to track used memslots in the address space
339  * @dimm_size: the sum of plugged DIMMs' sizes
340  * @used_region_size: the part of @mr already used by memory devices
341  * @required_memslots: the number of memslots required by memory devices
342  * @used_memslots: the number of memslots currently used by memory devices
343  * @memslot_auto_decision_active: whether any plugged memory device
344  *                                automatically decided to use more than
345  *                                one memslot
346  */
347 typedef struct DeviceMemoryState {
348     hwaddr base;
349     MemoryRegion mr;
350     AddressSpace as;
351     MemoryListener listener;
352     uint64_t dimm_size;
353     uint64_t used_region_size;
354     unsigned int required_memslots;
355     unsigned int used_memslots;
356     unsigned int memslot_auto_decision_active;
357 } DeviceMemoryState;
358 
359 /**
360  * CpuTopology:
361  * @cpus: the number of present logical processors on the machine
362  * @drawers: the number of drawers on the machine
363  * @books: the number of books in one drawer
364  * @sockets: the number of sockets in one book
365  * @dies: the number of dies in one socket
366  * @clusters: the number of clusters in one die
367  * @modules: the number of modules in one cluster
368  * @cores: the number of cores in one cluster
369  * @threads: the number of threads in one core
370  * @max_cpus: the maximum number of logical processors on the machine
371  */
372 typedef struct CpuTopology {
373     unsigned int cpus;
374     unsigned int drawers;
375     unsigned int books;
376     unsigned int sockets;
377     unsigned int dies;
378     unsigned int clusters;
379     unsigned int modules;
380     unsigned int cores;
381     unsigned int threads;
382     unsigned int max_cpus;
383 } CpuTopology;
384 
385 typedef struct SmpCache {
386     SmpCacheProperties props[CACHE_LEVEL_AND_TYPE__MAX];
387 } SmpCache;
388 
389 /**
390  * MachineState:
391  */
392 struct MachineState {
393     /*< private >*/
394     Object parent_obj;
395 
396     /*< public >*/
397 
398     void *fdt;
399     char *dtb;
400     char *dumpdtb;
401     int phandle_start;
402     char *dt_compatible;
403     bool dump_guest_core;
404     bool mem_merge;
405     bool usb;
406     bool usb_disabled;
407     char *firmware;
408     bool iommu;
409     bool suppress_vmdesc;
410     bool enable_graphics;
411     ConfidentialGuestSupport *cgs;
412     HostMemoryBackend *memdev;
413     bool aux_ram_share;
414     /*
415      * convenience alias to ram_memdev_id backend memory region
416      * or to numa container memory region
417      */
418     MemoryRegion *ram;
419     DeviceMemoryState *device_memory;
420 
421     /*
422      * Included in MachineState for simplicity, but not supported
423      * unless machine_add_audiodev_property is called.  Boards
424      * that have embedded audio devices can call it from the
425      * machine init function and forward the property to the device.
426      */
427     char *audiodev;
428 
429     ram_addr_t ram_size;
430     ram_addr_t maxram_size;
431     uint64_t   ram_slots;
432     BootConfiguration boot_config;
433     char *kernel_filename;
434     char *kernel_cmdline;
435     char *shim_filename;
436     char *initrd_filename;
437     const char *cpu_type;
438     AccelState *accelerator;
439     CPUArchIdList *possible_cpus;
440     CpuTopology smp;
441     SmpCache smp_cache;
442     struct NVDIMMState *nvdimms_state;
443     struct NumaState *numa_state;
444 };
445 
446 /*
447  * The macros which follow are intended to facilitate the
448  * definition of versioned machine types, using a somewhat
449  * similar pattern across targets.
450  *
451  * For example, a macro that can be used to define versioned
452  * 'virt' machine types would look like:
453  *
454  *  #define DEFINE_VIRT_MACHINE_IMPL(latest, ...) \
455  *      static void MACHINE_VER_SYM(class_init, virt, __VA_ARGS__)( \
456  *          ObjectClass *oc, \
457  *          void *data) \
458  *      { \
459  *          MachineClass *mc = MACHINE_CLASS(oc); \
460  *          MACHINE_VER_SYM(options, virt, __VA_ARGS__)(mc); \
461  *          mc->desc = "QEMU " MACHINE_VER_STR(__VA_ARGS__) " Virtual Machine"; \
462  *          MACHINE_VER_DEPRECATION(__VA_ARGS__); \
463  *          if (latest) { \
464  *              mc->alias = "virt"; \
465  *          } \
466  *      } \
467  *      static const TypeInfo MACHINE_VER_SYM(info, virt, __VA_ARGS__) = { \
468  *          .name = MACHINE_VER_TYPE_NAME("virt", __VA_ARGS__), \
469  *          .parent = TYPE_VIRT_MACHINE, \
470  *          .class_init = MACHINE_VER_SYM(class_init, virt, __VA_ARGS__), \
471  *      }; \
472  *      static void MACHINE_VER_SYM(register, virt, __VA_ARGS__)(void) \
473  *      { \
474  *          MACHINE_VER_DELETION(__VA_ARGS__); \
475  *          type_register_static(&MACHINE_VER_SYM(info, virt, __VA_ARGS__)); \
476  *      } \
477  *      type_init(MACHINE_VER_SYM(register, virt, __VA_ARGS__));
478  *
479  * Following this, one (or more) helpers can be added for
480  * whichever scenarios need to be catered for with a machine:
481  *
482  *  // Normal 2 digit, marked as latest e.g. 'virt-9.0'
483  *  #define DEFINE_VIRT_MACHINE_LATEST(major, minor) \
484  *      DEFINE_VIRT_MACHINE_IMPL(true, major, minor)
485  *
486  *  // Normal 2 digit e.g. 'virt-9.0'
487  *  #define DEFINE_VIRT_MACHINE(major, minor) \
488  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor)
489  *
490  *  // Bugfix 3 digit e.g. 'virt-9.0.1'
491  *  #define DEFINE_VIRT_MACHINE_BUGFIX(major, minor, micro) \
492  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor, micro)
493  *
494  *  // Tagged 2 digit e.g. 'virt-9.0-extra'
495  *  #define DEFINE_VIRT_MACHINE_TAGGED(major, minor, tag) \
496  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor, _, tag)
497  *
498  *  // Tagged bugfix 2 digit e.g. 'virt-9.0.1-extra'
499  *  #define DEFINE_VIRT_MACHINE_TAGGED(major, minor, micro, tag) \
500  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor, micro, _, tag)
501  */
502 
503 /*
504  * Helper for dispatching different macros based on how
505  * many __VA_ARGS__ are passed. Supports 1 to 5 variadic
506  * arguments, with the called target able to be prefixed
507  * with 0 or more fixed arguments too. To be called thus:
508  *
509  *  _MACHINE_VER_PICK(__VA_ARGS,
510  *                    MACRO_MATCHING_5_ARGS,
511  *                    MACRO_MATCHING_4_ARGS,
512  *                    MACRO_MATCHING_3_ARGS,
513  *                    MACRO_MATCHING_2_ARGS,
514  *                    MACRO_MATCHING_1_ARG) (FIXED-ARG-1,
515  *                                           ...,
516  *                                           FIXED-ARG-N,
517  *                                           __VA_ARGS__)
518  */
519 #define _MACHINE_VER_PICK(x1, x2, x3, x4, x5, x6, ...) x6
520 
521 /*
522  * Construct a human targeted machine version string.
523  *
524  * Can be invoked with various signatures
525  *
526  *  MACHINE_VER_STR(sym, prefix, major, minor)
527  *  MACHINE_VER_STR(sym, prefix, major, minor, micro)
528  *  MACHINE_VER_STR(sym, prefix, major, minor, _, tag)
529  *  MACHINE_VER_STR(sym, prefix, major, minor, micro, _, tag)
530  *
531  * Respectively emitting symbols with the format
532  *
533  *   "{major}.{minor}"
534  *   "{major}.{minor}-{tag}"
535  *   "{major}.{minor}.{micro}"
536  *   "{major}.{minor}.{micro}-{tag}"
537  */
538 #define _MACHINE_VER_STR2(major, minor) \
539     #major "." #minor
540 
541 #define _MACHINE_VER_STR3(major, minor, micro) \
542     #major "." #minor "." #micro
543 
544 #define _MACHINE_VER_STR4(major, minor, _unused_, tag) \
545     #major "." #minor "-" #tag
546 
547 #define _MACHINE_VER_STR5(major, minor, micro, _unused_, tag) \
548     #major "." #minor "." #micro "-" #tag
549 
550 #define MACHINE_VER_STR(...) \
551     _MACHINE_VER_PICK(__VA_ARGS__, \
552                       _MACHINE_VER_STR5, \
553                       _MACHINE_VER_STR4, \
554                       _MACHINE_VER_STR3, \
555                       _MACHINE_VER_STR2) (__VA_ARGS__)
556 
557 
558 /*
559  * Construct a QAPI type name for a versioned machine
560  * type
561  *
562  * Can be invoked with various signatures
563  *
564  *  MACHINE_VER_TYPE_NAME(prefix, major, minor)
565  *  MACHINE_VER_TYPE_NAME(prefix, major, minor, micro)
566  *  MACHINE_VER_TYPE_NAME(prefix, major, minor, _, tag)
567  *  MACHINE_VER_TYPE_NAME(prefix, major, minor, micro, _, tag)
568  *
569  * Respectively emitting symbols with the format
570  *
571  *   "{prefix}-{major}.{minor}"
572  *   "{prefix}-{major}.{minor}.{micro}"
573  *   "{prefix}-{major}.{minor}-{tag}"
574  *   "{prefix}-{major}.{minor}.{micro}-{tag}"
575  */
576 #define _MACHINE_VER_TYPE_NAME2(prefix, major, minor)   \
577     prefix "-" #major "." #minor TYPE_MACHINE_SUFFIX
578 
579 #define _MACHINE_VER_TYPE_NAME3(prefix, major, minor, micro) \
580     prefix "-" #major "." #minor "." #micro TYPE_MACHINE_SUFFIX
581 
582 #define _MACHINE_VER_TYPE_NAME4(prefix, major, minor, _unused_, tag) \
583     prefix "-" #major "." #minor "-" #tag TYPE_MACHINE_SUFFIX
584 
585 #define _MACHINE_VER_TYPE_NAME5(prefix, major, minor, micro, _unused_, tag) \
586     prefix "-" #major "." #minor "." #micro "-" #tag TYPE_MACHINE_SUFFIX
587 
588 #define MACHINE_VER_TYPE_NAME(prefix, ...) \
589     _MACHINE_VER_PICK(__VA_ARGS__, \
590                       _MACHINE_VER_TYPE_NAME5, \
591                       _MACHINE_VER_TYPE_NAME4, \
592                       _MACHINE_VER_TYPE_NAME3, \
593                       _MACHINE_VER_TYPE_NAME2) (prefix, __VA_ARGS__)
594 
595 /*
596  * Construct a name for a versioned machine type that is
597  * suitable for use as a C symbol (function/variable/etc).
598  *
599  * Can be invoked with various signatures
600  *
601  *  MACHINE_VER_SYM(sym, prefix, major, minor)
602  *  MACHINE_VER_SYM(sym, prefix, major, minor, micro)
603  *  MACHINE_VER_SYM(sym, prefix, major, minor, _, tag)
604  *  MACHINE_VER_SYM(sym, prefix, major, minor, micro, _, tag)
605  *
606  * Respectively emitting symbols with the format
607  *
608  *   {prefix}_machine_{major}_{minor}_{sym}
609  *   {prefix}_machine_{major}_{minor}_{micro}_{sym}
610  *   {prefix}_machine_{major}_{minor}_{tag}_{sym}
611  *   {prefix}_machine_{major}_{minor}_{micro}_{tag}_{sym}
612  */
613 #define _MACHINE_VER_SYM2(sym, prefix, major, minor) \
614     prefix ## _machine_ ## major ## _ ## minor ## _ ## sym
615 
616 #define _MACHINE_VER_SYM3(sym, prefix, major, minor, micro) \
617     prefix ## _machine_ ## major ## _ ## minor ## _ ## micro ## _ ## sym
618 
619 #define _MACHINE_VER_SYM4(sym, prefix, major, minor, _unused_, tag) \
620     prefix ## _machine_ ## major ## _ ## minor ## _ ## tag ## _ ## sym
621 
622 #define _MACHINE_VER_SYM5(sym, prefix, major, minor, micro, _unused_, tag) \
623     prefix ## _machine_ ## major ## _ ## minor ## _ ## micro ## _ ## tag ## _ ## sym
624 
625 #define MACHINE_VER_SYM(sym, prefix, ...) \
626     _MACHINE_VER_PICK(__VA_ARGS__, \
627                       _MACHINE_VER_SYM5, \
628                       _MACHINE_VER_SYM4, \
629                       _MACHINE_VER_SYM3, \
630                       _MACHINE_VER_SYM2) (sym, prefix, __VA_ARGS__)
631 
632 
633 /*
634  * How many years/major releases for each phase
635  * of the life cycle. Assumes use of versioning
636  * scheme where major is bumped each year
637  */
638 #define MACHINE_VER_DELETION_MAJOR 6
639 #define MACHINE_VER_DEPRECATION_MAJOR 3
640 
641 /*
642  * Expands to a static string containing a deprecation
643  * message for a versioned machine type
644  */
645 #define MACHINE_VER_DEPRECATION_MSG \
646     "machines more than " stringify(MACHINE_VER_DEPRECATION_MAJOR) \
647     " years old are subject to deletion after " \
648     stringify(MACHINE_VER_DELETION_MAJOR) " years"
649 
650 #define _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor) \
651     (((QEMU_VERSION_MAJOR - major) > cutoff) || \
652      (((QEMU_VERSION_MAJOR - major) == cutoff) && \
653       (QEMU_VERSION_MINOR - minor) >= 0))
654 
655 #define _MACHINE_VER_IS_EXPIRED2(cutoff, major, minor) \
656     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
657 #define _MACHINE_VER_IS_EXPIRED3(cutoff, major, minor, micro) \
658     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
659 #define _MACHINE_VER_IS_EXPIRED4(cutoff, major, minor, _unused, tag) \
660     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
661 #define _MACHINE_VER_IS_EXPIRED5(cutoff, major, minor, micro, _unused, tag)   \
662     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
663 
664 #define _MACHINE_IS_EXPIRED(cutoff, ...) \
665     _MACHINE_VER_PICK(__VA_ARGS__, \
666                       _MACHINE_VER_IS_EXPIRED5, \
667                       _MACHINE_VER_IS_EXPIRED4, \
668                       _MACHINE_VER_IS_EXPIRED3, \
669                       _MACHINE_VER_IS_EXPIRED2) (cutoff, __VA_ARGS__)
670 
671 /*
672  * Evaluates true when a machine type with (major, minor)
673  * or (major, minor, micro) version should be considered
674  * deprecated based on the current versioned machine type
675  * lifecycle rules
676  */
677 #define MACHINE_VER_IS_DEPRECATED(...) \
678     _MACHINE_IS_EXPIRED(MACHINE_VER_DEPRECATION_MAJOR, __VA_ARGS__)
679 
680 /*
681  * Evaluates true when a machine type with (major, minor)
682  * or (major, minor, micro) version should be considered
683  * for deletion based on the current versioned machine type
684  * lifecycle rules
685  */
686 #define MACHINE_VER_SHOULD_DELETE(...) \
687     _MACHINE_IS_EXPIRED(MACHINE_VER_DELETION_MAJOR, __VA_ARGS__)
688 
689 /*
690  * Sets the deprecation reason for a versioned machine based
691  * on its age
692  *
693  * This must be unconditionally used in the _class_init
694  * function for all machine types which support versioning.
695  *
696  * Initially it will effectively be a no-op, but after a
697  * suitable period of time has passed, it will set the
698  * 'deprecation_reason' field on the machine, to warn users
699  * about forthcoming removal.
700  */
701 #define MACHINE_VER_DEPRECATION(...) \
702     do { \
703         if (MACHINE_VER_IS_DEPRECATED(__VA_ARGS__)) { \
704             mc->deprecation_reason = MACHINE_VER_DEPRECATION_MSG; \
705         } \
706     } while (0)
707 
708 /*
709  * Prevents registration of a versioned machined based on
710  * its age
711  *
712  * This must be unconditionally used in the register
713  * method for all machine types which support versioning.
714  *
715  * Inijtially it will effectively be a no-op, but after a
716  * suitable period of time has passed, it will cause
717  * execution of the method to return, avoiding registration
718  * of the machine
719  *
720  * The new deprecation and deletion policy for versioned
721  * machine types was introduced in QEMU 9.1.0.
722  *
723  * Under the new policy a number of old machine types (any
724  * prior to 2.12) would be liable for immediate deletion
725  * which would be a violation of our historical deprecation
726  * and removal policy
727  *
728  * Thus deletions are temporarily gated on existance of
729  * the env variable "QEMU_DELETE_MACHINES" / QEMU version
730  * number >= 10.1.0. This gate can be deleted in the 10.1.0
731  * dev cycle
732  */
733 #define MACHINE_VER_DELETION(...) \
734     do { \
735         if (MACHINE_VER_SHOULD_DELETE(__VA_ARGS__)) { \
736             if (getenv("QEMU_DELETE_MACHINES") || \
737                 QEMU_VERSION_MAJOR > 10 || (QEMU_VERSION_MAJOR == 10 && \
738                                             QEMU_VERSION_MINOR >= 1)) { \
739                 return; \
740             } \
741         } \
742     } while (0)
743 
744 #define DEFINE_MACHINE(namestr, machine_initfn) \
745     static void machine_initfn##_class_init(ObjectClass *oc, void *data) \
746     { \
747         MachineClass *mc = MACHINE_CLASS(oc); \
748         machine_initfn(mc); \
749     } \
750     static const TypeInfo machine_initfn##_typeinfo = { \
751         .name       = MACHINE_TYPE_NAME(namestr), \
752         .parent     = TYPE_MACHINE, \
753         .class_init = machine_initfn##_class_init, \
754     }; \
755     static void machine_initfn##_register_types(void) \
756     { \
757         type_register_static(&machine_initfn##_typeinfo); \
758     } \
759     type_init(machine_initfn##_register_types)
760 
761 extern GlobalProperty hw_compat_9_2[];
762 extern const size_t hw_compat_9_2_len;
763 
764 extern GlobalProperty hw_compat_9_1[];
765 extern const size_t hw_compat_9_1_len;
766 
767 extern GlobalProperty hw_compat_9_0[];
768 extern const size_t hw_compat_9_0_len;
769 
770 extern GlobalProperty hw_compat_8_2[];
771 extern const size_t hw_compat_8_2_len;
772 
773 extern GlobalProperty hw_compat_8_1[];
774 extern const size_t hw_compat_8_1_len;
775 
776 extern GlobalProperty hw_compat_8_0[];
777 extern const size_t hw_compat_8_0_len;
778 
779 extern GlobalProperty hw_compat_7_2[];
780 extern const size_t hw_compat_7_2_len;
781 
782 extern GlobalProperty hw_compat_7_1[];
783 extern const size_t hw_compat_7_1_len;
784 
785 extern GlobalProperty hw_compat_7_0[];
786 extern const size_t hw_compat_7_0_len;
787 
788 extern GlobalProperty hw_compat_6_2[];
789 extern const size_t hw_compat_6_2_len;
790 
791 extern GlobalProperty hw_compat_6_1[];
792 extern const size_t hw_compat_6_1_len;
793 
794 extern GlobalProperty hw_compat_6_0[];
795 extern const size_t hw_compat_6_0_len;
796 
797 extern GlobalProperty hw_compat_5_2[];
798 extern const size_t hw_compat_5_2_len;
799 
800 extern GlobalProperty hw_compat_5_1[];
801 extern const size_t hw_compat_5_1_len;
802 
803 extern GlobalProperty hw_compat_5_0[];
804 extern const size_t hw_compat_5_0_len;
805 
806 extern GlobalProperty hw_compat_4_2[];
807 extern const size_t hw_compat_4_2_len;
808 
809 extern GlobalProperty hw_compat_4_1[];
810 extern const size_t hw_compat_4_1_len;
811 
812 extern GlobalProperty hw_compat_4_0[];
813 extern const size_t hw_compat_4_0_len;
814 
815 extern GlobalProperty hw_compat_3_1[];
816 extern const size_t hw_compat_3_1_len;
817 
818 extern GlobalProperty hw_compat_3_0[];
819 extern const size_t hw_compat_3_0_len;
820 
821 extern GlobalProperty hw_compat_2_12[];
822 extern const size_t hw_compat_2_12_len;
823 
824 extern GlobalProperty hw_compat_2_11[];
825 extern const size_t hw_compat_2_11_len;
826 
827 extern GlobalProperty hw_compat_2_10[];
828 extern const size_t hw_compat_2_10_len;
829 
830 extern GlobalProperty hw_compat_2_9[];
831 extern const size_t hw_compat_2_9_len;
832 
833 extern GlobalProperty hw_compat_2_8[];
834 extern const size_t hw_compat_2_8_len;
835 
836 extern GlobalProperty hw_compat_2_7[];
837 extern const size_t hw_compat_2_7_len;
838 
839 extern GlobalProperty hw_compat_2_6[];
840 extern const size_t hw_compat_2_6_len;
841 
842 extern GlobalProperty hw_compat_2_5[];
843 extern const size_t hw_compat_2_5_len;
844 
845 extern GlobalProperty hw_compat_2_4[];
846 extern const size_t hw_compat_2_4_len;
847 
848 #endif
849