From nobody Sun Nov 9 12:52:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551120308985299.7998789254608; Mon, 25 Feb 2019 10:45:08 -0800 (PST) Received: from localhost ([127.0.0.1]:42739 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLFN-0003uS-Nn for importer@patchew.org; Mon, 25 Feb 2019 13:45:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59072) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyL8u-000796-HV for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyL8s-0005eR-JB for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45088) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyL8j-0005MS-Kw; Mon, 25 Feb 2019 13:38:11 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3F53F3016F3C; Mon, 25 Feb 2019 18:38:07 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-92.ams2.redhat.com [10.36.116.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5E0AA5D9CC; Mon, 25 Feb 2019 18:38:00 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id D1B0E1138661; Mon, 25 Feb 2019 19:37:57 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 19:37:52 +0100 Message-Id: <20190225183757.27378-2-armbru@redhat.com> In-Reply-To: <20190225183757.27378-1-armbru@redhat.com> References: <20190225183757.27378-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Mon, 25 Feb 2019 18:38:07 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 1/6] qdev: Fix latent bug with compat_props and onboard devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pkrempa@redhat.com, qemu-block@nongnu.org, mst@redhat.com, mreitz@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , pbonzini@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Compatibility properties started life as a qdev property thing: we supported them only for qdev properties, and implemented them with the machinery backing command line option -global. Recent commit fa0cb34d221 put them to use (tacitly) with memory backend objects (subtypes of TYPE_MEMORY_BACKEND). To make that possible, we first moved the work of applying them from the -global machinery into TYPE_DEVICE's .instance_post_init() method device_post_init(), in commits ea9ce8934c5 and b66bbee39f6, then made it available to TYPE_MEMORY_BACKEND's .instance_post_init() method host_memory_backend_post_init() as object_apply_compat_props(), in commit 1c3994f6d2a. Note the code smell: we now have function name starting with object_ in hw/core/qdev.c. It has to be there rather than in qom/, because it calls qdev_get_machine() to find the current accelerator's and machine's compat_props. Turns out calling qdev_get_machine() there is problematic. If we qdev_create() from a machine's .instance_init() method, we call device_post_init() and thus qdev_get_machine() before main() can create "/machine" in QOM. qdev_get_machine() tries to get it with container_get(), which "helpfully" creates it as "container" object, and returns that. object_apply_compat_props() tries to paper over the problem by doing nothing when the value of qdev_get_machine() isn't a TYPE_MACHINE. But the damage is done already: when main() later attempts to create the real "/machine", it fails with "attempt to add duplicate property 'machine' to object (type 'container')", and aborts. Since no machine .instance_init() calls qdev_create() so far, the bug is latent. But since I want to do that, I get to fix the bug first. Observe that object_apply_compat_props() doesn't actually need the MachineState, only its the compat_props member of its MachineClass and AccelClass. This permits a simple fix: register MachineClass and AccelClass compat_props with the object_apply_compat_props() machinery right after these classes get selected. This is actually similar to how things worked before commits ea9ce8934c5 and b66bbee39f6, except we now register much earlier. The old code registered them only after the machine's .instance_init() ran, which would've broken compatibility properties for any devices created there. Cc: Marc-Andr=C3=A9 Lureau Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau --- accel/accel.c | 1 + hw/core/qdev.c | 48 ++++++++++++++++++++++++++++++++---------- include/hw/qdev-core.h | 2 ++ vl.c | 1 + 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/accel/accel.c b/accel/accel.c index 68b6d56323..4a1670e404 100644 --- a/accel/accel.c +++ b/accel/accel.c @@ -66,6 +66,7 @@ static int accel_init_machine(AccelClass *acc, MachineSta= te *ms) *(acc->allowed) =3D false; object_unref(OBJECT(accel)); } + object_set_accelerator_compat_props(acc->compat_props); return ret; } =20 diff --git a/hw/core/qdev.c b/hw/core/qdev.c index d59071b8ed..1a86c7990a 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -970,25 +970,51 @@ static void device_initfn(Object *obj) QLIST_INIT(&dev->gpios); } =20 +/* + * Global property defaults + * Slot 0: accelerator's global property defaults + * Slot 1: machine's global property defaults + * Each is a GPtrArray of of GlobalProperty. + * Applied in order, later entries override earlier ones. + */ +static GPtrArray *object_compat_props[2]; + +/* + * Set machine's global property defaults to @compat_props. + * May be called at most once. + */ +void object_set_machine_compat_props(GPtrArray *compat_props) +{ + assert(!object_compat_props[1]); + object_compat_props[1] =3D compat_props; +} + +/* + * Set accelerator's global property defaults to @compat_props. + * May be called at most once. + */ +void object_set_accelerator_compat_props(GPtrArray *compat_props) +{ + assert(!object_compat_props[0]); + object_compat_props[0] =3D compat_props; +} + void object_apply_compat_props(Object *obj) { - if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) { - MachineState *m =3D MACHINE(qdev_get_machine()); - MachineClass *mc =3D MACHINE_GET_CLASS(m); + int i; =20 - if (m->accelerator) { - AccelClass *ac =3D ACCEL_GET_CLASS(m->accelerator); - - if (ac->compat_props) { - object_apply_global_props(obj, ac->compat_props, &error_ab= ort); - } - } - object_apply_global_props(obj, mc->compat_props, &error_abort); + for (i =3D 0; i < ARRAY_SIZE(object_compat_props); i++) { + object_apply_global_props(obj, object_compat_props[i], + &error_abort); } } =20 static void device_post_init(Object *obj) { + /* + * Note: ordered so that the user's global properties take + * precedence. + */ object_apply_compat_props(obj); qdev_prop_set_globals(DEVICE(obj)); } diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 0a84c42756..bced1f2666 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -418,6 +418,8 @@ const char *qdev_fw_name(DeviceState *dev); =20 Object *qdev_get_machine(void); =20 +void object_set_machine_compat_props(GPtrArray *compat_props); +void object_set_accelerator_compat_props(GPtrArray *compat_props); void object_apply_compat_props(Object *obj); =20 /* FIXME: make this a link<> */ diff --git a/vl.c b/vl.c index 502857a176..c50c2d6178 100644 --- a/vl.c +++ b/vl.c @@ -3954,6 +3954,7 @@ int main(int argc, char **argv, char **envp) configure_rtc(qemu_find_opts_singleton("rtc")); =20 machine_class =3D select_machine(); + object_set_machine_compat_props(machine_class->compat_props); =20 set_memory_options(&ram_slots, &maxram_size, machine_class); =20 --=20 2.17.2 From nobody Sun Nov 9 12:52:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551120378125962.2638599142844; Mon, 25 Feb 2019 10:46:18 -0800 (PST) Received: from localhost ([127.0.0.1]:42779 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLGZ-0004by-5n for importer@patchew.org; Mon, 25 Feb 2019 13:46:15 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59069) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyL8u-000795-DX for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyL8s-0005f0-RZ for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:19 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45386) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyL8l-0005ON-Pp; Mon, 25 Feb 2019 13:38:12 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0BC2730A818C; Mon, 25 Feb 2019 18:38:09 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-92.ams2.redhat.com [10.36.116.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6924660C05; Mon, 25 Feb 2019 18:38:03 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id D49CB11385D4; Mon, 25 Feb 2019 19:37:57 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 19:37:53 +0100 Message-Id: <20190225183757.27378-3-armbru@redhat.com> In-Reply-To: <20190225183757.27378-1-armbru@redhat.com> References: <20190225183757.27378-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Mon, 25 Feb 2019 18:38:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 2/6] qom: Move compat_props machinery from qdev to QOM X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pkrempa@redhat.com, qemu-block@nongnu.org, mst@redhat.com, mreitz@redhat.com, pbonzini@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" See the previous commit for rationale. Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/core/qdev.c | 39 --------------------------------------- include/hw/qdev-core.h | 4 ---- include/qom/object.h | 3 +++ qom/object.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 1a86c7990a..25dffad3ed 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -970,45 +970,6 @@ static void device_initfn(Object *obj) QLIST_INIT(&dev->gpios); } =20 -/* - * Global property defaults - * Slot 0: accelerator's global property defaults - * Slot 1: machine's global property defaults - * Each is a GPtrArray of of GlobalProperty. - * Applied in order, later entries override earlier ones. - */ -static GPtrArray *object_compat_props[2]; - -/* - * Set machine's global property defaults to @compat_props. - * May be called at most once. - */ -void object_set_machine_compat_props(GPtrArray *compat_props) -{ - assert(!object_compat_props[1]); - object_compat_props[1] =3D compat_props; -} - -/* - * Set accelerator's global property defaults to @compat_props. - * May be called at most once. - */ -void object_set_accelerator_compat_props(GPtrArray *compat_props) -{ - assert(!object_compat_props[0]); - object_compat_props[0] =3D compat_props; -} - -void object_apply_compat_props(Object *obj) -{ - int i; - - for (i =3D 0; i < ARRAY_SIZE(object_compat_props); i++) { - object_apply_global_props(obj, object_compat_props[i], - &error_abort); - } -} - static void device_post_init(Object *obj) { /* diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index bced1f2666..f2f0006234 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -418,10 +418,6 @@ const char *qdev_fw_name(DeviceState *dev); =20 Object *qdev_get_machine(void); =20 -void object_set_machine_compat_props(GPtrArray *compat_props); -void object_set_accelerator_compat_props(GPtrArray *compat_props); -void object_apply_compat_props(Object *obj); - /* FIXME: make this a link<> */ void qdev_set_parent_bus(DeviceState *dev, BusState *bus); =20 diff --git a/include/qom/object.h b/include/qom/object.h index e0262962b5..288cdddf44 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -677,6 +677,9 @@ Object *object_new_with_propv(const char *typename, =20 void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp); +void object_set_machine_compat_props(GPtrArray *compat_props); +void object_set_accelerator_compat_props(GPtrArray *compat_props); +void object_apply_compat_props(Object *obj); =20 /** * object_set_props: diff --git a/qom/object.c b/qom/object.c index b8c732063b..adb9b7fe91 100644 --- a/qom/object.c +++ b/qom/object.c @@ -408,6 +408,45 @@ void object_apply_global_props(Object *obj, const GPtr= Array *props, Error **errp } } =20 +/* + * Global property defaults + * Slot 0: accelerator's global property defaults + * Slot 1: machine's global property defaults + * Each is a GPtrArray of of GlobalProperty. + * Applied in order, later entries override earlier ones. + */ +static GPtrArray *object_compat_props[2]; + +/* + * Set machine's global property defaults to @compat_props. + * May be called at most once. + */ +void object_set_machine_compat_props(GPtrArray *compat_props) +{ + assert(!object_compat_props[1]); + object_compat_props[1] =3D compat_props; +} + +/* + * Set accelerator's global property defaults to @compat_props. + * May be called at most once. + */ +void object_set_accelerator_compat_props(GPtrArray *compat_props) +{ + assert(!object_compat_props[0]); + object_compat_props[0] =3D compat_props; +} + +void object_apply_compat_props(Object *obj) +{ + int i; + + for (i =3D 0; i < ARRAY_SIZE(object_compat_props); i++) { + object_apply_global_props(obj, object_compat_props[i], + &error_abort); + } +} + static void object_initialize_with_type(void *data, size_t size, TypeImpl = *type) { Object *obj =3D data; --=20 2.17.2 From nobody Sun Nov 9 12:52:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551120122535870.1802496120006; Mon, 25 Feb 2019 10:42:02 -0800 (PST) Received: from localhost ([127.0.0.1]:42702 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLCT-0001T2-Fm for importer@patchew.org; Mon, 25 Feb 2019 13:42:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyL8t-00078L-Ch for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyL8r-0005d2-Ng for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50350) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyL8l-0005RR-Uj; Mon, 25 Feb 2019 13:38:12 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0C44E70D6D; Mon, 25 Feb 2019 18:38:10 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-92.ams2.redhat.com [10.36.116.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7124A261AC; Mon, 25 Feb 2019 18:38:00 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id D843911385D5; Mon, 25 Feb 2019 19:37:57 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 19:37:54 +0100 Message-Id: <20190225183757.27378-4-armbru@redhat.com> In-Reply-To: <20190225183757.27378-1-armbru@redhat.com> References: <20190225183757.27378-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 25 Feb 2019 18:38:10 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 3/6] vl: Fix latent bug with -global and onboard devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pkrempa@redhat.com, qemu-block@nongnu.org, mst@redhat.com, mreitz@redhat.com, pbonzini@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" main() registers the user's -global only after we create the machine object, i.e. too late for devices created in the machine's .instance_init(). Fortunately, we know the bug is only latent: the commit before previous fixed a bug that would've crashed any attempt to create a device in an .instance_init(). Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau --- vl.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/vl.c b/vl.c index c50c2d6178..e3fdce410f 100644 --- a/vl.c +++ b/vl.c @@ -2939,17 +2939,6 @@ static void user_register_global_props(void) global_init_func, NULL, NULL); } =20 -/* - * Note: we should see that these properties are actually having a - * priority: accel < machine < user. This means e.g. when user - * specifies something in "-global", it'll always be used with highest - * priority than either machine/accelerator compat properties. - */ -static void register_global_properties(MachineState *ms) -{ - user_register_global_props(); -} - int main(int argc, char **argv, char **envp) { int i; @@ -3943,6 +3932,8 @@ int main(int argc, char **argv, char **envp) */ loc_set_none(); =20 + user_register_global_props(); + replay_configure(icount_opts); =20 if (incoming && !preconfig_exit_requested) { @@ -4248,12 +4239,6 @@ int main(int argc, char **argv, char **envp) machine_class->name, machine_class->deprecation_reaso= n); } =20 - /* - * Register all the global properties, including accel properties, - * machine properties, and user-specified ones. - */ - register_global_properties(current_machine); - /* * Migration object can only be created after global properties * are applied correctly. --=20 2.17.2 From nobody Sun Nov 9 12:52:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551120057628410.3424326110212; Mon, 25 Feb 2019 10:40:57 -0800 (PST) Received: from localhost ([127.0.0.1]:42686 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLBK-0000YM-Ja for importer@patchew.org; Mon, 25 Feb 2019 13:40:50 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58983) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyL8q-00074C-LG for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyL8n-0005Wq-Gk for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:16 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45032) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyL8e-0005GA-O3; Mon, 25 Feb 2019 13:38:06 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A8D463060FDB; Mon, 25 Feb 2019 18:38:03 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-92.ams2.redhat.com [10.36.116.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 76B3A60865; Mon, 25 Feb 2019 18:38:00 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id DBEE911384DA; Mon, 25 Feb 2019 19:37:57 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 19:37:55 +0100 Message-Id: <20190225183757.27378-5-armbru@redhat.com> In-Reply-To: <20190225183757.27378-1-armbru@redhat.com> References: <20190225183757.27378-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Mon, 25 Feb 2019 18:38:03 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pkrempa@redhat.com, qemu-block@nongnu.org, mst@redhat.com, mreitz@redhat.com, pbonzini@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The first call of sysbus_get_default() creates the main system bus and stores it in QOM as "/machine/unattached/sysbus". This must not happen before main() creates "/machine", or else container_get() would "helpfully" create it as "container" object, and the real creation of "/machine" would later abort with "attempt to add duplicate property 'machine' to object (type 'container')". Has been that way ever since we wired up busses in QOM (commit f968fc6892d, v1.2.0). I believe the bug is latent. I got it to bite by trying to qdev_create() a sysbus device from a machine's .instance_init() method. The fix is obvious: store the main system bus in QOM right after creating "/machine". Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Thomas Huth --- hw/core/sysbus.c | 3 --- vl.c | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c index 9f9edbcab9..307cf90a51 100644 --- a/hw/core/sysbus.c +++ b/hw/core/sysbus.c @@ -357,9 +357,6 @@ static void main_system_bus_create(void) qbus_create_inplace(main_system_bus, system_bus_info.instance_size, TYPE_SYSTEM_BUS, NULL, "main-system-bus"); OBJECT(main_system_bus)->free =3D g_free; - object_property_add_child(container_get(qdev_get_machine(), - "/unattached"), - "sysbus", OBJECT(main_system_bus), NULL); } =20 BusState *sysbus_get_default(void) diff --git a/vl.c b/vl.c index e3fdce410f..6ce3d2d448 100644 --- a/vl.c +++ b/vl.c @@ -3990,6 +3990,10 @@ int main(int argc, char **argv, char **envp) } object_property_add_child(object_get_root(), "machine", OBJECT(current_machine), &error_abort); + object_property_add_child(container_get(OBJECT(current_machine), + "/unattached"), + "sysbus", OBJECT(sysbus_get_default()), + NULL); =20 if (machine_class->minimum_page_bits) { if (!set_preferred_target_page_bits(machine_class->minimum_page_bi= ts)) { --=20 2.17.2 From nobody Sun Nov 9 12:52:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551120200226651.4017230697121; Mon, 25 Feb 2019 10:43:20 -0800 (PST) Received: from localhost ([127.0.0.1]:42718 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLDh-0002Sn-84 for importer@patchew.org; Mon, 25 Feb 2019 13:43:17 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59017) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyL8r-00075n-Oh for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyL8o-0005Z9-Si for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:17 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50500) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyL8g-0005IX-Rt; Mon, 25 Feb 2019 13:38:07 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6AFCDC05000B; Mon, 25 Feb 2019 18:38:05 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-92.ams2.redhat.com [10.36.116.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 037ED60863; Mon, 25 Feb 2019 18:38:05 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id E52921133040; Mon, 25 Feb 2019 19:37:57 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 19:37:56 +0100 Message-Id: <20190225183757.27378-6-armbru@redhat.com> In-Reply-To: <20190225183757.27378-1-armbru@redhat.com> References: <20190225183757.27378-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 25 Feb 2019 18:38:05 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 5/6] vl: Create block backends before setting machine properties X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pkrempa@redhat.com, qemu-block@nongnu.org, mst@redhat.com, mreitz@redhat.com, pbonzini@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qemu-system-FOO's main() acts on command line arguments in its own idiosyncratic order. There's not much method to its madness. Whenever we find a case where one kind of command line argument needs to refer to something created for another kind later, we rejigger the order. Block devices get created long after machine properties get processed. Therefore, block device machine properties can be created, but not set. No such properties exist. But the next commit will create some. Time to rejigger again: create block devices earlier. Signed-off-by: Markus Armbruster Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- vl.c | 66 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/vl.c b/vl.c index 6ce3d2d448..5cb0810ffa 100644 --- a/vl.c +++ b/vl.c @@ -4232,6 +4232,39 @@ int main(int argc, char **argv, char **envp) exit(0); } =20 + /* If the currently selected machine wishes to override the units-per-= bus + * property of its default HBA interface type, do so now. */ + if (machine_class->units_per_default_bus) { + override_max_devs(machine_class->block_default_type, + machine_class->units_per_default_bus); + } + + /* open the virtual block devices */ + while (!QSIMPLEQ_EMPTY(&bdo_queue)) { + BlockdevOptions_queue *bdo =3D QSIMPLEQ_FIRST(&bdo_queue); + + QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry); + loc_push_restore(&bdo->loc); + qmp_blockdev_add(bdo->bdo, &error_fatal); + loc_pop(&bdo->loc); + qapi_free_BlockdevOptions(bdo->bdo); + g_free(bdo); + } + if (snapshot || replay_mode !=3D REPLAY_MODE_NONE) { + qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, + NULL, NULL); + } + if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, + &machine_class->block_default_type, &error_fatal= )) { + /* We printed help */ + exit(0); + } + + default_drive(default_cdrom, snapshot, machine_class->block_default_ty= pe, 2, + CDROM_OPTS); + default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS); + default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS); + machine_opts =3D qemu_get_machine_opts(); qemu_opt_foreach(machine_opts, machine_set_property, current_machine, &error_fatal); @@ -4355,39 +4388,6 @@ int main(int argc, char **argv, char **envp) ram_mig_init(); dirty_bitmap_mig_init(); =20 - /* If the currently selected machine wishes to override the units-per-= bus - * property of its default HBA interface type, do so now. */ - if (machine_class->units_per_default_bus) { - override_max_devs(machine_class->block_default_type, - machine_class->units_per_default_bus); - } - - /* open the virtual block devices */ - while (!QSIMPLEQ_EMPTY(&bdo_queue)) { - BlockdevOptions_queue *bdo =3D QSIMPLEQ_FIRST(&bdo_queue); - - QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry); - loc_push_restore(&bdo->loc); - qmp_blockdev_add(bdo->bdo, &error_fatal); - loc_pop(&bdo->loc); - qapi_free_BlockdevOptions(bdo->bdo); - g_free(bdo); - } - if (snapshot || replay_mode !=3D REPLAY_MODE_NONE) { - qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, - NULL, NULL); - } - if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, - &machine_class->block_default_type, &error_fatal= )) { - /* We printed help */ - exit(0); - } - - default_drive(default_cdrom, snapshot, machine_class->block_default_ty= pe, 2, - CDROM_OPTS); - default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS); - default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS); - qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, &error_fatal); =20 --=20 2.17.2 From nobody Sun Nov 9 12:52:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551120189582937.0694059642503; Mon, 25 Feb 2019 10:43:09 -0800 (PST) Received: from localhost ([127.0.0.1]:42710 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLDU-0002Jk-Eb for importer@patchew.org; Mon, 25 Feb 2019 13:43:04 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59138) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyL9E-0007Xh-Nb for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyL93-0005n1-JG for qemu-devel@nongnu.org; Mon, 25 Feb 2019 13:38:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50344) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyL8l-0005RJ-MR; Mon, 25 Feb 2019 13:38:11 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BBB9270D61; Mon, 25 Feb 2019 18:38:09 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-92.ams2.redhat.com [10.36.116.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 03F1B261B6; Mon, 25 Feb 2019 18:38:09 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id EA3471133056; Mon, 25 Feb 2019 19:37:57 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 19:37:57 +0100 Message-Id: <20190225183757.27378-7-armbru@redhat.com> In-Reply-To: <20190225183757.27378-1-armbru@redhat.com> References: <20190225183757.27378-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 25 Feb 2019 18:38:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 6/6] pc: Support firmware configuration with -blockdev X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pkrempa@redhat.com, qemu-block@nongnu.org, mst@redhat.com, mreitz@redhat.com, pbonzini@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The PC machines put firmware in ROM by default. To get it put into flash memory (required by OVMF), you have to use -drive if=3Dpflash,unit=3D0,... and optionally -drive if=3Dpflash,unit=3D1,... Why two -drive? This permits setting up one part of the flash memory read-only, and the other part read/write. Below the hood, it creates two separate flash devices, because we were too lazy to improve our flash device models to support sector protection. The problem at hand is to do the same with -blockdev somehow, as one more step towards deprecating -drive. Mapping -drive if=3Dnone,... to -blockdev is a solved problem. With if=3DT other than if=3Dnone, -drive additionally configures a block device frontend. For non-onboard devices, that part maps to -device. Also a solved problem. For onboard devices such as PC flash memory, we have an unsolved problem. This is actually an instance of a wider problem: our general device configuration interface doesn't cover onboard devices. Instead, we have a zoo of ad hoc interfaces that are much more limited. Some of them we'd rather deprecate (-drive, -net), but can't until we have suitable replacements. Sadly, I can't attack the wider problem today. So back to the narrow problem. My first idea was to reduce it to its solved buddy by using pluggable instead of onboard devices for the flash memory. Workable, but it requires some extra smarts in firmware descriptors and libvirt. Paolo had an idea that is simpler for libvirt: keep the devices onboard, and add machine properties for their block backends. The implementation is less than straightforward, I'm afraid. First, block backend properties are *qdev* properties. Machines can't have those, as they're not devices. I could duplicate these qdev properties as QOM properties, but I hate that. More seriously, the properties do not belong to the machine, they belong to the onboard flash devices. Adding them to the machine would then require bad magic to somehow transfer them to the flash devices. Fortunately, QOM provides the means to handle exactly this case: add alias properties to the machine that forward to the onboard devices' properties. Properties need to be created in .instance_init() methods. For PC machines, that's pc_machine_initfn(). To make alias properties work, we need to create the onboard flash devices there, too. Requires several bug fixes, in the previous commits. We also have to realize the devices. More on that below. If the user sets pflash0, firmware resides in flash memory. pc_system_firmware_init() maps and realizes the flash devices. Else, firmware resides in ROM. The onboard flash devices aren't used then. pc_system_firmware_init() destroys them unrealized, along with the alias properties. Except I can't figure out how to destroy the devices correctly. Marked FIXME. The existing code to pick up drives defined with -drive if=3Dpflash is replaced by code to desugar into the machine properties. Signed-off-by: Markus Armbruster Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/block/pflash_cfi01.c | 5 + hw/i386/pc.c | 4 +- hw/i386/pc_sysfw.c | 230 ++++++++++++++++++++++++++------------- include/hw/block/flash.h | 1 + include/hw/i386/pc.h | 6 +- 5 files changed, 168 insertions(+), 78 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 6ad27f4472..7c428bbf50 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -959,6 +959,11 @@ MemoryRegion *pflash_cfi01_get_memory(PFlashCFI01 *fl) return &fl->mem; } =20 +BlockBackend *pflash_cfi01_get_blk(PFlashCFI01 *fl) +{ + return fl->blk; +} + static void postload_update_cb(void *opaque, int running, RunState state) { PFlashCFI01 *pfl =3D opaque; diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 3889eccdc3..420a0c5c9e 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1830,7 +1830,7 @@ void pc_memory_init(PCMachineState *pcms, } =20 /* Initialize PC system firmware */ - pc_system_firmware_init(rom_memory, !pcmc->pci_enabled); + pc_system_firmware_init(pcms, rom_memory); =20 option_rom_mr =3D g_malloc(sizeof(*option_rom_mr)); memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE, @@ -2671,6 +2671,8 @@ static void pc_machine_initfn(Object *obj) pcms->smbus_enabled =3D true; pcms->sata_enabled =3D true; pcms->pit_enabled =3D true; + + pc_system_flash_create(pcms); } =20 static void pc_machine_reset(void) diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c index 34727c5b1f..98998e1ba8 100644 --- a/hw/i386/pc_sysfw.c +++ b/hw/i386/pc_sysfw.c @@ -76,7 +76,58 @@ static void pc_isa_bios_init(MemoryRegion *rom_memory, memory_region_set_readonly(isa_bios, true); } =20 -#define FLASH_MAP_UNIT_MAX 2 +static PFlashCFI01 *pc_pflash_create(const char *name) +{ + DeviceState *dev =3D qdev_create(NULL, TYPE_CFI_PFLASH01); + + qdev_prop_set_uint64(dev, "sector-length", 4096); + qdev_prop_set_uint8(dev, "width", 1); + qdev_prop_set_string(dev, "name", name); + return CFI_PFLASH01(dev); +} + +void pc_system_flash_create(PCMachineState *pcms) +{ + PCMachineClass *pcmc =3D PC_MACHINE_GET_CLASS(pcms); + + if (pcmc->pci_enabled) { + pcms->flash[0] =3D pc_pflash_create("system.flash0"); + pcms->flash[1] =3D pc_pflash_create("system.flash1"); + object_property_add_alias(OBJECT(pcms), "pflash0", + OBJECT(pcms->flash[0]), "drive", + &error_abort); + object_property_add_alias(OBJECT(pcms), "pflash1", + OBJECT(pcms->flash[1]), "drive", + &error_abort); + } +} + +static void pc_system_flash_cleanup_unused(PCMachineState *pcms) +{ + char *prop_name; + int i; + Object *dev_obj; + + assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); + + for (i =3D 0; i < ARRAY_SIZE(pcms->flash); i++) { + dev_obj =3D OBJECT(pcms->flash[i]); + if (!object_property_get_bool(dev_obj, "realized", &error_abort)) { + prop_name =3D g_strdup_printf("pflash%d", i); + object_property_del(OBJECT(pcms), prop_name, &error_abort); + g_free(prop_name); + /* + * FIXME delete @dev_obj. Straight object_unref() is + * wrong, since it leaves dangling references in the + * parent bus behind. object_unparent() doesn't work, + * either: since @dev_obj hasn't been realized, + * dev_obj->parent is null, and object_unparent() does + * nothing. + */ + pcms->flash[i] =3D NULL; + } + } +} =20 /* We don't have a theoretically justifiable exact lower bound on the base * address of any flash mapping. In practice, the IO-APIC MMIO range is @@ -84,88 +135,78 @@ static void pc_isa_bios_init(MemoryRegion *rom_memory, * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB= in * size. */ -#define FLASH_MAP_BASE_MIN ((hwaddr)(4 * GiB - 8 * MiB)) +#define FLASH_SIZE_LIMIT (8 * MiB) =20 -/* This function maps flash drives from 4G downward, in order of their unit - * numbers. The mapping starts at unit#0, with unit number increments of 1= , and - * stops before the first missing flash drive, or before - * unit#FLASH_MAP_UNIT_MAX, whichever is reached first. +/* + * Map the pcms->flash[] from 4GiB downward, and realize. + * Map them in descending order, i.e. pcms->flash[0] at the top, + * without gaps. + * Stop at the first pcms->flash[0] lacking a block backend. + * Set each flash's size from its block backend. Fatal error if the + * size isn't a non-zero multiples of 4KiB, or the total size exceeds + * FLASH_SIZE_LIMIT. * - * Addressing within one flash drive is of course not reversed. - * - * An error message is printed and the process exits if: - * - the size of the backing file for a flash drive is non-positive, or no= t a - * multiple of the required sector size, or - * - the current mapping's base address would fall below FLASH_MAP_BASE_MI= N. - * - * The drive with unit#0 (if available) is mapped at the highest address, = and - * it is passed to pc_isa_bios_init(). Merging several drives for isa-bios= is + * If pcms->flash[0] has a block backend, its memory is passed to + * pc_isa_bios_init(). Merging several flash devices for isa-bios is * not supported. */ -static void pc_system_flash_init(MemoryRegion *rom_memory) +static void pc_system_flash_map(PCMachineState *pcms, + MemoryRegion *rom_memory) { - int unit; - DriveInfo *pflash_drv; + hwaddr total_size =3D 0; + int i; BlockBackend *blk; int64_t size; - char *fatal_errmsg =3D NULL; - hwaddr phys_addr =3D 0x100000000ULL; uint32_t sector_size =3D 4096; PFlashCFI01 *system_flash; MemoryRegion *flash_mem; - char name[64]; void *flash_ptr; int ret, flash_size; =20 - for (unit =3D 0; - (unit < FLASH_MAP_UNIT_MAX && - (pflash_drv =3D drive_get(IF_PFLASH, 0, unit)) !=3D NULL); - ++unit) { - blk =3D blk_by_legacy_dinfo(pflash_drv); + assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); + + for (i =3D 0; i < ARRAY_SIZE(pcms->flash); i++) { + system_flash =3D pcms->flash[i]; + blk =3D pflash_cfi01_get_blk(system_flash); + if (!blk) { + break; + } size =3D blk_getlength(blk); if (size < 0) { - fatal_errmsg =3D g_strdup_printf("failed to get backing file s= ize"); - } else if (size =3D=3D 0) { - fatal_errmsg =3D g_strdup_printf("PC system firmware (pflash) " - "cannot have zero size"); - } else if ((size % sector_size) !=3D 0) { - fatal_errmsg =3D g_strdup_printf("PC system firmware (pflash) " - "must be a multiple of 0x%x", sector_size); - } else if (phys_addr < size || phys_addr - size < FLASH_MAP_BASE_M= IN) { - fatal_errmsg =3D g_strdup_printf("oversized backing file, pfla= sh " - "segments cannot be mapped under " - TARGET_FMT_plx, FLASH_MAP_BASE_MIN); + error_report("can't get size of block device %s: %s", + blk_name(blk), strerror(-size)); + exit(1); } - if (fatal_errmsg !=3D NULL) { - Location loc; - - /* push a new, "none" location on the location stack; overwrit= e its - * contents with the location saved in the option; print the e= rror - * (includes location); pop the top - */ - loc_push_none(&loc); - if (pflash_drv->opts !=3D NULL) { - qemu_opts_loc_restore(pflash_drv->opts); - } - error_report("%s", fatal_errmsg); - loc_pop(&loc); - g_free(fatal_errmsg); + if (size =3D=3D 0) { + error_report("system firmware block device %s is empty", + blk_name(blk)); + exit(1); + } + if (size =3D=3D 0 || size % sector_size !=3D 0) { + error_report("system firmware block device %s has invalid size= " + "%" PRId64, + blk_name(blk), size); + info_report("its size must be a non-zero multiple of 0x%x", + sector_size); + exit(1); + } + if ((hwaddr)size !=3D size + || total_size > HWADDR_MAX - size + || total_size + size > FLASH_SIZE_LIMIT) { + error_report("combined size of system firmware exceeds " + "%" PRIu64 " bytes", + FLASH_SIZE_LIMIT); exit(1); } =20 - phys_addr -=3D size; + total_size +=3D size; + qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks", + size / sector_size); + qdev_init_nofail(DEVICE(system_flash)); + sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, + 0x100000000ULL - total_size); =20 - /* pflash_cfi01_register() creates a deep copy of the name */ - snprintf(name, sizeof name, "system.flash%d", unit); - system_flash =3D pflash_cfi01_register(phys_addr, name, - size, blk, sector_size, - 1 /* width */, - 0x0000 /* id0 */, - 0x0000 /* id1 */, - 0x0000 /* id2 */, - 0x0000 /* id3 */, - 0 /* be */); - if (unit =3D=3D 0) { + if (i =3D=3D 0) { flash_mem =3D pflash_cfi01_get_memory(system_flash); pc_isa_bios_init(rom_memory, flash_mem, size); =20 @@ -236,24 +277,63 @@ static void old_pc_system_rom_init(MemoryRegion *rom_= memory, bool isapc_ram_fw) bios); } =20 -void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw) +void pc_system_firmware_init(PCMachineState *pcms, + MemoryRegion *rom_memory) { + PCMachineClass *pcmc =3D PC_MACHINE_GET_CLASS(pcms); + int i; DriveInfo *pflash_drv; + BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)]; + Location loc; =20 - pflash_drv =3D drive_get(IF_PFLASH, 0, 0); - - if (isapc_ram_fw || pflash_drv =3D=3D NULL) { - /* When a pflash drive is not found, use rom-mode */ - old_pc_system_rom_init(rom_memory, isapc_ram_fw); + if (!pcmc->pci_enabled) { + old_pc_system_rom_init(rom_memory, true); return; } =20 - if (kvm_enabled() && !kvm_readonly_mem_enabled()) { - /* Older KVM cannot execute from device memory. So, flash memory - * cannot be used unless the readonly memory kvm capability is pre= sent. */ - fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memor= y support\n"); - exit(1); + /* Map legacy -drive if=3Dpflash to machine properties */ + for (i =3D 0; i < ARRAY_SIZE(pcms->flash); i++) { + pflash_blk[i] =3D pflash_cfi01_get_blk(pcms->flash[i]); + pflash_drv =3D drive_get(IF_PFLASH, 0, i); + if (!pflash_drv) { + continue; + } + loc_push_none(&loc); + qemu_opts_loc_restore(pflash_drv->opts); + if (pflash_blk[i]) { + error_report("clashes with -machine"); + exit(1); + } + pflash_blk[i] =3D blk_by_legacy_dinfo(pflash_drv); + qdev_prop_set_drive(DEVICE(pcms->flash[i]), + "drive", pflash_blk[i], &error_fatal); + loc_pop(&loc); } =20 - pc_system_flash_init(rom_memory); + /* Reject gaps */ + for (i =3D 1; i < ARRAY_SIZE(pcms->flash); i++) { + if (pflash_blk[i] && !pflash_blk[i - 1]) { + error_report("pflash%d requires pflash%d", i, i - 1); + exit(1); + } + } + + if (!pflash_blk[0]) { + /* Machine property pflash0 not set, use ROM mode */ + old_pc_system_rom_init(rom_memory, false); + } else { + if (kvm_enabled() && !kvm_readonly_mem_enabled()) { + /* + * Older KVM cannot execute from device memory. So, flash + * memory cannot be used unless the readonly memory kvm + * capability is present. + */ + error_report("pflash with kvm requires KVM readonly memory sup= port"); + exit(1); + } + + pc_system_flash_map(pcms, rom_memory); + } + + pc_system_flash_cleanup_unused(pcms); } diff --git a/include/hw/block/flash.h b/include/hw/block/flash.h index 24b13eb525..91e0f8dd6e 100644 --- a/include/hw/block/flash.h +++ b/include/hw/block/flash.h @@ -22,6 +22,7 @@ PFlashCFI01 *pflash_cfi01_register(hwaddr base, uint16_t id0, uint16_t id1, uint16_t id2, uint16_t id3, int be); +BlockBackend *pflash_cfi01_get_blk(PFlashCFI01 *fl); MemoryRegion *pflash_cfi01_get_memory(PFlashCFI01 *fl); =20 /* pflash_cfi02.c */ diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h index 3ff127ebd0..266639ca8c 100644 --- a/include/hw/i386/pc.h +++ b/include/hw/i386/pc.h @@ -6,6 +6,7 @@ #include "hw/boards.h" #include "hw/isa/isa.h" #include "hw/block/fdc.h" +#include "hw/block/flash.h" #include "net/net.h" #include "hw/i386/ioapic.h" =20 @@ -39,6 +40,7 @@ struct PCMachineState { PCIBus *bus; FWCfgState *fw_cfg; qemu_irq *gsi; + PFlashCFI01 *flash[2]; =20 /* Configuration options: */ uint64_t max_ram_below_4g; @@ -278,8 +280,8 @@ extern PCIDevice *piix4_dev; int piix4_init(PCIBus *bus, ISABus **isa_bus, int devfn); =20 /* pc_sysfw.c */ -void pc_system_firmware_init(MemoryRegion *rom_memory, - bool isapc_ram_fw); +void pc_system_flash_create(PCMachineState *pcms); +void pc_system_firmware_init(PCMachineState *pcms, MemoryRegion *rom_memor= y); =20 /* acpi-build.c */ void pc_madt_cpu_entry(AcpiDeviceIf *adev, int uid, --=20 2.17.2