From nobody Sat Apr 5 01:14:27 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1739155400; cv=none; d=zohomail.com; s=zohoarc; b=mm4X3lN7hWY1BDfGfDOrgEyYT4rkTH3NHIfFLz/zlRP5c7Hnh81eE4UVxkFsk+wNwsQCfjpz6k4IibuG4rir+CQkjJXecanNTihPSs2uSKC4xNgd1JvRZXy+JAdBsD9zLyWb4ULkOyzuP2sF4vvjcivQO2kAeUwkKDFiNPDbhwg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1739155400; 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=bpZEqGit/18TL/wh1ddtmb5lTXDT+Edz5eEp8EK0SSM=; b=jqZquoJx0BY4c8fkySOgiNsXsRiXOHa2QCbbhz8Gf1HGRjQIjYtNxyceIDCH3IOkgfwli6674ZWJrNF0CinoOtkIokEVfy8dJosWvuPOpgFYshULONlIo9DrGBjWQVkCt4WdSX6zV67969Tuh2LUgI1LabmcwqYJRMq8pviyKGw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; 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 1739155400960755.7703581561397; Sun, 9 Feb 2025 18:43:20 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thJk1-0000An-3G; Sun, 09 Feb 2025 21:41:45 -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 1thJjz-0000AI-PQ; Sun, 09 Feb 2025 21:41:43 -0500 Received: from mgamail.intel.com ([192.198.163.11]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thJjx-00088s-Ns; Sun, 09 Feb 2025 21:41:43 -0500 Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Feb 2025 18:41:41 -0800 Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.39]) by fmviesa001.fm.intel.com with ESMTP; 09 Feb 2025 18:41:38 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1739155302; x=1770691302; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=atlW9CPzWUlAleUv5uCC+01KqSDTHP8baoSghJqOJ4E=; b=Pgb6/c/HtJb/mxRQsGcpI3Aiz0QV/x5SvZyz28nLKIB2NCTSXQbvf+oO EP7es/YFaTYM2GuriBuYHZZfN7iAwdhflXK5pUl2d/52kvEuo3qJuW+BK tcPH3G1JZCg07R9snzjf5M3vjyO8AtNRyrwGI3mR+/zFAlsk/XO0dGkUk D8VObNSQyQl40G36arbJNOYOkgWLz/5ZAArtPi1o2+QYUJAbMrtT9h/25 FMrUp7vCPK27+P9TapfOhc2geIWRU9i1HHUifcFlPgp06xRlsdsvF9v07 G1tuARAN/I4nB9kvDqcqkipcaGKxtkxDOFFyd/1ne4eqYNDJS7WY7sUVT A==; X-CSE-ConnectionGUID: Ms/PnYTRSRqvKK9/x42Yjw== X-CSE-MsgGUID: s8I7S8DpR5aSW4y+xx3d2A== X-IronPort-AV: E=McAfee;i="6700,10204,11340"; a="50351269" X-IronPort-AV: E=Sophos;i="6.13,273,1732608000"; d="scan'208";a="50351269" X-CSE-ConnectionGUID: w0DHvat3TuqpLC1jwuEEcA== X-CSE-MsgGUID: sMBZTORGTli6URDuipyBsA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,224,1728975600"; d="scan'208";a="142938227" From: Zhao Liu To: Paolo Bonzini , Manos Pitsidianakis , Junjie Mao , =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Richard Henderson , Peter Maydell , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org, Zhao Liu Subject: [PATCH v2 06/10] rust: add bindings for timer Date: Mon, 10 Feb 2025 11:00:47 +0800 Message-Id: <20250210030051.2562726-7-zhao1.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250210030051.2562726-1-zhao1.liu@intel.com> References: <20250210030051.2562726-1-zhao1.liu@intel.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=192.198.163.11; envelope-from=zhao1.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -57 X-Spam_score: -5.8 X-Spam_bar: ----- X-Spam_report: (-5.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.405, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1739155403385019100 Content-Type: text/plain; charset="utf-8" Add timer bindings to help handle idiomatic Rust callbacks. Additionally, wrap QEMUClockType in ClockType binding to avoid unsafe calls in device code. Signed-off-by: Zhao Liu --- Changes since RFC: * Use FnCall to support timer callback. * Only add timer_init_full() binding. timer_new() is unnecessary since device (HPET) could create and allocate QEMUTimer. * Implement Drop for QEMUTimer. * Add ClockType binding. Changes since Patch v1: * Use Timer/TimerListGroup in Rust code instead of QEMUTimer/QEMUTimerListGroup. * Rename timer_init_full()/timer_mod() to init_full()/modify(). * Ensure the callback passed to Timer::init() is not none. * Add as_mut_ptr()/delete() method for Timer. * Make ClockType's `id` field private, as well as make Timer::init() accept ClockType instead of QEMUClockType. * Drop used qemu_clock_get_virtual_ns(). * Add MS/US/NS wrappers. --- meson.build | 7 +++ rust/qemu-api/meson.build | 1 + rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/timer.rs | 98 ++++++++++++++++++++++++++++++++++++++ rust/wrapper.h | 1 + 5 files changed, 108 insertions(+) create mode 100644 rust/qemu-api/src/timer.rs diff --git a/meson.build b/meson.build index 32abefb7c48c..20192347d4d6 100644 --- a/meson.build +++ b/meson.build @@ -4086,6 +4086,13 @@ if have_rust foreach enum : c_bitfields bindgen_args +=3D ['--bitfield-enum', enum] endforeach + c_nocopy =3D [ + 'QEMUTimer', + ] + # Used to customize Drop trait + foreach struct : c_nocopy + bindgen_args +=3D ['--no-copy', struct] + endforeach =20 # TODO: Remove this comment when the clang/libclang mismatch issue is so= lved. # diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build index 45e30324b295..2e9c1078b9b2 100644 --- a/rust/qemu-api/meson.build +++ b/rust/qemu-api/meson.build @@ -30,6 +30,7 @@ _qemu_api_rs =3D static_library( 'src/qdev.rs', 'src/qom.rs', 'src/sysbus.rs', + 'src/timer.rs', 'src/vmstate.rs', 'src/zeroable.rs', ], diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs index 1d7112445e24..047511d36487 100644 --- a/rust/qemu-api/src/lib.rs +++ b/rust/qemu-api/src/lib.rs @@ -25,6 +25,7 @@ pub mod qdev; pub mod qom; pub mod sysbus; +pub mod timer; pub mod vmstate; pub mod zeroable; =20 diff --git a/rust/qemu-api/src/timer.rs b/rust/qemu-api/src/timer.rs new file mode 100644 index 000000000000..05a5061b6dda --- /dev/null +++ b/rust/qemu-api/src/timer.rs @@ -0,0 +1,98 @@ +// Copyright (C) 2024 Intel Corporation. +// Author(s): Zhao Liu +// SPDX-License-Identifier: GPL-2.0-or-later + +use std::os::raw::{c_int, c_void}; + +use crate::{ + bindings::{self, qemu_clock_get_ns, timer_del, timer_init_full, timer_= mod, QEMUClockType}, + callbacks::FnCall, +}; + +pub type Timer =3D bindings::QEMUTimer; +pub type TimerListGroup =3D bindings::QEMUTimerListGroup; + +impl Timer { + pub fn new() -> Self { + Default::default() + } + + const fn as_mut_ptr(&self) -> *mut Self { + self as *const Timer as *mut _ + } + + pub fn init_full<'timer, 'opaque: 'timer, T, F>( + &'timer mut self, + timer_list_group: Option<&TimerListGroup>, + clk_type: ClockType, + scale: u32, + attributes: u32, + _cb: F, + opaque: &'opaque T, + ) where + F: for<'a> FnCall<(&'a T,)>, + { + let _: () =3D F::ASSERT_IS_SOME; + + /// timer expiration callback + unsafe extern "C" fn rust_timer_handler FnCall<(&'a = T,)>>( + opaque: *mut c_void, + ) { + // SAFETY: the opaque was passed as a reference to `T`. + F::call((unsafe { &*(opaque.cast::()) },)) + } + + let timer_cb: unsafe extern "C" fn(*mut c_void) =3D rust_timer_han= dler::; + + // SAFETY: the opaque outlives the timer + unsafe { + timer_init_full( + self, + if let Some(g) =3D timer_list_group { + g as *const TimerListGroup as *mut _ + } else { + ::core::ptr::null_mut() + }, + clk_type.id, + scale as c_int, + attributes as c_int, + Some(timer_cb), + (opaque as *const T).cast::() as *mut c_void, + ) + } + } + + pub fn modify(&self, expire_time: u64) { + unsafe { timer_mod(self.as_mut_ptr(), expire_time as i64) } + } + + pub fn delete(&self) { + unsafe { timer_del(self.as_mut_ptr()) } + } +} + +impl Drop for Timer { + fn drop(&mut self) { + self.delete() + } +} + +pub struct ClockType { + id: QEMUClockType, +} + +impl ClockType { + pub fn get_ns(&self) -> u64 { + // SAFETY: cannot be created outside this module, therefore id + // is valid + (unsafe { qemu_clock_get_ns(self.id) }) as u64 + } +} + +pub const CLOCK_VIRTUAL: ClockType =3D ClockType { + id: QEMUClockType::QEMU_CLOCK_VIRTUAL, +}; + +pub const MS: u32 =3D bindings::SCALE_MS; +pub const US: u32 =3D bindings::SCALE_US; +pub const NS: u32 =3D bindings::SCALE_NS; diff --git a/rust/wrapper.h b/rust/wrapper.h index 54839ce0f510..a35bfbd1760d 100644 --- a/rust/wrapper.h +++ b/rust/wrapper.h @@ -63,3 +63,4 @@ typedef enum memory_order { #include "migration/vmstate.h" #include "chardev/char-serial.h" #include "exec/memattrs.h" +#include "qemu/timer.h" --=20 2.34.1