aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPer Larsen <perlarsen@google.com>2024-03-09 03:16:59 +0000
committerPer Larsen <perlarsen@google.com>2024-03-14 09:17:11 +0000
commit41e3a327c5e236afef99629d22e5e228ced6c4a8 (patch)
tree003931b3ab4d77dcf7d6ed7beb9cf2fd750d9865
parent09a166153908a488c187fb5a9b8a5f819c59f157 (diff)
downloadcommon-41e3a327c5e236afef99629d22e5e228ced6c4a8.tar.gz
dev: lib: Expose items used by vsock-rust
Exposes constants from mmu.h, err.h and functions from vmm.h. Bug: 298705967 Test: build.py generic-x86_64-test Change-Id: Id464e7884392e419ca4e4484eb530e1d3eecd4c7
-rw-r--r--lib/rust_support/bindings.h6
-rw-r--r--lib/rust_support/err.rs69
-rw-r--r--lib/rust_support/lib.rs9
-rw-r--r--lib/rust_support/mmu.rs36
-rw-r--r--lib/rust_support/rules.mk11
-rw-r--r--lib/rust_support/vmm.rs68
6 files changed, 199 insertions, 0 deletions
diff --git a/lib/rust_support/bindings.h b/lib/rust_support/bindings.h
index 4fda8c0c..2192bee1 100644
--- a/lib/rust_support/bindings.h
+++ b/lib/rust_support/bindings.h
@@ -1 +1,7 @@
+#include <err.h>
+
#include <panic.h>
+
+#include <kernel/vm.h>
+
+#include <arch/mmu.h>
diff --git a/lib/rust_support/err.rs b/lib/rust_support/err.rs
new file mode 100644
index 00000000..4b83c052
--- /dev/null
+++ b/lib/rust_support/err.rs
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2024 Google Inc. All rights reserved
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+// Bindgen emits this constant as a u32 so declare it as i32 here instead.
+pub const NO_ERROR: i32 = 0;
+pub use crate::sys::ERR_GENERIC;
+pub use crate::sys::ERR_NOT_FOUND;
+pub use crate::sys::ERR_NOT_READY;
+pub use crate::sys::ERR_NO_MSG;
+pub use crate::sys::ERR_NO_MEMORY;
+pub use crate::sys::ERR_ALREADY_STARTED;
+pub use crate::sys::ERR_NOT_VALID;
+pub use crate::sys::ERR_INVALID_ARGS;
+pub use crate::sys::ERR_NOT_ENOUGH_BUFFER;
+pub use crate::sys::ERR_NOT_SUSPENDED;
+pub use crate::sys::ERR_OBJECT_DESTROYED;
+pub use crate::sys::ERR_NOT_BLOCKED;
+pub use crate::sys::ERR_TIMED_OUT;
+pub use crate::sys::ERR_ALREADY_EXISTS;
+pub use crate::sys::ERR_CHANNEL_CLOSED;
+pub use crate::sys::ERR_OFFLINE;
+pub use crate::sys::ERR_NOT_ALLOWED;
+pub use crate::sys::ERR_BAD_PATH;
+pub use crate::sys::ERR_ALREADY_MOUNTED;
+pub use crate::sys::ERR_IO;
+pub use crate::sys::ERR_NOT_DIR;
+pub use crate::sys::ERR_NOT_FILE;
+pub use crate::sys::ERR_RECURSE_TOO_DEEP;
+pub use crate::sys::ERR_NOT_SUPPORTED;
+pub use crate::sys::ERR_TOO_BIG;
+pub use crate::sys::ERR_CANCELLED;
+pub use crate::sys::ERR_NOT_IMPLEMENTED;
+pub use crate::sys::ERR_CHECKSUM_FAIL;
+pub use crate::sys::ERR_CRC_FAIL;
+pub use crate::sys::ERR_CMD_UNKNOWN;
+pub use crate::sys::ERR_BAD_STATE;
+pub use crate::sys::ERR_BAD_LEN;
+pub use crate::sys::ERR_BUSY;
+pub use crate::sys::ERR_THREAD_DETACHED;
+pub use crate::sys::ERR_I2C_NACK;
+pub use crate::sys::ERR_ALREADY_EXPIRED;
+pub use crate::sys::ERR_OUT_OF_RANGE;
+pub use crate::sys::ERR_NOT_CONFIGURED;
+pub use crate::sys::ERR_NOT_MOUNTED;
+pub use crate::sys::ERR_FAULT;
+pub use crate::sys::ERR_NO_RESOURCES;
+pub use crate::sys::ERR_BAD_HANDLE;
+pub use crate::sys::ERR_ACCESS_DENIED;
+pub use crate::sys::ERR_PARTIAL_WRITE; \ No newline at end of file
diff --git a/lib/rust_support/lib.rs b/lib/rust_support/lib.rs
index 8f6464a3..c77f0056 100644
--- a/lib/rust_support/lib.rs
+++ b/lib/rust_support/lib.rs
@@ -31,8 +31,17 @@ use core::ffi::CStr;
use core::panic::PanicInfo;
mod sys {
+ #![allow(unused)]
+ #![allow(non_camel_case_types)]
include!(env!("BINDGEN_INC_FILE"));
}
+pub mod err;
+pub mod mmu;
+pub mod vmm;
+
+pub use sys::status_t;
+pub use sys::vaddr_t;
+pub use sys::paddr_t;
#[panic_handler]
fn handle_panic(info: &PanicInfo) -> ! {
diff --git a/lib/rust_support/mmu.rs b/lib/rust_support/mmu.rs
new file mode 100644
index 00000000..efb03675
--- /dev/null
+++ b/lib/rust_support/mmu.rs
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2024 Google Inc. All rights reserved
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+pub use crate::sys::ARCH_MMU_FLAG_CACHED;
+pub use crate::sys::ARCH_MMU_FLAG_UNCACHED;
+pub use crate::sys::ARCH_MMU_FLAG_UNCACHED_DEVICE;
+pub use crate::sys::ARCH_MMU_FLAG_CACHE_MASK;
+pub use crate::sys::ARCH_MMU_FLAG_PERM_USER;
+pub use crate::sys::ARCH_MMU_FLAG_PERM_RO;
+pub use crate::sys::ARCH_MMU_FLAG_PERM_NO_EXECUTE;
+pub use crate::sys::ARCH_MMU_FLAG_NS;
+pub use crate::sys::ARCH_MMU_FLAG_TAGGED;
+pub use crate::sys::ARCH_MMU_FLAG_INVALID;
+
+pub use crate::sys::PAGE_SIZE;
+pub use crate::sys::PAGE_SIZE_SHIFT; \ No newline at end of file
diff --git a/lib/rust_support/rules.mk b/lib/rust_support/rules.mk
index 2fd8d422..7fc96fb8 100644
--- a/lib/rust_support/rules.mk
+++ b/lib/rust_support/rules.mk
@@ -36,6 +36,17 @@ MODULE_DEPS := \
MODULE_BINDGEN_ALLOW_FUNCTIONS := \
_panic \
+ vmm_alloc_physical_etc \
+ vmm_alloc_contiguous \
+ vmm_free_region \
+ vaddr_to_paddr \
+
+MODULE_BINDGEN_ALLOW_VARS := \
+ ARCH_MMU_FLAG_.* \
+ ERR_.* \
+ PAGE_SIZE \
+ PAGE_SIZE_SHIFT \
+ _kernel_aspace \
MODULE_BINDGEN_SRC_HEADER := $(LOCAL_DIR)/bindings.h
diff --git a/lib/rust_support/vmm.rs b/lib/rust_support/vmm.rs
new file mode 100644
index 00000000..08e293c0
--- /dev/null
+++ b/lib/rust_support/vmm.rs
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2024 Google Inc. All rights reserved
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+use core::ffi::c_char;
+use core::ffi::c_uint;
+use core::ffi::c_void;
+use core::ptr::addr_of_mut;
+
+use crate::status_t;
+use crate::paddr_t;
+
+pub use crate::sys::vmm_aspace_t;
+pub use crate::sys::vmm_alloc_physical_etc;
+pub use crate::sys::vmm_alloc_contiguous;
+pub use crate::sys::vmm_free_region;
+pub use crate::sys::vaddr_to_paddr;
+
+#[inline]
+pub fn vmm_get_kernel_aspace() -> *mut vmm_aspace_t {
+ // SAFETY: The returned raw pointer holds the same safety invariants as accessing a `static mut`,
+ // so this `unsafe` is unconditionally sound, and may become safe in edition 2024:
+ // <https://github.com/rust-lang/rust/issues/114447>.
+ unsafe { addr_of_mut!(crate::sys::_kernel_aspace) }
+}
+
+#[inline]
+pub unsafe fn vmm_alloc_physical(
+ aspace: *mut vmm_aspace_t,
+ name: *const c_char,
+ size: usize,
+ ptr: *const *mut c_void,
+ align_log2: u8,
+ paddr: *const paddr_t,
+ vmm_flags: c_uint,
+ arch_mmu_flags: c_uint,
+) -> status_t {
+ vmm_alloc_physical_etc(
+ aspace,
+ name,
+ size,
+ ptr.cast_mut(),
+ align_log2,
+ paddr.cast_mut(),
+ 1,
+ vmm_flags,
+ arch_mmu_flags
+ )
+} \ No newline at end of file