From patchwork Tue Jun 27 16:09:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 696885 Delivered-To: patch@linaro.org Received: by 2002:adf:e885:0:0:0:0:0 with SMTP id d5csp3867769wrm; Tue, 27 Jun 2023 09:16:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5+3PMOz69OdvBPZ0JZF0YSsiyjWgf98ioKCGU74ZEggi9CRTY48jq4SxOZ7Sfu4853VX3P X-Received: by 2002:aca:1919:0:b0:3a1:eb15:5ec4 with SMTP id l25-20020aca1919000000b003a1eb155ec4mr4572454oii.42.1687882608072; Tue, 27 Jun 2023 09:16:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687882608; cv=none; d=google.com; s=arc-20160816; b=ME3INXQQQ/XwrKkhDBhKCIv2z37iG7inj+5Em9XkBws7FWQDesGO3c67WOLKIQWrnq SMA15ZuPFwJlvxojEZ+07UW6OuyPlWRLrivExAI+85q7lSXpzW3TPzAyj0yZjZTnl4lQ 0W4Cw4A+T2UYZrHhQ2ZGpCJ3eCw28jSiEwMrJnPkLaOUEwQkpKedHvdHVkLFl24nGrny 8CzzuqqlaQhcrtNuCPvTVNiuLC0D/8ZKLRAtV+3VJwsEjs+K+jIqhv5gncpcSkzx7H6M 4kti7qwQ/MVGIuJmghCQyAATpPsH+895cQc8WHXaZIPBUAS4GlfzbiUUUO0/A3E4+/Eq m1xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5iMtZ+QxrIdtSm3RSwhWDx4L6tPX96mwOLg/Wme4Yys=; fh=ArV5Yx1z9lg+jufSSsiXTUg87d1rJ5PmrQDHLSB2i58=; b=RG8NvxqeSZnZP806sYGG9udxQdXBdQtxkr3MGdfSdyZ/FEOfFnHyzyrL6C1j3XKE0f T3z7Xj/Qj/StGq4YI0E8cCATREPj74460+FXxFDCCwGGOY066OCEysiExISJp4oOegga R4G0a2gcLZBV6BUr459M9ethJLvhI6ftsdzKmwr2wm5RNKc1JWcw4wACokNCsLdLYHMG O/mPd/H3iqI/zHdlREs65YOg4q2q260nKkLXe/H+hJQLpExtiujbiYz5wLdH5SfHtdIl 6bMZUXj63ISOmEVVoL4XORF3aYFPJgtJ7NivUJSdVzofY7YiTGLYOqU3NNi9SbxhKWNx I8lA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WIwXrl4G; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id j12-20020ac85f8c000000b003f6b50d68d0si3312478qta.195.2023.06.27.09.16.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Jun 2023 09:16:48 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WIwXrl4G; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEBMX-0001Rw-Nh; Tue, 27 Jun 2023 12:16:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEBMN-0001Bo-ET for qemu-devel@nongnu.org; Tue, 27 Jun 2023 12:16:07 -0400 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qEBMD-0008Ag-2t for qemu-devel@nongnu.org; Tue, 27 Jun 2023 12:16:07 -0400 Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-3113dabc549so6452534f8f.1 for ; Tue, 27 Jun 2023 09:15:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687882555; x=1690474555; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5iMtZ+QxrIdtSm3RSwhWDx4L6tPX96mwOLg/Wme4Yys=; b=WIwXrl4Gv7fagv2WaOmuGRYEhm6HA7QHFCHMfzGCh0mFtdxTIXrfMj0XHnfuWGZOlJ zJ4ilcGfqA9guABVs0P5GFfadjybkcpbFTsjQTf360qmX3vuZervAwmkv2oRxr40z1cy YX6cYsC2Djqcwqm2OLPHWJtbSlc8L0ispKCVylVCH/WSW2IanxX07I3G9+8dIu5uPtzx LSd/Xhq6/oq6qmC4VU8U2EvHefqClXTs3r9FHX5DOnnouFhQybYWUILgGSit5JHYX7Xs al2PkEqPwv5owc8veN5RTEgtPtfIjX+uLGq3kgh/6UzzEZfqIfxHdYQYDr+boZXn1ujo BDgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687882555; x=1690474555; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5iMtZ+QxrIdtSm3RSwhWDx4L6tPX96mwOLg/Wme4Yys=; b=jpoc+1FJ4CumE+xY0ZjFwzcKfCeqT/HlU2TzJK45qeTjxZTJu7bj1DNAq/sWDuTOYn BcKVWHrO48kovUxlojG/B/AQtMP0hVPvL2kzN5XWTDC0U7YIEd67stmMBNwcDAfM25Vq L69vg4YCpmpwp1Ghu6Sy9CHP4cqAzbIb52vrMp5YS1rj27ID6K66dfWVNmPFMmmpSeds QE7ei5sAVzdsHRnk0IjooFr5nh6An+J0jvA+MbPE3mxYux7Ex8kAJCHWZBjV1GmC29fW oHSqgw7bOkOk+AlaI93rYGJomt6h/JaTVqZz/yoD7laZ67bP+qgF9A1i8VOMYkb2U1vX m2KQ== X-Gm-Message-State: AC+VfDy5MkKvjSJy/ABUM+NcUbSgBNXRq7yEH7XBbU2L8r6yDFjEm54v BX/mi1GjC7SNz2LnbELb48kbeg== X-Received: by 2002:a5d:68c5:0:b0:313:eb34:b23e with SMTP id p5-20020a5d68c5000000b00313eb34b23emr9468672wrw.49.1687882555650; Tue, 27 Jun 2023 09:15:55 -0700 (PDT) Received: from zen.linaroharston ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id i18-20020a5d5232000000b003047ae72b14sm10885501wra.82.2023.06.27.09.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 09:15:53 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id E2BB41FFD1; Tue, 27 Jun 2023 17:09:46 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Wainer dos Santos Moschetta , Juan Quintela , Thomas Huth , Cleber Rosa , Leonardo Bras , Beraldo Leal , Peter Maydell , Bin Meng , Yanan Wang , Darren Kenny , Alexander Bulekov , Marcel Apfelbaum , Peter Xu , Radoslaw Biernacki , Laurent Vivier , Paolo Bonzini , Eduardo Habkost , qemu-arm@nongnu.org, Stefan Hajnoczi , Richard Henderson , Bandan Das , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , =?utf-8?q?Dani?= =?utf-8?q?el_P=2E_Berrang=C3=A9?= , Alexandre Iooss , Marcin Juszkiewicz , Leif Lindholm , Laurent Vivier , Qiuhao Li , Mahmoud Mandour , Riku Voipio , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH v3 25/36] docs/devel: introduce some key concepts for QOM development Date: Tue, 27 Jun 2023 17:09:32 +0100 Message-Id: <20230627160943.2956928-26-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230627160943.2956928-1-alex.bennee@linaro.org> References: <20230627160943.2956928-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42a; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x42a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Using QOM correctly is increasingly important to maintaining a modern code base. However the current documentation skips some important concepts before launching into a simple example. Lets: - at least mention properties - mention TYPE_OBJECT and TYPE_DEVICE - talk about why we have realize/unrealize - mention the QOM tree - lightly re-arrange the order we mention things Signed-off-by: Alex Bennée Message-Id: <20230619171437.357374-6-alex.bennee@linaro.org> Reviewed-by: Philippe Mathieu-Daudé --- v3 - moved around as per Paolo's review --- docs/devel/qom.rst | 58 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/docs/devel/qom.rst b/docs/devel/qom.rst index c342ce18e3..0b506426d7 100644 --- a/docs/devel/qom.rst +++ b/docs/devel/qom.rst @@ -13,6 +13,24 @@ features: - System for dynamically registering types - Support for single-inheritance of types - Multiple inheritance of stateless interfaces +- Mapping internal members to publicly exposed properties + +The root object class is TYPE_OBJECT which provides for the basic +object methods. + +The QOM tree +============ + +The QOM tree is a composition tree which represents all of the objects +that make up a QEMU "machine". You can view this tree by running +``info qom-tree`` in the :ref:`QEMU monitor`. It will contain both +objects created by the machine itself as well those created due to +user configuration. + +Creating a QOM class +==================== + +A simple minimal device implementation may look something like bellow: .. code-block:: c :caption: Creating a minimal type @@ -48,6 +66,12 @@ In the above example, we create a simple type that is described by #TypeInfo. #TypeInfo describes information about the type including what it inherits from, the instance and class size, and constructor/destructor hooks. +The TYPE_DEVICE class is the parent class for all modern devices +implemented in QEMU and adds some specific methods to handle QEMU +device model. This includes managing the lifetime of devices from +creation through to when they become visible to the guest and +eventually unrealized. + Alternatively several static types could be registered using helper macro DEFINE_TYPES() @@ -98,7 +122,7 @@ when the object is needed. module_obj(TYPE_MY_DEVICE); Class Initialization -==================== +-------------------- Before an object is initialized, the class for the object must be initialized. There is only one class object for all instance objects @@ -168,7 +192,7 @@ will also have a wrapper function to call it easily: } Interfaces -========== +---------- Interfaces allow a limited form of multiple inheritance. Instances are similar to normal types except for the fact that are only defined by @@ -182,7 +206,7 @@ an argument to a method on its corresponding SomethingIfClass, or to dynamically cast it to an object that implements the interface. Methods -======= +------- A *method* is a function within the namespace scope of a class. It usually operates on the object instance by passing it as a @@ -275,8 +299,8 @@ Alternatively, object_class_by_name() can be used to obtain the class and its non-overridden methods for a specific type. This would correspond to ``MyClass::method(...)`` in C++. -The first example of such a QOM method was #CPUClass.reset, -another example is #DeviceClass.realize. +One example of such methods is ``DeviceClass.reset``. More examples +can be found at :ref:`device-life-cycle`. Standard type declaration and definition macros =============================================== @@ -382,10 +406,32 @@ OBJECT_DEFINE_ABSTRACT_TYPE() macro can be used instead: OBJECT_DEFINE_ABSTRACT_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE) +.. _device-life-cycle: + +Device Life-cycle +================= + +As class initialisation cannot fail devices have an two additional +methods to handle the creation of dynamic devices. The ``realize`` +function is called with ``Error **`` pointer which should be set if +the device cannot complete its setup. Otherwise on successful +completion of the ``realize`` method the device object is added to the +QOM tree and made visible to the guest. + +The reverse function is ``unrealize`` and should be were clean-up +code lives to tidy up after the system is done with the device. + +All devices can be instantiated by C code, however only some can +created dynamically via the command line or monitor. +Likewise only some can be unplugged after creation and need an +explicit ``unrealize`` implementation. This is determined by the +``user_creatable`` variable in the root ``DeviceClass`` structure. +Devices can only be unplugged if their ``parent_bus`` has a registered +``HotplugHandler``. API Reference -------------- +============= See the :ref:`QOM API` and :ref:`QDEV API` documents for the complete API description.