This can be used to declare a type as being target specific.

Implementing the interface just records the target metadata,
applying the enforcement requires the object creation path to
call 'target_specific_class_check()' to validate the system
state.

This approach is intended to allow classes to be unconditionally
registered, which allows introspection at any time. The target
specific enforcement is applied at runtime in the object
creation codepaths that need to apply restrictions. This implies
that QOM class names must be globally unique across all targets
known to QEMU.

Signed-off-by: Daniel P. Berrangé <[email protected]>
---
 include/qom/object_interfaces.h | 75 +++++++++++++++++++++++++++++++++
 qom/object_interfaces.c         | 31 ++++++++++++++
 2 files changed, 106 insertions(+)

diff --git a/include/qom/object_interfaces.h b/include/qom/object_interfaces.h
index afd0fb93b2b..d222a3d7481 100644
--- a/include/qom/object_interfaces.h
+++ b/include/qom/object_interfaces.h
@@ -16,6 +16,17 @@ DECLARE_CLASS_CHECKERS(UserCreatableClass, USER_CREATABLE,
 
 typedef struct UserCreatable UserCreatable;
 
+#define TYPE_TARGET_SPECIFIC "target-specific"
+
+typedef struct TargetSpecificClass TargetSpecificClass;
+DECLARE_CLASS_CHECKERS(TargetSpecificClass, TARGET_SPECIFIC,
+                       TYPE_TARGET_SPECIFIC)
+#define TARGET_SPECIFIC(obj) \
+     INTERFACE_CHECK(TargetSpecific, (obj), \
+                     TYPE_TARGET_SPECIFIC)
+
+typedef struct TargetSpecific TargetSpecific;
+
 /**
  * UserCreatableClass:
  * @parent_class: the base class
@@ -167,4 +178,68 @@ bool user_creatable_del(const char *id, Error **errp);
  */
 void user_creatable_cleanup(void);
 
+/**
+ * TargetSpecificClass:
+ * @parent_class: the base class
+ *
+ * Interface for classes that want to declare they have target
+ * specific restrictions. This is intended to allow classes to
+ * be registered and introspected unconditionally, while
+ * preventing creation of instances unless a compatible target
+ * is selected.
+ */
+struct TargetSpecificClass {
+    InterfaceClass parent_class;
+
+    /*
+     * Leave as 0 to indicate no target specific creation restrictions,
+     * or set to a bitmask of QEMU_ARCH_nnn constants to limit what
+     * targets it can be created under.
+     */
+    int targets;
+};
+
+
+/**
+ * target_specific_class_creatable:
+ * @oc: the object class to check
+ *
+ * Determine whether @oc implements the TargetSpecific
+ * interface and if so, whether its declared targets
+ * allow creation in the current system state.
+ *
+ * If @oc does not implement the TargetSpecific interface
+ * it is assumed there are no creation restrictions.
+ *
+ * If @oc implements the TargetSpecific interface but leaves
+ * the 'targets' field as zero, it is assumed there are
+ * no creation restrictions.
+ *
+ * If @oc implements the TargetSpecific interface and the
+ * 'targets' field is non-zero, it is compared to
+ * target_mask_selected() to determine if it is valid to
+ * create an instance of @oc.
+ *
+ * Returns: true if it is valid to create an instance of @oc
+ * in the current system state, false otherwise.
+ */
+bool target_specific_class_creatable(ObjectClass *oc);
+
+
+/**
+ * target_specific_class_check:
+ * @oc: the object class to check
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Check whether it is permitted to create an instance
+ * of @oc in the current system target state.
+ *
+ * This validates @oc against target_specific_class_creatable
+ * and sets @errp if it is not creatable.
+ *
+ * Returns: @oc if it is creatable, NULL otherwise with @errp set.
+ */
+ObjectClass *target_specific_class_check(ObjectClass *oc,
+                                         Error **errp);
+
 #endif
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index d83c12744b7..f523a55b93a 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -20,6 +20,7 @@
 #include "qapi/opts-visitor.h"
 #include "qemu/config-file.h"
 #include "qemu/keyval.h"
+#include "qemu/target-info.h"
 #include "trace.h"
 
 bool user_creatable_complete(UserCreatable *uc, Error **errp)
@@ -280,6 +281,30 @@ void user_creatable_cleanup(void)
     object_unparent(object_get_objects_root());
 }
 
+
+bool target_specific_class_creatable(ObjectClass *oc)
+{
+    TargetSpecificClass *tsc =
+        (TargetSpecificClass *)object_class_dynamic_cast(
+            oc, TYPE_TARGET_SPECIFIC);
+
+    return !tsc || !tsc->targets || (tsc->targets & target_mask_selected());
+}
+
+
+ObjectClass *target_specific_class_check(ObjectClass *oc,
+                                         Error **errp)
+{
+    if (!target_specific_class_creatable(oc)) {
+        error_setg(errp, "Object '%s' not compatible with selected target",
+                   object_class_get_name(oc));
+        return NULL;
+    }
+
+    return oc;
+}
+
+
 static void register_types(void)
 {
     static const TypeInfo uc_interface_info = {
@@ -287,8 +312,14 @@ static void register_types(void)
         .parent        = TYPE_INTERFACE,
         .class_size = sizeof(UserCreatableClass),
     };
+    static const TypeInfo ts_interface_info = {
+        .name          = TYPE_TARGET_SPECIFIC,
+        .parent        = TYPE_INTERFACE,
+        .class_size = sizeof(TargetSpecificClass),
+    };
 
     type_register_static(&uc_interface_info);
+    type_register_static(&ts_interface_info);
 }
 
 type_init(register_types)
-- 
2.56.1


Reply via email to