diff mbox series

[RFC,20/25] tools/xen-sys: Create a crate with autogenerated Rust constructs

Message ID 20241115115200.2824-21-alejandro.vallejo@cloud.com (mailing list archive)
State New
Headers show
Series Introduce xenbindgen to autogen hypercall structs | expand

Commit Message

Alejandro Vallejo Nov. 15, 2024, 11:51 a.m. UTC
Signed-off-by: Alejandro Vallejo <alejandro.vallejo@cloud.com>
---
 tools/rust/Makefile           |  5 ++++-
 tools/rust/xen-sys/.gitignore |  2 ++
 tools/rust/xen-sys/Cargo.toml |  8 +++++++
 tools/rust/xen-sys/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++
 4 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 tools/rust/xen-sys/.gitignore
 create mode 100644 tools/rust/xen-sys/Cargo.toml
 create mode 100644 tools/rust/xen-sys/src/lib.rs
diff mbox series

Patch

diff --git a/tools/rust/Makefile b/tools/rust/Makefile
index 80e2f0e2817e..814e5b94447f 100644
--- a/tools/rust/Makefile
+++ b/tools/rust/Makefile
@@ -3,6 +3,9 @@  XEN_ROOT=$(CURDIR)/../..
 # Path to the Xen hypercall IDL parser
 XENBINDGEN=$(CURDIR)/xenbindgen
 
+# Path to the autogenerated Rust bindings crate
+CRATE_XENSYS=$(CURDIR)/xen-sys
+
 # Output folder for the autogenerated C headers
 #
 # Must contain autogenerated files only because they're all wiped on update
@@ -37,7 +40,7 @@  update: clean-autogen
 .PHONY: verify
 verify:
 	set -eu; \
-	for i in "${XENBINDGEN}"; do \
+	for i in "${CRATE_XENSYS}" "${XENBINDGEN}"; do \
 	    echo "Verifying $$i"; \
 	    cd "$$i"; \
 	    cargo fmt -- --check; \
diff --git a/tools/rust/xen-sys/.gitignore b/tools/rust/xen-sys/.gitignore
new file mode 100644
index 000000000000..ca98cd96efdc
--- /dev/null
+++ b/tools/rust/xen-sys/.gitignore
@@ -0,0 +1,2 @@ 
+/target/
+Cargo.lock
diff --git a/tools/rust/xen-sys/Cargo.toml b/tools/rust/xen-sys/Cargo.toml
new file mode 100644
index 000000000000..fb91beaa1525
--- /dev/null
+++ b/tools/rust/xen-sys/Cargo.toml
@@ -0,0 +1,8 @@ 
+[package]
+name = "xen-sys"
+version = "0.1.0"
+edition = "2021"
+license = "MIT"
+
+[dependencies]
+bitflags = { version = "2.6.0", default-features = false }
diff --git a/tools/rust/xen-sys/src/lib.rs b/tools/rust/xen-sys/src/lib.rs
new file mode 100644
index 000000000000..7b3ea6c8ad4c
--- /dev/null
+++ b/tools/rust/xen-sys/src/lib.rs
@@ -0,0 +1,41 @@ 
+//! xen-sys
+//!
+//! This crate contains low-level primitives to interact with the Xen
+//! hypervisor. It relies on the autogenerated bindings of xenbindgen and
+//! a few closely related primitives, like [`Align64`].
+#![no_std]
+
+use core::ops::{Deref, DerefMut};
+
+/// Wrapper for pointers and 64bit integers so they are _always_ aligned to 8
+/// octets, even in 32bit machines.
+#[repr(align(8))]
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+pub struct Align64<T>(pub T);
+
+impl<T> Default for Align64<T> {
+    fn default() -> Self {
+        // Experimental feature
+        //
+        // This is required because `*mut U` can't implement Default. We take
+        // the convention that `Default` means "zero". For `t: Align64<*mut T>`
+        // that means `t` is null.
+        //
+        // If the `xen` crate stops needing this, then this impl must go away.
+        unsafe { Self(core::mem::zeroed()) }
+    }
+}
+
+impl<T> Deref for Align64<T> {
+    type Target = T;
+
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
+impl<T> DerefMut for Align64<T> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.0
+    }
+}