From 40cbc3f043eeea8b8f27ece943f42437a264239b Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Tue, 24 Sep 2019 01:14:15 +0200 Subject: Initial Rust support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently we have several lines of work:   - Integrating with the kernel tree and build system (Nick's & mine, both uploaded as branches, based on `rustc`; and another one I have been working on, based on `cargo`).   - Bindings and the first bits of functionality (Alex's & Geoffrey's, based on `cargo`). This patch effectively merges the work we have been doing and integrates it in the latest mainline kernel tree. This does *not* mean anything needs to stay as-is, but this gives us a working, common base to work and experiment upon. Hopefully, it will also attract external people to join! As a summary, I added: - `cargo` integration with the kernel `Makefile`s:   + Virtual `cargo` workspace to have a single lock file and to share deps between `cargo` jobs. + Picks the same optimization level as configured for C. + Verbose output on `V=1`. + A `cargoclean` target to clean all the Rust-related artifacts. - Initial support for built-in modules (i.e. `Y` as well as `M`): + It is a hack, we need to implement a few things (see the `TODO`s), but it is enough to start playing with things that depend on `MODULE`. + Passes `--cfg module` to built-in modules to be able to compile conditionally inside Rust. + Increased `KSYM_NAME_LEN` length to avoid warnings due to Rust long mangled symbols.   - Rust infrastructure in a new top level folder `rust/`: + A `kernel` package which contains the sources from Alex & Geoffrey's work plus some changes:     * Adapted `build.rs`. * Removed the `THIS_MODULE` emulation until it is implemented.     * Removed `Makefile` logic and the code that `cfg`-depended on kernel version (no need in mainline). * Moved the helpers to be triggered via normal compilation, renamed them under `rust_*` and exported via `EXPORT_SYMBOL` instead. * Added a prelude. + A `shlex` package which serves as an example of an "inline" dependency (i.e. package checked out copy to avoid the network) - The example driver was setup at `drivers/char/rust_example/`. - Misc + The beginning of `Documentation/rust/` with a quick start guide. + `MAINTAINERS` entry. + SPDXs for all files. Other notes that aren't in `TODO`s:   - We could minimize the network requirements (use `bindgen` binary, use more inline dependencies...), but it is not clear it would be a good idea for the moment due to `core`/`alloc`/`compiler-builtins`. - The intention of `rust/` is to have a place to put extra dependencies and split the `kernel` package into several in the future if it grows. It could resemble the usual kernel tree structure. - With several drivers being built-in, `cargo` recompiles `kernel` and triggers duplicate symbol errors when merging thin archives. We need to make it only compile `kernel` once.   - When the above works, then `make`'s `-j` calling concurrent `cargo`s (e.g. several drivers at the same time) should be OK, I think. According to https://github.com/rust-lang/cargo/pull/2486 it shouldn't miscompile anything, but if they start locking on each other we will have make jobs waiting that could have been doing something else. Signed-off-by: Miguel Ojeda --- rust/kernel/src/filesystem.rs | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 rust/kernel/src/filesystem.rs (limited to 'rust/kernel/src/filesystem.rs') diff --git a/rust/kernel/src/filesystem.rs b/rust/kernel/src/filesystem.rs new file mode 100644 index 000000000000..11b415008f68 --- /dev/null +++ b/rust/kernel/src/filesystem.rs @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 + +use alloc::boxed::Box; +use core::default::Default; +use core::marker; + +use crate::bindings; +use crate::c_types; +use crate::error; +use crate::types::CStr; + +pub struct Registration { + _phantom: marker::PhantomData, + ptr: Box, +} + +// This is safe because Registration doesn't actually expose any methods. +unsafe impl Sync for Registration where T: FileSystem {} + +impl Drop for Registration { + fn drop(&mut self) { + unsafe { bindings::unregister_filesystem(&mut *self.ptr) }; + } +} + +pub trait FileSystem: Sync { + const NAME: &'static CStr; + const FLAGS: FileSystemFlags; +} + +bitflags::bitflags! { + pub struct FileSystemFlags: c_types::c_int { + const REQUIRES_DEV = bindings::FS_REQUIRES_DEV as c_types::c_int; + const BINARY_MOUNTDATA = bindings::FS_BINARY_MOUNTDATA as c_types::c_int; + const HAS_SUBTYPE = bindings::FS_HAS_SUBTYPE as c_types::c_int; + const USERNS_MOUNT = bindings::FS_USERNS_MOUNT as c_types::c_int; + const RENAME_DOES_D_MOVE = bindings::FS_RENAME_DOES_D_MOVE as c_types::c_int; + } +} + +extern "C" fn fill_super_callback( + _sb: *mut bindings::super_block, + _data: *mut c_types::c_void, + _silent: c_types::c_int, +) -> c_types::c_int { + // T::fill_super(...) + // This should actually create an object that gets dropped by + // file_system_registration::kill_sb. You can point to it with + // sb->s_fs_info. + unimplemented!(); +} + +extern "C" fn mount_callback( + fs_type: *mut bindings::file_system_type, + flags: c_types::c_int, + _dev_name: *const c_types::c_char, + data: *mut c_types::c_void, +) -> *mut bindings::dentry { + unsafe { bindings::mount_nodev(fs_type, flags, data, Some(fill_super_callback::)) } +} + +pub fn register() -> error::KernelResult> { + let mut fs_registration = Registration { + ptr: Box::new(bindings::file_system_type { + name: T::NAME.as_ptr() as *const i8, + // TODO: proper `THIS_MODULE` handling + owner: core::ptr::null_mut(), + fs_flags: T::FLAGS.bits(), + mount: Some(mount_callback::), + kill_sb: Some(bindings::kill_litter_super), + + ..Default::default() + }), + _phantom: marker::PhantomData, + }; + let result = unsafe { bindings::register_filesystem(&mut *fs_registration.ptr) }; + if result != 0 { + return Err(error::Error::from_kernel_errno(result)); + } + + Ok(fs_registration) +} -- cgit v1.2.3 From 351ab09b7d2b338d34125bfaf684348303cb6909 Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Sat, 5 Sep 2020 03:13:37 +0200 Subject: Alex's review Signed-off-by: Miguel Ojeda --- rust/kernel/src/filesystem.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'rust/kernel/src/filesystem.rs') diff --git a/rust/kernel/src/filesystem.rs b/rust/kernel/src/filesystem.rs index 11b415008f68..a5b7774292f1 100644 --- a/rust/kernel/src/filesystem.rs +++ b/rust/kernel/src/filesystem.rs @@ -1,5 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 +// TODO: `filesystem` is really incomplete -- deletion to be considered + use alloc::boxed::Box; use core::default::Default; use core::marker; -- cgit v1.2.3