new file mode 100644
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Helpers to run Rust futures using QEMU coroutines
+ *
+ * Copyright Red Hat
+ *
+ * Author:
+ * Kevin Wolf <kwolf@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#ifndef QEMU_COROUTINE_RUST_H
+#define QEMU_COROUTINE_RUST_H
+
+typedef struct RustBoxedFuture RustBoxedFuture;
+typedef void coroutine_fn RunFuture(RustBoxedFuture *future, void *opaque);
+
+void no_coroutine_fn rust_run_future(RustBoxedFuture *future,
+ RunFuture *entry,
+ void *opaque);
+
+#endif
@@ -58,3 +58,4 @@ typedef enum memory_order {
#include "block/block_int.h"
#include "block/qdict.h"
#include "qapi/qapi-visit-block-core.h"
+#include "qemu/coroutine-rust.h"
new file mode 100644
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Helpers to run Rust futures using QEMU coroutines
+ *
+ * Copyright Red Hat
+ *
+ * Author:
+ * Kevin Wolf <kwolf@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "block/aio-wait.h"
+#include "qemu/coroutine.h"
+#include "qemu/coroutine-rust.h"
+#include "qemu/main-loop.h"
+
+typedef struct FutureCo {
+ RustBoxedFuture *future;
+ RunFuture *entry;
+ void *opaque;
+ bool done;
+} FutureCo;
+
+static void coroutine_fn rust_co_run_future_entry(void *opaque)
+{
+ FutureCo *data = opaque;
+
+ data->entry(data->future, data->opaque);
+ data->done = true;
+ aio_wait_kick();
+}
+
+void no_coroutine_fn rust_run_future(RustBoxedFuture *future,
+ RunFuture *entry,
+ void *opaque)
+{
+ AioContext *ctx = qemu_get_current_aio_context();
+ Coroutine *co;
+ FutureCo data = {
+ .future = future,
+ .entry = entry,
+ .opaque = opaque,
+ .done = false,
+ };
+
+ GLOBAL_STATE_CODE();
+
+ co = qemu_coroutine_create(rust_co_run_future_entry, &data);
+ aio_co_enter(ctx, co);
+ AIO_WAIT_WHILE(ctx, !data.done);
+}
@@ -22,6 +22,7 @@ sources_core = [
'src/chardev.rs',
'src/c_str.rs',
'src/errno.rs',
+ 'src/futures.rs',
'src/module.rs',
'src/offset_of.rs',
'src/prelude.rs',
new file mode 100644
@@ -0,0 +1,77 @@
+use crate::bindings;
+use std::ffi::c_void;
+use std::future::Future;
+use std::mem::MaybeUninit;
+use std::sync::Arc;
+use std::task::{Context, Poll, Wake, Waker};
+
+struct RunFutureWaker {
+ co: *mut bindings::Coroutine,
+}
+unsafe impl Send for RunFutureWaker {}
+unsafe impl Sync for RunFutureWaker {}
+
+impl Wake for RunFutureWaker {
+ fn wake(self: Arc<Self>) {
+ unsafe {
+ bindings::aio_co_wake(self.co);
+ }
+ }
+}
+
+/// Use QEMU's event loops to run a Rust [`Future`] to completion and return its result.
+///
+/// This function must be called in coroutine context. If the future isn't ready yet, it yields.
+pub fn qemu_co_run_future<F: Future>(future: F) -> F::Output {
+ let waker = Waker::from(Arc::new(RunFutureWaker {
+ co: unsafe { bindings::qemu_coroutine_self() },
+ }));
+ let mut cx = Context::from_waker(&waker);
+
+ let mut pinned_future = std::pin::pin!(future);
+ loop {
+ match pinned_future.as_mut().poll(&mut cx) {
+ Poll::Ready(res) => return res,
+ Poll::Pending => unsafe {
+ bindings::qemu_coroutine_yield();
+ },
+ }
+ }
+}
+
+/// Wrapper around [`qemu_co_run_future`] that can be called from C.
+///
+/// # Safety
+///
+/// `future` must be a valid pointer to an owned `F` (it will be freed in this function). `output`
+/// must be a valid pointer representing a mutable reference to an `F::Output` where the result can
+/// be stored.
+unsafe extern "C" fn rust_co_run_future<F: Future>(
+ future: *mut bindings::RustBoxedFuture,
+ output: *mut c_void,
+) {
+ let future = unsafe { Box::from_raw(future.cast::<F>()) };
+ let output = output.cast::<F::Output>();
+ let ret = qemu_co_run_future(*future);
+ unsafe {
+ output.write(ret);
+ }
+}
+
+/// Use QEMU's event loops to run a Rust [`Future`] to completion and return its result.
+///
+/// This function must be called outside of coroutine context to avoid deadlocks. It blocks and
+/// runs a nested even loop until the future is ready and returns a result.
+pub fn qemu_run_future<F: Future>(future: F) -> F::Output {
+ let future_ptr = Box::into_raw(Box::new(future));
+ let mut output = MaybeUninit::<F::Output>::uninit();
+ unsafe {
+ bindings::rust_run_future(
+ future_ptr.cast::<bindings::RustBoxedFuture>(),
+ #[allow(clippy::as_underscore)]
+ Some(rust_co_run_future::<F> as _),
+ output.as_mut_ptr().cast::<c_void>(),
+ );
+ output.assume_init()
+ }
+}
@@ -20,6 +20,7 @@
pub mod cell;
pub mod chardev;
pub mod errno;
+pub mod futures;
#[cfg(feature = "system")]
pub mod irq;
#[cfg(feature = "system")]
@@ -101,6 +101,9 @@ if have_block
util_ss.add(files('qemu-coroutine-sleep.c'))
util_ss.add(files('qemu-co-shared-resource.c'))
util_ss.add(files('qemu-co-timeout.c'))
+ if have_rust
+ util_ss.add(files('qemu-co-rust-async.c'))
+ endif
util_ss.add(files('readline.c'))
util_ss.add(files('throttle.c'))
util_ss.add(files('timed-average.c'))
This adds helper functions that allow running Rust futures to completion using QEMU's event loops. Signed-off-by: Kevin Wolf <kwolf@redhat.com> --- include/qemu/coroutine-rust.h | 24 +++++++++++ rust/wrapper.h | 1 + util/qemu-co-rust-async.c | 55 +++++++++++++++++++++++++ rust/qemu-api/meson.build | 1 + rust/qemu-api/src/futures.rs | 77 +++++++++++++++++++++++++++++++++++ rust/qemu-api/src/lib.rs | 1 + util/meson.build | 3 ++ 7 files changed, 162 insertions(+) create mode 100644 include/qemu/coroutine-rust.h create mode 100644 util/qemu-co-rust-async.c create mode 100644 rust/qemu-api/src/futures.rs