From nobody Sat Apr 5 05:28:50 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1738923511; cv=none; d=zohomail.com; s=zohoarc; b=T2LKYaaZzDGEL4cE/GPFaliCOXd95/3rs8rqSxtiiVULj6MnoAOpQe6HnztG9s8bhtQsl0pxzbsy/X/uI8IFpGCZLUXy2b9KiILYonOMX/AUnBdI5JbjAL0y6vEsi8d0n+Uu5JjkQgO0zDrfvcpSNnlbAfDc++RAA2GMKz/n+zU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1738923511; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=e8ugkz2MCTVi9k0wZ/H/gZ6hjpuLYxOLaMc5B1rAzi0=; b=fCYtDT6U3bXPTiyoUREqSvUINq7kFSxsHGkR6PooRLEH7Cts06eDmSFb3lGCqTY7+ElsCM4C3q1gQ292ZPqE+7KVox0VuIMxOH0InFnv+gdnQcZVIgvIYbMUmQdnT3oLeSocg00sgCRlwco+qH2/pzPMTNxpJl/UHj6exvarWUE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1738923511071707.8161624712503; Fri, 7 Feb 2025 02:18:31 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tgLQ4-0007Ur-2q; Fri, 07 Feb 2025 05:17:09 -0500 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 1tgLPv-0007Pf-HF for qemu-devel@nongnu.org; Fri, 07 Feb 2025 05:16:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tgLPu-0004Hc-2F for qemu-devel@nongnu.org; Fri, 07 Feb 2025 05:16:59 -0500 Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-642-GS6K0KNGMhC6aO2VD5gKSw-1; Fri, 07 Feb 2025 05:16:56 -0500 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-ab2e44dc9b8so312064666b.1 for ; Fri, 07 Feb 2025 02:16:56 -0800 (PST) Received: from [192.168.10.3] ([151.62.97.55]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab7732e7232sm241982766b.97.2025.02.07.02.16.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 02:16:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738923417; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e8ugkz2MCTVi9k0wZ/H/gZ6hjpuLYxOLaMc5B1rAzi0=; b=cmqBMnWSdKLdmKXESraUkoraBDV7jfv/Ytal+oJlgfV/kg6Hv3lxQ1b566xC8p3zMaNFT1 wKo8+H48h1yKI6A3hmYBVVaxSErreHWM6Shur5VNNj99iDgDntg+YdZjhSk4ZGEyi5vWxx Krxm9hZ2H1HnD1pq2Ut/Px6pPWcMk0g= X-MC-Unique: GS6K0KNGMhC6aO2VD5gKSw-1 X-Mimecast-MFC-AGG-ID: GS6K0KNGMhC6aO2VD5gKSw X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738923413; x=1739528213; 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=e8ugkz2MCTVi9k0wZ/H/gZ6hjpuLYxOLaMc5B1rAzi0=; b=Xd3Y1l4/offFQ7GIkhQ8aj7yoPLWaVlyHVPOHplYAYf+6y7vMvFUH4nzOTLpPV8d1X lDkpI5P1JZJUGtSjSZ0gp5plBkYv4QJczfSy96LxtvS7v6oHuDiMwMXVce4XL3usaNaG i5r4eVDsCttLsbuuct+vE0Eb1x16iBkzdxtqW+8se/XZZ/rkmUsNXrGubpKJ2IY1GaSe DkCvTulU2NoWUvmFSHdW3T7LvtoAo7OwBhA1wC8KSmMg83JOCEUx7O8EjG6LVWK+hXwc 6ImLMCcx/wPBUn+cDmBcY5JHdkPyuEpZWmjCi3X7HWXz8WJZa8mrfGFaPzSt7DVpMC1w afFg== X-Gm-Message-State: AOJu0Yyc6SUyTiS0CFh19sd/9PMd41H9he/hHp6/LQplP50HIViZGlgh x6ZQ6bJ7ViN598afQzHAUpulfNcMDm08eoeCI1ndrwLBzYvQ7Wx0vWf32D2gUtbyFhwdyS/C43G engWCQ5vCU6FY/SlWKlEx36BsewyRgFjH/uhVvaxQW+vtGSUxIkygKQnmDu+OrzaDl8k0t4NKez QLlp3Mk6WnJjdcgTqUlTXJjWkasGsItH+NVIseRmY= X-Gm-Gg: ASbGncu6A1S97fP1nC0ULYuUYmIP6hF8inAE54RXf0eDdUnMxfcoi3DWCf+x9PTJr0Q WBpbXAoC+Kqs/39314QFytPvWWD5hRYyOFW7phSU69cykrS614GrgNQ65AzlPAKkMSvsDqu7ZLM jJQrNOyYS8wtKAsumhzCjTjJ+36EuD+J/4Za1wJD46m1bBzfiqwV5id8UPiTpRQ280QVQhwAc7x ublC/JkQNRdl3qS7PX8GOWo4/cGYBcdWa2aQGwmwwqWhaiGcjf+vskjXF0WyoB68+aE7HEZLzn8 GmSj6w== X-Received: by 2002:a17:907:7e8c:b0:ab6:f782:3ee2 with SMTP id a640c23a62f3a-ab76e896c1cmr679681466b.9.1738923412914; Fri, 07 Feb 2025 02:16:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IFM3lk2ydc7epwpkztg3w/h+v1br1TpLcOGK5HJ3x0fJvwRo4UJf6MhFQZ1H9LxVlL2Y6xFHw== X-Received: by 2002:a17:907:7e8c:b0:ab6:f782:3ee2 with SMTP id a640c23a62f3a-ab76e896c1cmr679677266b.9.1738923412411; Fri, 07 Feb 2025 02:16:52 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com Subject: [PATCH 05/12] rust: qom: allow initializing interface vtables Date: Fri, 7 Feb 2025 11:16:16 +0100 Message-ID: <20250207101623.2443552-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250207101623.2443552-1-pbonzini@redhat.com> References: <20250207101623.2443552-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.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, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1738923511618019000 Content-Type: text/plain; charset="utf-8" Unlike regular classes, interface vtables can only be obtained via object_class_dynamic_cast. Provide a wrapper that allows accessing the vtable and pass it to a ClassInitImpl implementation, for example ClassInitImpl. Reviewed-by: Zhao Liu Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/prelude.rs | 1 + rust/qemu-api/src/qom.rs | 45 ++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/rust/qemu-api/src/prelude.rs b/rust/qemu-api/src/prelude.rs index 87e3ce90f26..254edb476dd 100644 --- a/rust/qemu-api/src/prelude.rs +++ b/rust/qemu-api/src/prelude.rs @@ -9,6 +9,7 @@ =20 pub use crate::qdev::DeviceMethods; =20 +pub use crate::qom::InterfaceType; pub use crate::qom::IsA; pub use crate::qom::Object; pub use crate::qom::ObjectCast; diff --git a/rust/qemu-api/src/qom.rs b/rust/qemu-api/src/qom.rs index fad4759d7a6..fa69ae4c702 100644 --- a/rust/qemu-api/src/qom.rs +++ b/rust/qemu-api/src/qom.rs @@ -66,8 +66,8 @@ =20 use crate::{ bindings::{ - self, object_dynamic_cast, object_get_class, object_get_typename, = object_new, object_ref, - object_unref, TypeInfo, + self, object_class_dynamic_cast, object_dynamic_cast, object_get_c= lass, + object_get_typename, object_new, object_ref, object_unref, TypeInf= o, }, cell::bql_locked, }; @@ -263,6 +263,47 @@ unsafe fn as_object_mut_ptr(&self) -> *mut Object { } } =20 +/// Trait exposed by all structs corresponding to QOM interfaces. +/// Unlike `ObjectType`, it is implemented on the class type (which provid= es +/// the vtable for the interfaces). +/// +/// # Safety +/// +/// `TYPE` must match the contents of the `TypeInfo` as found in the C cod= e; +/// right now, interfaces can only be declared in C. +pub unsafe trait InterfaceType: Sized { + /// The name of the type, which can be passed to + /// `object_class_dynamic_cast()` to obtain the pointer to the vtable + /// for this interface. + const TYPE_NAME: &'static CStr; + + /// Initialize the vtable for the interface; the generic argument `T` = is the + /// type being initialized, while the generic argument `U` is the type= that + /// lists the interface in its `TypeInfo`. + /// + /// # Panics + /// + /// Panic if the incoming argument if `T` does not implement the inter= face. + fn interface_init< + T: ObjectType + ClassInitImpl + ClassInitImpl, + U: ObjectType, + >( + klass: &mut U::Class, + ) { + unsafe { + // SAFETY: upcasting to ObjectClass is always valid, and the + // return type is either NULL or the argument itself + let result: *mut Self =3D object_class_dynamic_cast( + (klass as *mut U::Class).cast(), + Self::TYPE_NAME.as_ptr(), + ) + .cast(); + + >::class_init(result.as_mut().unwrap(= )) + } + } +} + /// This trait provides safe casting operations for QOM objects to raw poi= nters, /// to be used for example for FFI. The trait can be applied to any kind of /// reference or smart pointers, and enforces correctness through the [`Is= A`] --=20 2.48.1