From 37872aa54efec5c29d847d853951a7157fde2003 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Mon, 6 Nov 2023 23:11:35 +1100 Subject: [PATCH 01/11] Add `std` and `clean-std` targets Signed-off-by: Klimenty Tsoutsman --- .cargo/config.toml | 2 + .gitmodules | 3 + .ignore | 1 + Cargo.lock | 23 + Cargo.toml | 2 + Makefile | 63 +- applications/test_std/Cargo.toml | 6 + applications/test_std/src/lib.rs | 7 + cfg/Config.mk | 2 +- kernel/environment/src/lib.rs | 2 +- kernel/libtheseus/Cargo.toml | 14 + kernel/libtheseus/src/lib.rs | 185 ++++ kernel/panic_entry/src/lib.rs | 13 + kernel/thread_local_macro/src/lib.rs | 2 +- libs/theseus_ffi/Cargo.toml | 12 + libs/theseus_ffi/src/lib.rs | 306 ++++++ libtheseus/Cargo.lock | 1423 -------------------------- libtheseus/Cargo.toml | 45 - libtheseus/build.sh | 56 - libtheseus/src/lib.rs | 85 -- libtheseus/src/my_mod.rs | 3 - ports/rust | 1 + shim/Cargo.lock | 39 + shim/Cargo.toml | 13 + shim/src/lib.rs | 203 ++++ theseus_features/Cargo.toml | 2 + 26 files changed, 885 insertions(+), 1628 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 .ignore create mode 100644 applications/test_std/Cargo.toml create mode 100644 applications/test_std/src/lib.rs create mode 100644 kernel/libtheseus/Cargo.toml create mode 100644 kernel/libtheseus/src/lib.rs create mode 100644 libs/theseus_ffi/Cargo.toml create mode 100644 libs/theseus_ffi/src/lib.rs delete mode 100644 libtheseus/Cargo.lock delete mode 100644 libtheseus/Cargo.toml delete mode 100755 libtheseus/build.sh delete mode 100644 libtheseus/src/lib.rs delete mode 100644 libtheseus/src/my_mod.rs create mode 160000 ports/rust create mode 100644 shim/Cargo.lock create mode 100644 shim/Cargo.toml create mode 100644 shim/src/lib.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000000..3e180cd255 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +rustc = "/Users/klim/Projects/theseus-5/ports/rust/build/aarch64-apple-darwin/stage1/bin/rustc" diff --git a/.gitmodules b/.gitmodules index fa662cb6f2..71729230d5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -22,3 +22,6 @@ path = libs/core2 url = https://github.com/theseus-os/core2.git shallow = true +[submodule "ports/rust"] + path = ports/rust + url = https://github.com/theseus-os/rust.git diff --git a/.ignore b/.ignore new file mode 100644 index 0000000000..c5b4d1d786 --- /dev/null +++ b/.ignore @@ -0,0 +1 @@ +ports/rust diff --git a/Cargo.lock b/Cargo.lock index 35c2f6ea2c..7f235abe98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1845,6 +1845,20 @@ dependencies = [ "task", ] +[[package]] +name = "libtheseus" +version = "0.1.0" +dependencies = [ + "app_io", + "core2", + "environment", + "path", + "random", + "task", + "theseus_ffi", + "thread_local_macro", +] + [[package]] name = "linked_list_allocator" version = "0.9.1" @@ -3866,6 +3880,10 @@ dependencies = [ "task", ] +[[package]] +name = "test_std" +version = "0.1.0" + [[package]] name = "test_std_fs" version = "0.1.0" @@ -4021,6 +4039,7 @@ dependencies = [ "test_preemption_counter", "test_restartable", "test_scheduler", + "test_std", "test_std_fs", "test_sync_block", "test_task_cancel", @@ -4035,6 +4054,10 @@ dependencies = [ "wasm", ] +[[package]] +name = "theseus_ffi" +version = "0.1.0" + [[package]] name = "theseus_std" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 250e103cc9..18a7cbac36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,8 @@ default-members = [ exclude = [ + "shim", + ## Exclude the build directories "build", "target", diff --git a/Makefile b/Makefile index d6d1d219f1..5046961570 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,7 @@ else $(error Error:unsupported option "boot_spec=$(boot_spec)". Options are 'bios' or 'uefi') endif +HOST_TRIPLE = $(shell rustc -vV | sed -n 's|host: ||p') ## test for Windows Subsystem for Linux (Linux on Windows) IS_WSL = $(shell grep -is 'microsoft' /proc/version) @@ -68,6 +69,7 @@ THESEUS_CARGO := $(ROOT_DIR)/tools/theseus_cargo THESEUS_CARGO_BIN := $(THESEUS_CARGO)/bin/theseus_cargo EXTRA_FILES := $(ROOT_DIR)/extra_files LIMINE_DIR := $(ROOT_DIR)/limine-prebuilt +RUST_SOURCE := $(ROOT_DIR)/ports/rust ### Set up tool names/locations for cross-compiling on a Mac OS / macOS host (Darwin). @@ -77,6 +79,7 @@ ifeq ($(UNAME),Darwin) ## macOS uses a different unmounting utility UNMOUNT = diskutil unmount USB_DRIVES = $(shell diskutil list external | grep -s "/dev/" | awk '{print $$1}') + HEAD = ghead else ## Handle building for aarch64 on x86_64 Linux/WSL ifeq ($(ARCH),aarch64) @@ -85,6 +88,7 @@ else ## Just use normal umount on Linux/WSL UNMOUNT = umount USB_DRIVES = $(shell lsblk -O | grep -i usb | awk '{print $$2}' | grep --color=never '[^0-9]$$') + HEAD = head endif ### Handle multiple bootloader options and ensure the corresponding tools are installed. @@ -157,7 +161,6 @@ APP_CRATE_NAMES += $(EXTRA_APP_CRATE_NAMES) check-usb \ clean clean-doc clean-old-build \ orun orun_pause run run_pause iso build cargo copy_kernel $(bootloader) extra_files \ - libtheseus \ simd_personality_sse build_sse simd_personality_avx build_avx \ gdb gdb_aarch64 \ clippy doc docs view-doc view-docs book view-book @@ -250,7 +253,6 @@ build: $(nano_core_binary) -a ./applications \ --kernel-prefix $(KERNEL_PREFIX) \ --app-prefix $(APP_PREFIX) \ - -e "$(EXTRA_APP_CRATE_NAMES) libtheseus" ## Third, perform partial linking on each object file, which shrinks their size ## and most importantly, accelerates their loading and linking at runtime. @@ -313,10 +315,33 @@ endif ### end of "build" target ### ############################# - +std: +## Cache std/Cargo.toml + cp $(RUST_SOURCE)/library/std/Cargo.toml $(ROOT_DIR)/std-cargo.toml +## Remove the last line of std/Cargo.toml + @$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml + @mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml +## Add the correct dependency path, since the dependency path in std/Cargo.toml assumes that std is +## being built using -Zbuild-std. + @echo "theseus-shim = { path = \"../../../../shim\", features = ['rustc-dep-of-std'] }" >> $(RUST_SOURCE)/library/std/Cargo.toml + +## Build the compiler + @cd $(RUST_SOURCE) && RUSTFLAGS="" CARGOFLAGS="" ./x.py build library --stage 1 + @cd $(ROOT_DIR) + +## Remove the last line of std/Cargo.toml + @$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml + @mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml +## Restore previous std/Cargo.toml + mv $(ROOT_DIR)/std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml + +## Add .cargo/config.toml + @mkdir -p $(ROOT_DIR)/.cargo + @echo "[build]" > $(ROOT_DIR)/.cargo/config.toml + @echo "rustc = \"$(RUST_SOURCE)/build/$(HOST_TRIPLE)/stage1/bin/rustc\"" >> $(ROOT_DIR)/.cargo/config.toml ## This target invokes the actual Rust build process via `cargo`. -cargo: +cargo: std @mkdir -p $(BUILD_DIR) @mkdir -p $(NANO_CORE_BUILD_DIR) @mkdir -p $(OBJECT_FILES_BUILD_DIR) @@ -470,14 +495,6 @@ c_test: -### Demo/test target for building libtheseus -libtheseus: theseus_cargo $(ROOT_DIR)/libtheseus/Cargo.* $(ROOT_DIR)/libtheseus/src/* - @( \ - cd $(ROOT_DIR)/libtheseus && \ - $(THESEUS_CARGO_BIN) --input $(DEPS_BUILD_DIR) build; \ - ) - - ### This target builds the `theseus_cargo` tool as a dedicated binary. theseus_cargo: $(wildcard $(THESEUS_CARGO)/Cargo.*) $(wildcard$(THESEUS_CARGO)/src/*) @echo -e "\n=================== Building the theseus_cargo tool ===================" @@ -489,6 +506,26 @@ theseus_cargo: $(wildcard $(THESEUS_CARGO)/Cargo.*) $(wildcard$(THESEUS_CARGO)/ clean: @rm -rf $(BUILD_DIR) cargo clean + +clean-std: clean + @rm -rf $(ROOT_DIR)/.cargo +## Cache std/Cargo.toml + cp $(RUST_SOURCE)/library/std/Cargo.toml $(ROOT_DIR)/std-cargo.toml +## Remove the last line of std/Cargo.toml + @$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml + @mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml +## Add the correct dependency path, since the dependency path in std/Cargo.toml assumes that std is +## being built using -Zbuild-std. + @echo "theseus-shim = { path = \"../../../../shim\", features = ['rustc-dep-of-std'] }" >> $(RUST_SOURCE)/library/std/Cargo.toml + + @cd $(RUST_SOURCE) && RUSTFLAGS="" CARGOFLAGS="" ./x.py clean + @cd $(ROOT_DIR) + +## Remove the last line of std/Cargo.toml + @$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml + @mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml +## Restore previous std/Cargo.toml + mv $(ROOT_DIR)/std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml ### Removes only the old files that were copied into the build directory from a previous build. @@ -615,7 +652,7 @@ else ifeq ($(ARCH),aarch64) clippy : export override FEATURES := $(subst --workspace,,$(FEATURES)) endif clippy : export override RUSTFLAGS = $(patsubst %,--cfg %, $(THESEUS_CONFIG)) -clippy: +clippy: std RUST_TARGET_PATH='$(CFG_DIR)' RUSTFLAGS='$(RUSTFLAGS)' \ cargo clippy \ $(BUILD_STD_CARGOFLAGS) $(FEATURES) \ diff --git a/applications/test_std/Cargo.toml b/applications/test_std/Cargo.toml new file mode 100644 index 0000000000..2c52bf19f7 --- /dev/null +++ b/applications/test_std/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "test_std" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/applications/test_std/src/lib.rs b/applications/test_std/src/lib.rs new file mode 100644 index 0000000000..b87c270f6d --- /dev/null +++ b/applications/test_std/src/lib.rs @@ -0,0 +1,7 @@ +use std::{string::String, vec::Vec}; + +pub fn main(_: Vec) -> isize { + println!("Hello, world!"); + println!("Task ID: {}", std::process::id()); + 0 +} diff --git a/cfg/Config.mk b/cfg/Config.mk index f01a672d54..eed72c3723 100644 --- a/cfg/Config.mk +++ b/cfg/Config.mk @@ -70,7 +70,7 @@ endif ## Also ensure that core memory functions (e.g., memcpy) are included in the build and not name-mangled. ## We keep these flags separate from the regular CARGOFLAGS for purposes of easily creating a sysroot directory. BUILD_STD_CARGOFLAGS += -Z unstable-options -BUILD_STD_CARGOFLAGS += -Z build-std=core,alloc +BUILD_STD_CARGOFLAGS += -Z build-std BUILD_STD_CARGOFLAGS += -Z build-std-features=compiler-builtins-mem diff --git a/kernel/environment/src/lib.rs b/kernel/environment/src/lib.rs index df3e23d1dc..efd78b4e30 100644 --- a/kernel/environment/src/lib.rs +++ b/kernel/environment/src/lib.rs @@ -31,7 +31,7 @@ impl Environment { /// Changes the current working directory. #[doc(alias("change"))] pub fn chdir(&mut self, path: &Path) -> Result<()> { - let new_dir = self.working_dir.lock().get(path.as_ref()); + let new_dir = path.get(&self.working_dir); match new_dir { Some(FileOrDir::Dir(dir_ref)) => { self.working_dir = dir_ref; diff --git a/kernel/libtheseus/Cargo.toml b/kernel/libtheseus/Cargo.toml new file mode 100644 index 0000000000..dd27c35e45 --- /dev/null +++ b/kernel/libtheseus/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "libtheseus" +version = "0.1.0" +edition = "2021" + +[dependencies] +app_io = { path = "../app_io" } +core2 = { version = "0.4.0", default-features = false, features = ["alloc", "nightly"] } +environment = { path = "../environment" } +path = { path = "../path" } +random = { path = "../random" } +theseus_ffi = { path = "../../libs/theseus_ffi" } +task = { path = "../task" } +thread_local_macro = { path = "../thread_local_macro" } diff --git a/kernel/libtheseus/src/lib.rs b/kernel/libtheseus/src/lib.rs new file mode 100644 index 0000000000..e5338118b5 --- /dev/null +++ b/kernel/libtheseus/src/lib.rs @@ -0,0 +1,185 @@ +#![no_std] + +extern crate alloc; + +use alloc::{borrow::ToOwned, sync::Arc}; +use core::mem; + +use app_io::{ImmutableRead, ImmutableWrite}; +use path::Path; +use theseus_ffi::{Error, FatPointer, FfiOption, FfiResult, FfiSlice, FfiSliceMut, FfiStr, FfiString}; +use task::{KillReason, TaskRef}; + +const _: theseus_ffi::next_u64 = next_u64; +const _: theseus_ffi::getcwd = getcwd; +const _: theseus_ffi::chdir = chdir; +const _: theseus_ffi::getenv = getenv; +const _: theseus_ffi::setenv = setenv; +const _: theseus_ffi::unsetenv = unsetenv; +const _: theseus_ffi::exit = exit; +const _: theseus_ffi::getpid = getpid; +const _: theseus_ffi::register_dtor = register_dtor; +const _: theseus_ffi::stdin = stdin; +const _: theseus_ffi::stdout = stdout; +const _: theseus_ffi::stderr = stderr; +const _: theseus_ffi::read = read; +const _: theseus_ffi::write = write; +const _: theseus_ffi::flush = flush; +const _: theseus_ffi::drop_reader = drop_reader; +const _: theseus_ffi::drop_writer = drop_writer; + +fn current_task() -> TaskRef { + task::get_my_current_task().expect("failed to get current task") +} + +// Add the libtheseus:: so mod_mgmt knows which crate to search. + +#[export_name = "libtheseus::next_u64"] +pub extern "C" fn next_u64() -> u64 { + random::next_u64() +} + +#[export_name = "libtheseus::getcwd"] +pub extern "C" fn getcwd() -> FfiString { + current_task().get_env().lock().cwd().into() +} + +#[export_name = "libtheseus::chdir"] +pub extern "C" fn chdir(path: FfiStr<'_>) -> FfiResult<(), Error> { + current_task() + .get_env() + .lock() + .chdir(Path::new(path.into())) + .map_err(|e| match e { + environment::Error::NotADirectory => Error::NotADirectory, + environment::Error::NotFound => Error::NotFound, + }) + .into() +} + +#[export_name = "libtheseus::getenv"] +pub extern "C" fn getenv(key: FfiStr<'_>) -> FfiOption { + current_task() + .get_env() + .lock() + .get(key.into()) + .map(|s| s.to_owned().into()) + .into() +} + +#[export_name = "libtheseus::setenv"] +pub extern "C" fn setenv(key: FfiStr<'_>, value: FfiStr<'_>) -> FfiResult<(), Error> { + current_task() + .get_env() + .lock() + .set(<&str>::from(key).to_owned(), <&str>::from(value).to_owned()); + FfiResult::Ok(()) +} + +#[export_name = "libtheseus::unsetenv"] +pub extern "C" fn unsetenv(key: FfiStr<'_>) -> FfiResult<(), Error> { + current_task().get_env().lock().unset(key.into()); + FfiResult::Ok(()) +} + +#[export_name = "libtheseus::exit"] +pub extern "C" fn exit(_code: i32) -> ! { + // TODO: Supply correct reason. + current_task() + .kill(KillReason::Requested) + .expect("couldn't mark task as exited"); + task::schedule(); + panic!("task scheduled after exiting"); +} + +#[export_name = "libtheseus::getpid"] +pub extern "C" fn getpid() -> u32 { + task::get_my_current_task_id() + .try_into() + .expect("current task id too large") +} + +#[export_name = "libtheseus::register_dtor"] +pub unsafe extern "C" fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { + unsafe { thread_local_macro::register_dtor(t, dtor) } +} + +// TODO: Something better than transmutations. +// TODO: Explain why we shouldn't bother using stabby at least for trait objects. + +#[export_name = "libtheseus::stdin"] +pub extern "C" fn stdin() -> FfiResult { + let ptr: *const dyn ImmutableRead = Arc::into_raw(FfiResult::from( + app_io::stdin().map_err(|_| Error::BrokenPipe), + )?); + FfiResult::Ok(unsafe { mem::transmute(ptr) }) +} + +#[export_name = "libtheseus::stdout"] +pub extern "C" fn stdout() -> FfiResult { + let ptr: *const dyn ImmutableWrite = Arc::into_raw(FfiResult::from( + app_io::stdout().map_err(|_| Error::BrokenPipe), + )?); + FfiResult::Ok(unsafe { mem::transmute(ptr) }) +} + +#[export_name = "libtheseus::stderr"] +pub extern "C" fn stderr() -> FfiResult { + let ptr: *const dyn ImmutableWrite = Arc::into_raw(FfiResult::from( + app_io::stderr().map_err(|_| Error::BrokenPipe), + )?); + FfiResult::Ok(unsafe { mem::transmute(ptr) }) +} + +#[export_name = "libtheseus::read"] +pub unsafe extern "C" fn read(reader: FatPointer, buf: FfiSliceMut<'_, u8>) -> FfiResult { + let ptr: *const dyn ImmutableRead = unsafe { mem::transmute(reader) }; + let r = unsafe { &*ptr }; + FfiResult::from(r.read(buf.into()).map_err(from_core2)) +} + +#[export_name = "libtheseus::write"] +pub unsafe extern "C" fn write(writer: FatPointer, buf: FfiSlice<'_, u8>) -> FfiResult { + let ptr: *const dyn ImmutableWrite = unsafe { mem::transmute(writer) }; + let r = unsafe { &*ptr }; + FfiResult::from(r.write(buf.into()).map_err(from_core2)) +} + +#[export_name = "libtheseus::flush"] +pub unsafe extern "C" fn flush(_writer: FatPointer) -> FfiResult<(), Error> { + FfiResult::Ok(()) +} + +#[export_name = "libtheseus::drop_reader"] +pub unsafe extern "C" fn drop_reader(reader: FatPointer) { + Arc::::from_raw(unsafe { mem::transmute(reader) }); +} + +#[export_name = "libtheseus::drop_writer"] +pub unsafe extern "C" fn drop_writer(writer: FatPointer) { + Arc::::from_raw(unsafe { mem::transmute(writer) }); +} + +fn from_core2(e: core2::io::Error) -> Error { + match e.kind() { + core2::io::ErrorKind::NotFound => Error::NotFound, + core2::io::ErrorKind::PermissionDenied => Error::PermissionDenied, + core2::io::ErrorKind::ConnectionRefused => Error::ConnectionRefused, + core2::io::ErrorKind::ConnectionReset => Error::ConnectionReset, + core2::io::ErrorKind::ConnectionAborted => Error::ConnectionAborted, + core2::io::ErrorKind::NotConnected => Error::NotConnected, + core2::io::ErrorKind::AddrInUse => Error::AddrInUse, + core2::io::ErrorKind::AddrNotAvailable => Error::AddrNotAvailable, + core2::io::ErrorKind::BrokenPipe => Error::BrokenPipe, + core2::io::ErrorKind::AlreadyExists => Error::AlreadyExists, + core2::io::ErrorKind::WouldBlock => Error::WouldBlock, + core2::io::ErrorKind::InvalidInput => Error::InvalidInput, + core2::io::ErrorKind::InvalidData => Error::InvalidData, + core2::io::ErrorKind::TimedOut => Error::TimedOut, + core2::io::ErrorKind::WriteZero => Error::WriteZero, + core2::io::ErrorKind::Interrupted => Error::Interrupted, + core2::io::ErrorKind::UnexpectedEof => Error::UnexpectedEof, + core2::io::ErrorKind::Other => Error::Other, + _ => Error::Other, + } +} diff --git a/kernel/panic_entry/src/lib.rs b/kernel/panic_entry/src/lib.rs index 68a6416703..977dcb9fb4 100644 --- a/kernel/panic_entry/src/lib.rs +++ b/kernel/panic_entry/src/lib.rs @@ -132,3 +132,16 @@ fn oom(_layout: core::alloc::Layout) -> ! { error!("\n(oom) Out of Heap Memory! requested allocation: {:?}", _layout); panic!("\n(oom) Out of Heap Memory! requested allocation: {:?}", _layout); } + +// TODO: Are these truly unreachable? + +#[no_mangle] +pub unsafe extern "C" fn __rust_start_panic() { + unreachable!(); +} + +#[no_mangle] +#[allow(improper_ctypes_definitions)] +pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn core::any::Any + Send + 'static) { + unreachable!(); +} diff --git a/kernel/thread_local_macro/src/lib.rs b/kernel/thread_local_macro/src/lib.rs index e59ae985c3..76ab879a0d 100644 --- a/kernel/thread_local_macro/src/lib.rs +++ b/kernel/thread_local_macro/src/lib.rs @@ -91,7 +91,7 @@ pub fn take_current_tls_destructors() -> Vec { /// /// Currently the only value of `dtor` that is used is a type-specific monomorphized /// version of the above [`fast::destroy_value()`] function. -fn register_dtor(object_ptr: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { +pub unsafe fn register_dtor(object_ptr: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { TLS_DESTRUCTORS.borrow_mut().push(TlsObjectDestructor { object_ptr, dtor }); } diff --git a/libs/theseus_ffi/Cargo.toml b/libs/theseus_ffi/Cargo.toml new file mode 100644 index 0000000000..2d70a7a481 --- /dev/null +++ b/libs/theseus_ffi/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "theseus_ffi" +version = "0.1.0" +edition = "2021" + +[dependencies] +alloc = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-alloc' } +core = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-core' } +compiler_builtins = { version = "0.1.73", optional = true } + +[features] +rustc-dep-of-std = ['alloc', 'core', 'compiler_builtins'] diff --git a/libs/theseus_ffi/src/lib.rs b/libs/theseus_ffi/src/lib.rs new file mode 100644 index 0000000000..6074053306 --- /dev/null +++ b/libs/theseus_ffi/src/lib.rs @@ -0,0 +1,306 @@ +#![no_std] +#![feature(vec_into_raw_parts, try_trait_v2, never_type, exhaustive_patterns)] +#![allow(non_camel_case_types)] + +#[cfg(not(feature = "rustc-dep-of-std"))] +extern crate alloc; + +use alloc::string::String; +use core::{ + convert::Infallible, + marker::PhantomData, + ops::{ControlFlow, FromResidual, Try}, + str, +}; + +pub type next_u64 = unsafe extern "C" fn() -> u64; +pub type getcwd = unsafe extern "C" fn() -> FfiString; +pub type chdir = unsafe extern "C" fn(path: FfiStr<'_>) -> FfiResult<(), Error> ; +pub type getenv = unsafe extern "C" fn(key: FfiStr<'_>) -> FfiOption; +pub type setenv = unsafe extern "C" fn(key: FfiStr<'_>, value: FfiStr<'_>) -> FfiResult<(), Error>; +pub type unsetenv = unsafe extern "C" fn(key: FfiStr<'_>) -> FfiResult<(), Error>; +pub type exit = unsafe extern "C" fn(code: i32) -> !; +pub type getpid = unsafe extern "C" fn() -> u32; +pub type register_dtor = unsafe extern "C" fn(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)); +pub type stdin = unsafe extern "C" fn() -> FfiResult; +pub type stdout = unsafe extern "C" fn() -> FfiResult; +pub type stderr = unsafe extern "C" fn() -> FfiResult; +pub type read = unsafe extern "C" fn(reader: FatPointer, buf: FfiSliceMut<'_, u8>) -> FfiResult; +pub type write = unsafe extern "C" fn(writer: FatPointer, buf: FfiSlice<'_, u8>) -> FfiResult; +pub type flush = unsafe extern "C" fn(writer: FatPointer) -> FfiResult<(), Error>; +pub type drop_reader = unsafe extern "C" fn(reader: FatPointer); +pub type drop_writer = unsafe extern "C" fn(writer: FatPointer); + +#[derive(Debug)] +#[repr(C)] +pub struct FfiString { + buf: *mut u8, + length: usize, + capacity: usize, +} + +impl From for FfiString { + fn from(value: String) -> Self { + let (buf, length, capacity) = value.into_raw_parts(); + Self { + buf, + length, + capacity, + } + } +} + +impl From for String { + fn from( + FfiString { + buf, + length, + capacity, + }: FfiString, + ) -> Self { + unsafe { Self::from_raw_parts(buf, length, capacity) } + } +} + +#[derive(Debug)] +#[repr(C)] +pub struct FfiStr<'a> { + inner: FfiSlice<'a, u8>, +} + +impl<'a> From<&'a str> for FfiStr<'a> { + fn from(value: &'a str) -> Self { + Self { + inner: value.as_bytes().into(), + } + } +} + +impl<'a> From> for &'a str { + fn from(FfiStr { inner }: FfiStr<'a>) -> Self { + unsafe { str::from_utf8_unchecked(inner.into()) } + } +} + +#[derive(Debug)] +#[repr(C)] +pub struct FfiStrMut<'a> { + inner: FfiSliceMut<'a, u8>, +} + +impl<'a> From<&'a mut str> for FfiStrMut<'a> { + fn from(value: &'a mut str) -> Self { + Self { + inner: unsafe { value.as_bytes_mut() }.into(), + } + } +} + +impl<'a> From> for &'a mut str { + fn from(FfiStrMut { inner }: FfiStrMut<'a>) -> Self { + unsafe { str::from_utf8_unchecked_mut(inner.into()) } + } +} + +#[derive(Debug)] +#[repr(C)] +pub struct FfiSlice<'a, T> { + buf: *const T, + length: usize, + _phantom_data: PhantomData<&'a T>, +} + +impl<'a, T> From<&'a [T]> for FfiSlice<'a, T> { + fn from(value: &'a [T]) -> Self { + Self { + buf: value.as_ptr(), + length: value.len(), + _phantom_data: PhantomData, + } + } +} + +impl<'a, T> From> for &'a [T] { + fn from(value: FfiSlice<'a, T>) -> Self { + unsafe { core::slice::from_raw_parts(value.buf, value.length) } + } +} + +#[derive(Debug)] +#[repr(C)] +pub struct FfiSliceMut<'a, T> { + buf: *mut T, + length: usize, + _phantom_data: PhantomData<&'a T>, +} + +impl<'a, T> From<&'a mut [T]> for FfiSliceMut<'a, T> { + fn from(value: &'a mut [T]) -> Self { + Self { + buf: value.as_mut_ptr(), + length: value.len(), + _phantom_data: PhantomData, + } + } +} + +impl<'a, T> From> for &'a mut [T] { + fn from(value: FfiSliceMut<'a, T>) -> Self { + unsafe { core::slice::from_raw_parts_mut(value.buf, value.length) } + } +} + +#[derive(Debug)] +#[repr(C)] +pub enum FfiResult { + Ok(T), + Err(E), +} + +impl From> for Result { + fn from(value: FfiResult) -> Self { + match value { + FfiResult::Ok(t) => Ok(t), + FfiResult::Err(e) => Err(e), + } + } +} + +impl From> for FfiResult { + fn from(value: Result) -> Self { + match value { + Ok(t) => FfiResult::Ok(t), + Err(e) => FfiResult::Err(e), + } + } +} + +impl Try for FfiResult { + type Output = T; + + type Residual = FfiResult; + + fn from_output(output: Self::Output) -> Self { + Self::Ok(output) + } + + fn branch(self) -> ControlFlow { + match self { + Self::Ok(v) => ControlFlow::Continue(v), + Self::Err(e) => ControlFlow::Break(FfiResult::Err(e)), + } + } +} + +impl FromResidual for FfiResult { + fn from_residual(residual: ::Residual) -> Self { + match residual { + FfiResult::Err(e) => Self::Err(e), + } + } +} + +#[derive(Debug)] +#[repr(C)] +pub enum FfiOption { + Some(T), + None, +} + +impl From> for Option { + fn from(value: FfiOption) -> Self { + match value { + FfiOption::Some(t) => Some(t), + FfiOption::None => None, + } + } +} + +impl From> for FfiOption { + fn from(value: Option) -> Self { + match value { + Some(t) => FfiOption::Some(t), + None => FfiOption::None, + } + } +} + +impl Try for FfiOption { + type Output = T; + + type Residual = FfiOption; + + fn from_output(output: Self::Output) -> Self { + Self::Some(output) + } + + fn branch(self) -> ControlFlow { + match self { + Self::Some(v) => ControlFlow::Continue(v), + Self::None => ControlFlow::Break(FfiOption::None), + } + } +} + +impl FromResidual for FfiOption { + fn from_residual(residual: ::Residual) -> Self { + match residual { + FfiOption::None => Self::None, + } + } +} + +#[derive(Debug, Clone, Copy)] +#[repr(C)] +pub enum Error { + NotFound, + PermissionDenied, + ConnectionRefused, + ConnectionReset, + HostUnreachable, + NetworkUnreachable, + ConnectionAborted, + NotConnected, + AddrInUse, + AddrNotAvailable, + NetworkDown, + BrokenPipe, + AlreadyExists, + WouldBlock, + NotADirectory, + IsADirectory, + DirectoryNotEmpty, + ReadOnlyFilesystem, + FilesystemLoop, + StaleNetworkFileHandle, + InvalidInput, + InvalidData, + TimedOut, + WriteZero, + StorageFull, + NotSeekable, + FilesystemQuotaExceeded, + FileTooLarge, + ResourceBusy, + ExecutableFileBusy, + Deadlock, + CrossesDevices, + TooManyLinks, + InvalidFilename, + ArgumentListTooLong, + Interrupted, + Unsupported, + UnexpectedEof, + OutOfMemory, + Other, +} + +#[derive(Debug, Clone)] +#[repr(C)] +pub struct FatPointer { + _a: *mut (), + _b: *mut (), +} + +const _FAT_POINTER_SIZE: () = + assert!(core::mem::size_of::() == core::mem::size_of::<*const dyn core::fmt::Debug>()); diff --git a/libtheseus/Cargo.lock b/libtheseus/Cargo.lock deleted file mode 100644 index 7eb31c4ad8..0000000000 --- a/libtheseus/Cargo.lock +++ /dev/null @@ -1,1423 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "ahash" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - -[[package]] -name = "apic" -version = "0.1.0" -dependencies = [ - "atomic_linked_list", - "bit_field 0.7.0", - "crossbeam-utils", - "irq_safety", - "kernel_config", - "log", - "memory", - "msr", - "pit_clock_basic", - "raw-cpuid", - "spin 0.9.4", - "static_assertions", - "volatile 0.2.7", - "x86_64", - "zerocopy", -] - -[[package]] -name = "atomic_linked_list" -version = "0.1.0" - -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - -[[package]] -name = "bincode" -version = "2.0.0-rc.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bb50c5a2ef4b9b1e7ae73e3a73b52ea24b20312d629f9c4df28260b7ad2c3c4" -dependencies = [ - "serde", -] - -[[package]] -name = "bit_field" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff91a64014e1bc53bf643920f2c9ab5f0980d92a0948295f3ee550e9266849ad" - -[[package]] -name = "bit_field" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4" - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "block_allocator" -version = "0.1.0" -dependencies = [ - "linked_list_allocator", -] - -[[package]] -name = "bootloader_modules" -version = "0.1.0" -dependencies = [ - "memory_structs", -] - -[[package]] -name = "byteorder" -version = "1.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" - -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "const_format" -version = "0.2.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7309d9b4d3d2c0641e018d449232f2e28f1b22933c137f157d3dbc14228b8c0e" -dependencies = [ - "const_format_proc_macros", -] - -[[package]] -name = "const_format_proc_macros" -version = "0.2.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f47bf7270cf70d370f8f98c1abb6d2d4cf60a6845d30e05bfb90c6568650" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - -[[package]] -name = "context_switch" -version = "0.1.0" -dependencies = [ - "cfg-if 0.1.10", - "context_switch_avx", - "context_switch_regular", - "context_switch_sse", -] - -[[package]] -name = "context_switch_avx" -version = "0.1.0" -dependencies = [ - "context_switch_regular", - "zerocopy", -] - -[[package]] -name = "context_switch_regular" -version = "0.1.0" -dependencies = [ - "zerocopy", -] - -[[package]] -name = "context_switch_sse" -version = "0.1.0" -dependencies = [ - "context_switch_regular", - "zerocopy", -] - -[[package]] -name = "convert_case" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" - -[[package]] -name = "core2" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b49ba7ef1ad6107f8824dbe97de947cbaac53c44e7f9756a1fba0d37c1eec505" -dependencies = [ - "memchr", -] - -[[package]] -name = "cow_arc" -version = "0.1.0" -dependencies = [ - "dereffer", - "spin 0.9.4", -] - -[[package]] -name = "crate_metadata" -version = "0.1.0" -dependencies = [ - "cow_arc", - "crate_metadata_serde", - "fs_node", - "goblin", - "hashbrown", - "log", - "memory", - "qp-trie", - "serde", - "spin 0.9.4", - "static_assertions", - "str_ref", - "xmas-elf", -] - -[[package]] -name = "crate_metadata_serde" -version = "0.1.0" -dependencies = [ - "hashbrown", - "serde", -] - -[[package]] -name = "crate_name_utils" -version = "0.1.0" -dependencies = [ - "crate_metadata", - "itertools", - "path", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" -dependencies = [ - "cfg-if 1.0.0", -] - -[[package]] -name = "cstr_core" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd98742e4fdca832d40cab219dc2e3048de17d873248f83f17df47c1bea70956" -dependencies = [ - "cty", - "memchr", -] - -[[package]] -name = "cty" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" - -[[package]] -name = "delegate" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35c47a31748d9cfa641f6cccb3608385fafe261ba36054f3d40d5a3ca11eb1af" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "dereffer" -version = "0.1.0" - -[[package]] -name = "derive_more" -version = "0.99.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" -dependencies = [ - "convert_case", - "proc-macro2", - "quote", - "rustc_version 0.4.0", - "syn", -] - -[[package]] -name = "dfqueue" -version = "0.1.0" - -[[package]] -name = "either" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" - -[[package]] -name = "environment" -version = "0.1.0" -dependencies = [ - "fs_node", - "hashbrown", - "path", - "root", -] - -[[package]] -name = "event_types" -version = "0.1.0" -dependencies = [ - "keycodes_ascii", - "mouse_data", - "shapes", -] - -[[package]] -name = "exceptions_early" -version = "0.1.0" -dependencies = [ - "gdt", - "locked_idt", - "memory", - "mod_mgmt", - "spin 0.9.4", - "tss", - "vga_buffer", - "x86_64", -] - -[[package]] -name = "external_unwind_info" -version = "0.1.0" -dependencies = [ - "log", - "memory", - "spin 0.9.4", -] - -[[package]] -name = "fallible-iterator" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" - -[[package]] -name = "fault_log" -version = "0.1.0" -dependencies = [ - "apic", - "irq_safety", - "log", - "memory", - "print", - "task", - "vga_buffer", -] - -[[package]] -name = "frame_allocator" -version = "0.1.0" -dependencies = [ - "intrusive-collections", - "kernel_config", - "log", - "memory_structs", - "spin 0.9.4", - "static_assertions", -] - -[[package]] -name = "fs_node" -version = "0.1.0" -dependencies = [ - "io", - "lazy_static", - "log", - "memory", - "spin 0.9.4", -] - -[[package]] -name = "gdt" -version = "0.1.0" -dependencies = [ - "atomic_linked_list", - "bit_field 0.7.0", - "bitflags", - "log", - "memory", - "spin 0.9.4", - "tss", - "x86_64", -] - -[[package]] -name = "getrandom" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" -dependencies = [ - "cfg-if 1.0.0", - "libc", - "wasi", -] - -[[package]] -name = "gimli" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" - -[[package]] -name = "goblin" -version = "0.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c65cd533b33e3d04c6e393225fa8919ddfcf5862ca8919c7f9a167c312ef41c2" -dependencies = [ - "plain", - "scroll", -] - -[[package]] -name = "hashbrown" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" -dependencies = [ - "ahash", - "serde", -] - -[[package]] -name = "heap" -version = "0.1.0" -dependencies = [ - "block_allocator", - "irq_safety", - "kernel_config", - "log", - "memory", - "spin 0.9.4", -] - -[[package]] -name = "interrupts" -version = "0.1.0" -dependencies = [ - "apic", - "exceptions_early", - "gdt", - "locked_idt", - "log", - "memory", - "pic", - "scheduler", - "sleep", - "spin 0.9.4", - "tss", - "vga_buffer", - "x86_64", -] - -[[package]] -name = "intrusive-collections" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfe531a7789d7120f3e17d4f3f2cd95f54418ba7354f60b7b622b6644a07888a" -dependencies = [ - "memoffset", -] - -[[package]] -name = "io" -version = "0.1.0" -dependencies = [ - "core2", - "delegate", - "lazy_static", - "lockable", - "log", - "spin 0.9.4", -] - -[[package]] -name = "irq_safety" -version = "0.1.1" -source = "git+https://github.com/theseus-os/irq_safety#4908fbb38aca1513572e0b4c7a98884390ef361a" -dependencies = [ - "owning_ref", - "spin 0.9.4", - "stable_deref_trait", -] - -[[package]] -name = "itertools" -version = "0.7.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d" -dependencies = [ - "either", -] - -[[package]] -name = "kernel_config" -version = "0.1.0" - -[[package]] -name = "keycodes_ascii" -version = "0.1.0" -dependencies = [ - "bitflags", - "num_enum", -] - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -dependencies = [ - "spin 0.5.2", -] - -[[package]] -name = "libc" -version = "0.2.138" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" - -[[package]] -name = "libtheseus" -version = "0.1.0" -dependencies = [ - "heap", - "logger", - "panic_entry", -] - -[[package]] -name = "linked_list_allocator" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "549ce1740e46b291953c4340adcd74c59bcf4308f4cac050fd33ba91b7168f4a" - -[[package]] -name = "lock_api" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" -dependencies = [ - "autocfg", - "scopeguard", -] - -[[package]] -name = "lockable" -version = "0.1.0" -dependencies = [ - "irq_safety", - "spin 0.9.4", -] - -[[package]] -name = "locked_idt" -version = "0.1.0" -dependencies = [ - "irq_safety", - "x86_64", -] - -[[package]] -name = "log" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" -dependencies = [ - "cfg-if 0.1.10", -] - -[[package]] -name = "logger" -version = "0.1.0" -dependencies = [ - "irq_safety", - "log", - "serial_port_basic", - "spin 0.9.4", -] - -[[package]] -name = "memchr" -version = "2.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" - -[[package]] -name = "memfs" -version = "0.1.0" -dependencies = [ - "fs_node", - "io", - "irq_safety", - "log", - "memory", - "spin 0.9.4", -] - -[[package]] -name = "memoffset" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" -dependencies = [ - "autocfg", -] - -[[package]] -name = "memory" -version = "0.1.0" -dependencies = [ - "atomic_linked_list", - "bit_field 0.7.0", - "bitflags", - "frame_allocator", - "irq_safety", - "kernel_config", - "lazy_static", - "log", - "memory_structs", - "memory_x86_64", - "multiboot2", - "no_drop", - "owned_borrowed_trait", - "page_allocator", - "page_table_entry", - "pte_flags", - "spin 0.9.4", - "x86_64", - "xmas-elf", - "zerocopy", -] - -[[package]] -name = "memory_structs" -version = "0.1.0" -dependencies = [ - "bit_field 0.7.0", - "derive_more", - "kernel_config", - "paste", - "zerocopy", -] - -[[package]] -name = "memory_x86_64" -version = "0.1.0" -dependencies = [ - "kernel_config", - "log", - "memory_structs", - "multiboot2", - "pte_flags", - "x86_64", -] - -[[package]] -name = "mod_mgmt" -version = "0.1.0" -dependencies = [ - "bincode", - "bootloader_modules", - "const_format", - "cow_arc", - "crate_metadata", - "crate_metadata_serde", - "crate_name_utils", - "cstr_core", - "fs_node", - "hashbrown", - "kernel_config", - "log", - "memfs", - "memory", - "path", - "qp-trie", - "rangemap", - "root", - "rustc-demangle", - "serde", - "spin 0.9.4", - "util", - "vfs_node", - "xmas-elf", -] - -[[package]] -name = "modular-bitfield" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a53d79ba8304ac1c4f9eb3b9d281f21f7be9d4626f72ce7df4ad8fbde4f38a74" -dependencies = [ - "modular-bitfield-impl", - "static_assertions", -] - -[[package]] -name = "modular-bitfield-impl" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a7d5f7076603ebc68de2dc6a650ec331a062a13abaa346975be747bbfa4b789" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "mouse_data" -version = "0.1.0" -dependencies = [ - "modular-bitfield", -] - -[[package]] -name = "msr" -version = "0.1.0" - -[[package]] -name = "multiboot2" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6170b6f12ea75d8d0f5621e3ed780b041a666c4a5b904c77261fe343d0e798d" -dependencies = [ - "bitflags", -] - -[[package]] -name = "mutex_preemption" -version = "0.1.0" -dependencies = [ - "lockable", - "preemption", - "spin 0.9.4", -] - -[[package]] -name = "new_debug_unreachable" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" - -[[package]] -name = "no_drop" -version = "0.1.0" - -[[package]] -name = "num_enum" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf5395665662ef45796a4ff5486c5d41d29e0c09640af4c5f17fd94ee2c119c9" -dependencies = [ - "num_enum_derive", -] - -[[package]] -name = "num_enum_derive" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b0498641e53dd6ac1a4f22547548caa6864cc4933784319cd1775271c5a46ce" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "once_cell" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" - -[[package]] -name = "owned_borrowed_trait" -version = "0.1.0" - -[[package]] -name = "owning_ref" -version = "0.4.1" -source = "git+https://github.com/theseus-os/owning-ref-rs.git#0d2dcdcffd75b80157d0e72fb82593a8696a9c49" -dependencies = [ - "spin 0.9.4", - "stable_deref_trait", -] - -[[package]] -name = "page_allocator" -version = "0.1.0" -dependencies = [ - "intrusive-collections", - "kernel_config", - "log", - "memory_structs", - "spin 0.9.4", - "static_assertions", -] - -[[package]] -name = "page_table_entry" -version = "0.1.0" -dependencies = [ - "bit_field 0.7.0", - "frame_allocator", - "kernel_config", - "memory_structs", - "pte_flags", - "zerocopy", -] - -[[package]] -name = "panic_entry" -version = "0.1.0" -dependencies = [ - "log", - "memory", - "mod_mgmt", - "panic_wrapper", - "unwind", - "vga_buffer", -] - -[[package]] -name = "panic_wrapper" -version = "0.1.0" -dependencies = [ - "fault_log", - "log", - "memory", - "mod_mgmt", - "runqueue", - "stack_trace", - "stack_trace_frame_pointers", - "task", - "unwind", -] - -[[package]] -name = "paste" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1" - -[[package]] -name = "path" -version = "0.1.0" -dependencies = [ - "fs_node", - "lazy_static", - "log", - "root", - "spin 0.9.4", - "vfs_node", -] - -[[package]] -name = "pic" -version = "0.1.0" -dependencies = [ - "log", - "port_io", -] - -[[package]] -name = "pit_clock_basic" -version = "0.1.0" -dependencies = [ - "log", - "port_io", - "spin 0.9.4", -] - -[[package]] -name = "plain" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" - -[[package]] -name = "port_io" -version = "0.2.1" - -[[package]] -name = "preemption" -version = "0.1.0" -dependencies = [ - "apic", - "log", -] - -[[package]] -name = "print" -version = "0.1.0" -dependencies = [ - "dfqueue", - "event_types", - "log", - "spin 0.9.4", -] - -[[package]] -name = "proc-macro2" -version = "1.0.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "pte_flags" -version = "0.1.0" -dependencies = [ - "bitflags", - "cfg-if 1.0.0", - "static_assertions", -] - -[[package]] -name = "qp-trie" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a075ecba64154fed5429568c9c6a0e0eccc3276209e93e6133206413ea6834b4" -dependencies = [ - "new_debug_unreachable", - "unreachable", -] - -[[package]] -name = "quote" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rangemap" -version = "1.0.3" -source = "git+https://github.com/jeffparsons/rangemap#8b290d474bae680531bfba42e545e4d0121b70fd" - -[[package]] -name = "raw-cpuid" -version = "10.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6823ea29436221176fe662da99998ad3b4db2c7f31e7b6f5fe43adccd6320bb" -dependencies = [ - "bitflags", -] - -[[package]] -name = "root" -version = "0.1.0" -dependencies = [ - "fs_node", - "lazy_static", - "log", - "spin 0.9.4", -] - -[[package]] -name = "runqueue" -version = "0.1.0" -dependencies = [ - "atomic_linked_list", - "cfg-if 1.0.0", - "lazy_static", - "log", - "mutex_preemption", - "runqueue_priority", - "runqueue_realtime", - "runqueue_round_robin", - "single_simd_task_optimization", - "task", -] - -[[package]] -name = "runqueue_priority" -version = "0.1.0" -dependencies = [ - "atomic_linked_list", - "log", - "mutex_preemption", - "single_simd_task_optimization", - "task", -] - -[[package]] -name = "runqueue_realtime" -version = "0.1.0" -dependencies = [ - "atomic_linked_list", - "log", - "mutex_preemption", - "task", -] - -[[package]] -name = "runqueue_round_robin" -version = "0.1.0" -dependencies = [ - "atomic_linked_list", - "log", - "mutex_preemption", - "single_simd_task_optimization", - "task", -] - -[[package]] -name = "rustc-demangle" -version = "0.1.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" - -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver 0.9.0", -] - -[[package]] -name = "rustc_version" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" -dependencies = [ - "semver 1.0.14", -] - -[[package]] -name = "rustversion" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8" - -[[package]] -name = "scheduler" -version = "0.1.0" -dependencies = [ - "apic", - "cfg-if 1.0.0", - "irq_safety", - "log", - "preemption", - "runqueue", - "scheduler_priority", - "scheduler_realtime", - "scheduler_round_robin", - "spin 0.9.4", - "task", -] - -[[package]] -name = "scheduler_priority" -version = "0.1.0" -dependencies = [ - "log", - "runqueue", - "runqueue_priority", - "spin 0.9.4", - "task", -] - -[[package]] -name = "scheduler_realtime" -version = "0.1.0" -dependencies = [ - "log", - "runqueue_realtime", - "task", -] - -[[package]] -name = "scheduler_round_robin" -version = "0.1.0" -dependencies = [ - "log", - "runqueue", - "runqueue_round_robin", - "spin 0.9.4", - "task", -] - -[[package]] -name = "scopeguard" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" - -[[package]] -name = "scroll" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383" -dependencies = [ - "rustc_version 0.2.3", -] - -[[package]] -name = "semver" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" -dependencies = [ - "semver-parser", -] - -[[package]] -name = "semver" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4" - -[[package]] -name = "semver-parser" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" - -[[package]] -name = "serde" -version = "1.0.149" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "256b9932320c590e707b94576e3cc1f7c9024d0ee6612dfbcf1cb106cbe8e055" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.149" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4eae9b04cbffdfd550eb462ed33bc6a1b68c935127d008b27444d08380f94e4" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serial_port_basic" -version = "0.1.0" -dependencies = [ - "irq_safety", - "port_io", - "spin 0.9.4", -] - -[[package]] -name = "shapes" -version = "0.1.0" - -[[package]] -name = "single_simd_task_optimization" -version = "0.1.0" -dependencies = [ - "cfg-if 0.1.10", - "log", - "task", -] - -[[package]] -name = "sleep" -version = "0.1.0" -dependencies = [ - "irq_safety", - "lazy_static", - "scheduler", - "task", -] - -[[package]] -name = "spin" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" - -[[package]] -name = "spin" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f6002a767bff9e83f8eeecf883ecb8011875a21ae8da43bffb817a57e78cc09" -dependencies = [ - "lock_api", -] - -[[package]] -name = "stable_deref_trait" -version = "1.1.1" -source = "git+https://github.com/theseus-os/stable_deref_trait.git?branch=spin#e006c79280042e27c4f16c7d29633eb2273752ee" -dependencies = [ - "spin 0.9.4", -] - -[[package]] -name = "stack" -version = "0.1.0" -dependencies = [ - "kernel_config", - "log", - "memory", - "memory_structs", - "page_allocator", - "spin 0.9.4", -] - -[[package]] -name = "stack_trace" -version = "0.1.0" -dependencies = [ - "fallible-iterator", - "log", - "mod_mgmt", - "task", - "unwind", -] - -[[package]] -name = "stack_trace_frame_pointers" -version = "0.1.0" -dependencies = [ - "cfg-if 0.1.10", - "memory", -] - -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - -[[package]] -name = "str_ref" -version = "0.1.0" - -[[package]] -name = "syn" -version = "1.0.105" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "task" -version = "0.1.0" -dependencies = [ - "apic", - "context_switch", - "crossbeam-utils", - "environment", - "irq_safety", - "kernel_config", - "log", - "memory", - "mod_mgmt", - "no_drop", - "preemption", - "root", - "spin 0.9.4", - "stack", - "static_assertions", - "tss", - "x86_64", -] - -[[package]] -name = "tss" -version = "0.1.0" -dependencies = [ - "apic", - "atomic_linked_list", - "log", - "memory", - "spin 0.9.4", - "x86_64", -] - -[[package]] -name = "unicode-ident" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" - -[[package]] -name = "unicode-xid" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" - -[[package]] -name = "unreachable" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" -dependencies = [ - "void", -] - -[[package]] -name = "unwind" -version = "0.1.0" -dependencies = [ - "external_unwind_info", - "fallible-iterator", - "gimli", - "interrupts", - "log", - "memory", - "mod_mgmt", - "task", -] - -[[package]] -name = "util" -version = "0.1.0" - -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[package]] -name = "vfs_node" -version = "0.1.0" -dependencies = [ - "fs_node", - "log", - "memory", - "spin 0.9.4", -] - -[[package]] -name = "vga_buffer" -version = "0.1.0" -dependencies = [ - "kernel_config", - "logger", - "spin 0.9.4", - "volatile 0.2.7", -] - -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - -[[package]] -name = "volatile" -version = "0.2.7" -source = "git+https://github.com/theseus-os/volatile#73a307a2906c9f67fa4b951ce858d642c2fa669b" -dependencies = [ - "zerocopy", -] - -[[package]] -name = "volatile" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3ca98349dda8a60ae74e04fd90c7fb4d6a4fbe01e6d3be095478aa0b76f6c0c" - -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - -[[package]] -name = "x86_64" -version = "0.14.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "100555a863c0092238c2e0e814c1096c1e5cf066a309c696a87e907b5f8c5d69" -dependencies = [ - "bit_field 0.10.1", - "bitflags", - "rustversion", - "volatile 0.4.5", -] - -[[package]] -name = "xmas-elf" -version = "0.6.2" -source = "git+https://github.com/theseus-os/xmas-elf.git#635d55f6886ae3fe0ec8a78e0bcc1238224c903d" -dependencies = [ - "zero", -] - -[[package]] -name = "zero" -version = "0.1.3" -source = "git+https://github.com/theseus-os/zero.git#9fc7ff523138a21f40359b706d2d6bf91deafc62" - -[[package]] -name = "zerocopy" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e59ec1d2457bd6c0dd89b50e7d9d6b0b647809bf3f0a59ac85557046950b7b2" -dependencies = [ - "byteorder", - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6505e6815af7de1746a08f69c69606bb45695a17149517680f3b2149713b19a3" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[patch.unused]] -name = "getopts" -version = "0.2.21" -source = "git+https://github.com/theseus-os/getopts#da1e04828d3ecd6adc90e2da61e2e3cccc7ca97c" - -[[patch.unused]] -name = "smoltcp" -version = "0.6.0" -source = "git+https://github.com/m-labs/smoltcp#f25a531987c83652433e76e9dbab1b41daa98f2a" diff --git a/libtheseus/Cargo.toml b/libtheseus/Cargo.toml deleted file mode 100644 index 33e811c9ff..0000000000 --- a/libtheseus/Cargo.toml +++ /dev/null @@ -1,45 +0,0 @@ -[package] -name = "libtheseus" -version = "0.1.0" -description = "The application-facing 'library' that exposes Theseus OS features, similar to a standard library." -authors = ["Kevin Boos "] - -[dependencies] -# getopts = "0.2.21" - -[dependencies.logger] -path = "../kernel/logger" - -[dependencies.panic_entry] -path = "../kernel/panic_entry" - -[dependencies.heap] -path = "../kernel/heap" - - -# [dependencies.terminal_print] -# path = "../kernel/terminal_print" - -# [lib] -# crate-type = [ "rlib", "staticlib" ] -# crate-type = ["rlib", "staticlib", "dylib"] -# crate-type = ["staticlib"] -# crate-type = ["dylib"] -# crate-type = ["cdylib"] - -[profile.dev] -codegen-units = 1 -incremental = false - -[profile.release] -codegen-units = 1 -incremental = false - - -[patch.crates-io] -### use our own version of volatile which supports zerocopy -volatile = { git = "https://github.com/theseus-os/volatile" } -### use our own no_std-compatilbe getopts -getopts = { git = "https://github.com/theseus-os/getopts" } -### use the latest version of smoltcp from github; the one on crates.io is out of date -smoltcp = { git = "https://github.com/m-labs/smoltcp" } diff --git a/libtheseus/build.sh b/libtheseus/build.sh deleted file mode 100755 index e45b227ead..0000000000 --- a/libtheseus/build.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash -set -e - -# capture all output to a file -# script -e .script_output - -LIBTHESEUS_DIR="$(dirname "$(readlink -f "$0")")" -THESEUS_BASE_DIR=$LIBTHESEUS_DIR/.. -THESEUS_CARGO_PATH="$THESEUS_BASE_DIR/tools/theseus_cargo" - -export RUST_BACKTRACE=1 - -### Note: the "theseus_cargo" tool must be installed locally instead of invoked via `cargo run` -cargo install --force --path=$THESEUS_CARGO_PATH --root=$THESEUS_CARGO_PATH - - -### This is our new auto-config'd cargo command -$THESEUS_CARGO_PATH/bin/theseus_cargo --input ../build/deps build - - - -## The newer NEWER raw cargo command that works without xargo at all, using cargo build-std. -## We don't want to pass the build-std flags to cargo since that would re-build all of the core files from scratch. -## Instead, we want to re-use the existing pre-built core files from our previously-created sysroot output directory. -# RUST_TARGET_PATH="$THESEUS_BASE_DIR/build/deps" \ -# RUSTFLAGS="--emit=obj -C debuginfo=2 -C code-model=large -C relocation-model=static -D unused-must-use -Z merge-functions=disabled -Z share-generics=no --sysroot $THESEUS_BASE_DIR/build/deps/sysroot" \ -# cargo build --release --verbose \ -# --target x86_64-unknown-theseus - - -## The "newer" raw cargo command that works without xargo at all. This is good because we can use a pre-built/distributed sysroot directory. -# RUST_TARGET_PATH="$THESEUS_BASE_DIR/build/deps" \ -# RUSTFLAGS="--emit=obj -C debuginfo=2 -C code-model=large -C relocation-model=static -D unused-must-use -Z merge-functions=disabled -Z share-generics=no --sysroot $THESEUS_BASE_DIR/build/deps/sysroot" \ -# cargo build --release --verbose -vv \ -# --target x86_64-unknown-theseus - - -## The initial normal command that uses `xargo` -# RUST_TARGET_PATH="$THESEUS_BASE_DIR/cfg" \ -# RUSTFLAGS="--emit=obj -C debuginfo=2 -C code-model=large -C relocation-model=static -D unused-must-use -Z merge-functions=disabled -Z share-generics=no" \ -# xargo build --release --verbose -vv \ -# --target x86_64-unknown-theseus - - - -# RUST_TARGET_PATH="$THESEUS_BASE_DIR/cfg" \ -# rustc --crate-name libtheseus src/lib.rs --crate-type lib \ -# --emit=dep-info,metadata,link \ -# -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 -C metadata=43462c60d48a531a -C extra-filename=-43462c60d48a531a \ -# --out-dir $THESEUS_BASE_DIR/libtheseus/target/x86_64-unknown-theseus/release/deps \ -# --target x86_64-unknown-theseus \ -# -L dependency=$THESEUS_BASE_DIR/target/x86_64-unknown-theseus/release/deps \ -# --extern rlibc=$THESEUS_BASE_DIR/target/x86_64-unknown-theseus/release/deps/librlibc-4eb1a1ba9385f780.rmeta \ -# --extern serial_port=$THESEUS_BASE_DIR/target/x86_64-unknown-theseus/release/deps/libserial_port-ce2d7a263b9ad06d.rmeta \ -# --emit=obj -C debuginfo=2 -C code-model=large -C relocation-model=static -D unused-must-use -Z merge-functions=disabled -Z share-generics=no \ -# --sysroot /home/kevin/.xargo diff --git a/libtheseus/src/lib.rs b/libtheseus/src/lib.rs deleted file mode 100644 index e3a8297d1b..0000000000 --- a/libtheseus/src/lib.rs +++ /dev/null @@ -1,85 +0,0 @@ -//! Sample test of a dynamically-linked library atop Theseus kernel crates. - -#![no_std] -// #![feature(allocator_api)] -// #![feature(alloc_error_handler)] -// #![feature(lang_items)] -// #![feature(panic_info_message)] - -extern crate panic_entry; -extern crate heap; - -#[macro_use] extern crate alloc; -// #[macro_use] extern crate terminal_print; -extern crate logger; - -use alloc::vec::Vec; -use alloc::string::String; - -pub mod my_mod; - -pub fn main() { - libtheseus_hello(vec![String::from("hisss"), String::from("there")]); - panic!("hello from my main"); -} - - -#[inline(never)] -pub fn libtheseus_hello(_args: Vec) -> isize { - // println!("Hello from an example dylib main function!"); - logger::write_fmt(format_args!("\n\nHello from libtheseus: args: {:?}", _args)).unwrap(); - 0 -} - - - -//////////////////////////////////////////////// -////// Dummy lang items -//////////////////////////////////////////////// - -/* - -#[panic_handler] // same as: #[lang = "panic_impl"] -fn panic_entry_point(_info: &core::panic::PanicInfo) -> ! { - // println!("panic: {:?}", info); - loop { } -} - -/// This is the callback entry point that gets invoked when the heap allocator runs out of memory. -#[alloc_error_handler] -fn oom(_layout: core::alloc::Layout) -> ! { - panic!("\n(oom) Out of Heap Memory! requested allocation: {:?}", _layout); - // loop { } -} - -#[lang = "eh_personality"] -#[no_mangle] -extern "C" fn rust_eh_personality() -> ! { - // println!("BUG: Theseus does not use rust_eh_personality. Why has it been invoked?"); - loop { } -} - -#[global_allocator] -pub static GLOBAL_ALLOCATOR: DummyHeap = DummyHeap{}; - -pub struct DummyHeap; - -use alloc::alloc::{GlobalAlloc, Layout}; - -unsafe impl GlobalAlloc for DummyHeap { - unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { - core::ptr::null_mut() - } - - unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { - } - -} - - -#[no_mangle] -extern "C" fn _Unwind_Resume(_arg: usize) -> ! { - panic!("_Unwind_Resume invoked"); -} - -*/ diff --git a/libtheseus/src/my_mod.rs b/libtheseus/src/my_mod.rs deleted file mode 100644 index f039f970a9..0000000000 --- a/libtheseus/src/my_mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub fn my_mod_func() { - logger::write_fmt(format_args!("\n\nHello from my_mod_func!")).unwrap(); -} diff --git a/ports/rust b/ports/rust new file mode 160000 index 0000000000..3167b29622 --- /dev/null +++ b/ports/rust @@ -0,0 +1 @@ +Subproject commit 3167b29622044871ba674d7b84f897fa880a7538 diff --git a/shim/Cargo.lock b/shim/Cargo.lock new file mode 100644 index 0000000000..ed03d1b5dd --- /dev/null +++ b/shim/Cargo.lock @@ -0,0 +1,39 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "compiler_builtins" +version = "0.1.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3b73c3443a5fd2438d7ba4853c64e4c8efc2404a9e28a9234cc2d5eebc6c242" + +[[package]] +name = "rustc-std-workspace-alloc" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff66d57013a5686e1917ed6a025d54dd591fcda71a41fe07edf4d16726aefa86" + +[[package]] +name = "rustc-std-workspace-core" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1956f5517128a2b6f23ab2dadf1a976f4f5b27962e7724c2bf3d45e539ec098c" + +[[package]] +name = "theseus-shim" +version = "0.1.0" +dependencies = [ + "rustc-std-workspace-alloc", + "rustc-std-workspace-core", + "theseus_ffi", +] + +[[package]] +name = "theseus_ffi" +version = "0.1.0" +dependencies = [ + "compiler_builtins", + "rustc-std-workspace-alloc", + "rustc-std-workspace-core", +] diff --git a/shim/Cargo.toml b/shim/Cargo.toml new file mode 100644 index 0000000000..9b75b1de85 --- /dev/null +++ b/shim/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "theseus-shim" +version = "0.1.0" +edition = "2021" + +[dependencies] +alloc = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-alloc' } +compiler_builtins = { version = '0.1.103', optional = true } +core = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-core' } +theseus_ffi = { path = "../libs/theseus_ffi" } + +[features] +rustc-dep-of-std = ['alloc', 'compiler_builtins', 'core', 'theseus_ffi/rustc-dep-of-std'] diff --git a/shim/src/lib.rs b/shim/src/lib.rs new file mode 100644 index 0000000000..f3c58fde9d --- /dev/null +++ b/shim/src/lib.rs @@ -0,0 +1,203 @@ +#![no_std] +#![feature(extern_types)] + +#[cfg(not(feature = "rustc-dep-of-std"))] +extern crate alloc; + +use alloc::string::String; + +pub use theseus_ffi::Error; +use theseus_ffi::{FatPointer, FfiString}; + +type Result = core::result::Result; + +mod c { + use theseus_ffi::{ + Error, FatPointer, FfiOption, FfiResult, FfiSlice, FfiSliceMut, FfiStr, FfiString, + }; + + #[link(name = "libtheseus")] + extern "C" { + #[link_name = "libtheseus::next_u64"] + pub(crate) fn next_u64() -> u64; + + #[link_name = "libtheseus::getcwd"] + pub(crate) fn getcwd() -> FfiString; + + #[link_name = "libtheseus::chdir"] + pub(crate) fn chdir(path: FfiStr<'_>) -> FfiResult<(), Error>; + + #[link_name = "libtheseus::getenv"] + pub(crate) fn getenv(key: FfiStr<'_>) -> FfiOption; + + #[link_name = "libtheseus::setenv"] + pub(crate) fn setenv(key: FfiStr<'_>, value: FfiStr<'_>) -> FfiResult<(), Error>; + + #[link_name = "libtheseus::unsetenv"] + pub(crate) fn unsetenv(key: FfiStr<'_>) -> FfiResult<(), Error>; + + #[link_name = "libtheseus::exit"] + pub(crate) fn exit(code: i32) -> !; + + #[link_name = "libtheseus::getpid"] + pub(crate) fn getpid() -> u32; + + #[link_name = "libtheseus::register_dtor"] + pub(crate) fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)); + + #[link_name = "libtheseus::stdin"] + pub(crate) fn stdin() -> FfiResult; + + #[link_name = "libtheseus::stdout"] + pub(crate) fn stdout() -> FfiResult; + + #[link_name = "libtheseus::stderr"] + pub(crate) fn stderr() -> FfiResult; + + #[link_name = "libtheseus::read"] + pub(crate) fn read(reader: FatPointer, buf: FfiSliceMut<'_, u8>) + -> FfiResult; + + #[link_name = "libtheseus::write"] + pub(crate) fn write(writer: FatPointer, buf: FfiSlice<'_, u8>) -> FfiResult; + + #[link_name = "libtheseus::flush"] + pub(crate) fn flush(writer: FatPointer) -> FfiResult<(), Error>; + + #[link_name = "libtheseus::drop_reader"] + pub(crate) fn drop_reader(reader: FatPointer); + + #[link_name = "libtheseus::drop_writer"] + pub(crate) fn drop_writer(writer: FatPointer); + } + + const _: theseus_ffi::next_u64 = next_u64; + const _: theseus_ffi::getcwd = getcwd; + const _: theseus_ffi::chdir = chdir; + const _: theseus_ffi::getenv = getenv; + const _: theseus_ffi::setenv = setenv; + const _: theseus_ffi::unsetenv = unsetenv; + const _: theseus_ffi::exit = exit; + const _: theseus_ffi::getpid = getpid; + const _: theseus_ffi::register_dtor = register_dtor; + const _: theseus_ffi::stdin = stdin; + const _: theseus_ffi::stdout = stdout; + const _: theseus_ffi::stderr = stderr; + const _: theseus_ffi::read = read; + const _: theseus_ffi::write = write; + const _: theseus_ffi::flush = flush; + const _: theseus_ffi::drop_reader = drop_reader; + const _: theseus_ffi::drop_writer = drop_writer; +} + +#[inline] +pub fn next_u64() -> u64 { + unsafe { c::next_u64() } +} + +#[inline] +pub fn getcwd() -> String { + unsafe { c::getcwd() }.into() +} + +#[inline] +pub fn chdir(path: &str) -> Result<()> { + let path = path.into(); + unsafe { c::chdir(path) }.into() +} + +#[inline] +pub fn getenv(key: &str) -> Option { + let key = key.into(); + Into::>::into(unsafe { c::getenv(key) }).map(|s| s.into()) +} + +#[inline] +pub fn setenv(key: &str, value: &str) -> Result<()> { + let key = key.into(); + let value = value.into(); + unsafe { c::setenv(key, value) }.into() +} + +#[inline] +pub fn unsetenv(key: &str) -> Result<()> { + let key = key.into(); + unsafe { c::unsetenv(key) }.into() +} + +#[inline] +pub fn exit(code: i32) -> ! { + unsafe { c::exit(code) } +} + +#[inline] +pub fn getpid() -> u32 { + unsafe { c::getpid() } +} + +#[inline] +pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { + c::register_dtor(t, dtor) +} + +// TODO: impl Send + Sync for Reader and Writer? + +pub struct Reader { + inner: FatPointer, +} + +impl Drop for Reader { + #[inline] + fn drop(&mut self) { + unsafe { c::drop_reader(self.inner.clone()) } + } +} + +pub struct Writer { + inner: FatPointer, +} + +impl Drop for Writer { + #[inline] + fn drop(&mut self) { + unsafe { c::drop_writer(self.inner.clone()) } + } +} + +#[inline] +pub fn stdin() -> Result { + Ok(Reader { + inner: Result::from(unsafe { c::stdin() })?, + }) +} + +#[inline] +pub fn stdout() -> Result { + Ok(Writer { + inner: Result::from(unsafe { c::stdout() })?, + }) +} + +#[inline] +pub fn stderr() -> Result { + Ok(Writer { + inner: Result::from(unsafe { c::stderr() })?, + }) +} + +// TODO: Mutable reference? + +#[inline] +pub fn read(reader: &mut Reader, buf: &mut [u8]) -> Result { + unsafe { c::read(reader.inner.clone(), buf.into()) }.into() +} + +#[inline] +pub fn write(writer: &mut Writer, buf: &[u8]) -> Result { + unsafe { c::write(writer.inner.clone(), buf.into()) }.into() +} + +#[inline] +pub fn flush(writer: &mut Writer) -> Result<()> { + unsafe { c::flush(writer.inner.clone()) }.into() +} diff --git a/theseus_features/Cargo.toml b/theseus_features/Cargo.toml index 47bebb30aa..bfe02ddd63 100644 --- a/theseus_features/Cargo.toml +++ b/theseus_features/Cargo.toml @@ -64,6 +64,7 @@ test_panic = { path = "../applications/test_panic", optional = true } test_preemption_counter = { path = "../applications/test_preemption_counter", optional = true } test_restartable = { path = "../applications/test_restartable", optional = true } test_scheduler = { path = "../applications/test_scheduler", optional = true } +test_std = { path = "../applications/test_std", optional = true } test_std_fs = { path = "../applications/test_std_fs", optional = true } test_sync_block = { path = "../applications/test_sync_block", optional = true } test_task_cancel = { path = "../applications/test_task_cancel", optional = true } @@ -161,6 +162,7 @@ theseus_tests = [ "test_preemption_counter", "test_restartable", "test_scheduler", + "test_std", "test_std_fs", "test_sync_block", "test_task_cancel", From c0c8eb1bb9e7a6f81b85432cc1d26367752aa6ab Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Mon, 6 Nov 2023 23:15:40 +1100 Subject: [PATCH 02/11] Ignore `.cargo` Signed-off-by: Klimenty Tsoutsman --- .cargo/config.toml | 2 -- .gitignore | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 3e180cd255..0000000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -rustc = "/Users/klim/Projects/theseus-5/ports/rust/build/aarch64-apple-darwin/stage1/bin/rustc" diff --git a/.gitignore b/.gitignore index b070989ee3..1e6773a7e3 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,6 @@ github_pages/doc/ # library lock files /libs/**/Cargo.lock + +# TODO: Remove once std is upstreamed +.cargo/ From 13ff8f746e70f2c8985a3d09e794e8ec955742cc Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Mon, 6 Nov 2023 23:16:31 +1100 Subject: [PATCH 03/11] Add dependency on `std` target for `docs` Signed-off-by: Klimenty Tsoutsman --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5046961570..c4a17491e5 100644 --- a/Makefile +++ b/Makefile @@ -666,7 +666,7 @@ RUSTDOC_OUT_FILE := $(RUSTDOC_OUT)/___Theseus_Crates___/index.html ## Builds Theseus's source-level documentation for all Rust crates except applications. ## The entire project is built as normal using the `cargo doc` command (`rustdoc` under the hood). -docs: doc +docs: doc std doc : export override RUSTDOCFLAGS += -A rustdoc::private_intra_doc_links doc : export override RUSTFLAGS= doc : export override CARGOFLAGS= From 0149e3c6ea59276f7701e5f3ba7038390991c195 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Mon, 6 Nov 2023 23:42:03 +1100 Subject: [PATCH 04/11] Docs Signed-off-by: Klimenty Tsoutsman --- Makefile | 2 +- kernel/libtheseus/Cargo.toml | 2 ++ kernel/libtheseus/src/lib.rs | 29 +++++++++++++++++++++++------ libs/theseus_ffi/Cargo.toml | 2 ++ libs/theseus_ffi/src/lib.rs | 2 ++ shim/Cargo.toml | 2 ++ shim/src/lib.rs | 12 +++++++++++- 7 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index c4a17491e5..fc10f7cb78 100644 --- a/Makefile +++ b/Makefile @@ -69,7 +69,7 @@ THESEUS_CARGO := $(ROOT_DIR)/tools/theseus_cargo THESEUS_CARGO_BIN := $(THESEUS_CARGO)/bin/theseus_cargo EXTRA_FILES := $(ROOT_DIR)/extra_files LIMINE_DIR := $(ROOT_DIR)/limine-prebuilt -RUST_SOURCE := $(ROOT_DIR)/ports/rust +RUST_SOURCE := $(ROOT_DIR)/ports/rust ### Set up tool names/locations for cross-compiling on a Mac OS / macOS host (Darwin). diff --git a/kernel/libtheseus/Cargo.toml b/kernel/libtheseus/Cargo.toml index dd27c35e45..c9d313ec3c 100644 --- a/kernel/libtheseus/Cargo.toml +++ b/kernel/libtheseus/Cargo.toml @@ -1,6 +1,8 @@ [package] name = "libtheseus" version = "0.1.0" +authors = ["Klim Tsoutsman "] +description = "Implementation of shim functions for std linkage boundary" edition = "2021" [dependencies] diff --git a/kernel/libtheseus/src/lib.rs b/kernel/libtheseus/src/lib.rs index e5338118b5..6f61fcc6ba 100644 --- a/kernel/libtheseus/src/lib.rs +++ b/kernel/libtheseus/src/lib.rs @@ -1,3 +1,7 @@ +//! This crate implements the functions defined in `shim`. +//! +//! See the `shim` crate for more details. + #![no_std] extern crate alloc; @@ -7,8 +11,10 @@ use core::mem; use app_io::{ImmutableRead, ImmutableWrite}; use path::Path; -use theseus_ffi::{Error, FatPointer, FfiOption, FfiResult, FfiSlice, FfiSliceMut, FfiStr, FfiString}; use task::{KillReason, TaskRef}; +use theseus_ffi::{ + Error, FatPointer, FfiOption, FfiResult, FfiSlice, FfiSliceMut, FfiStr, FfiString, +}; const _: theseus_ffi::next_u64 = next_u64; const _: theseus_ffi::getcwd = getcwd; @@ -32,7 +38,7 @@ fn current_task() -> TaskRef { task::get_my_current_task().expect("failed to get current task") } -// Add the libtheseus:: so mod_mgmt knows which crate to search. +// Add the libtheseus:: prefix so mod_mgmt knows which crate to search. #[export_name = "libtheseus::next_u64"] pub extern "C" fn next_u64() -> u64 { @@ -104,8 +110,13 @@ pub unsafe extern "C" fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*m unsafe { thread_local_macro::register_dtor(t, dtor) } } -// TODO: Something better than transmutations. -// TODO: Explain why we shouldn't bother using stabby at least for trait objects. +// TODO: Something better than transmutations? + +// One might naively assume that we are better off using `stabby` trait objects, +// but that means throughtout Theseus we would have to use +// stabby::alloc::sync::Arc and vtable!(Trait), rather than alloc::sync::Arc and +// dyn Trait respectively. The current solution isn't great, but it's only funky +// at the boundary to `shim` and doesn't impact the rest of Theseus. #[export_name = "libtheseus::stdin"] pub extern "C" fn stdin() -> FfiResult { @@ -132,14 +143,20 @@ pub extern "C" fn stderr() -> FfiResult { } #[export_name = "libtheseus::read"] -pub unsafe extern "C" fn read(reader: FatPointer, buf: FfiSliceMut<'_, u8>) -> FfiResult { +pub unsafe extern "C" fn read( + reader: FatPointer, + buf: FfiSliceMut<'_, u8>, +) -> FfiResult { let ptr: *const dyn ImmutableRead = unsafe { mem::transmute(reader) }; let r = unsafe { &*ptr }; FfiResult::from(r.read(buf.into()).map_err(from_core2)) } #[export_name = "libtheseus::write"] -pub unsafe extern "C" fn write(writer: FatPointer, buf: FfiSlice<'_, u8>) -> FfiResult { +pub unsafe extern "C" fn write( + writer: FatPointer, + buf: FfiSlice<'_, u8>, +) -> FfiResult { let ptr: *const dyn ImmutableWrite = unsafe { mem::transmute(writer) }; let r = unsafe { &*ptr }; FfiResult::from(r.write(buf.into()).map_err(from_core2)) diff --git a/libs/theseus_ffi/Cargo.toml b/libs/theseus_ffi/Cargo.toml index 2d70a7a481..bc7dc00405 100644 --- a/libs/theseus_ffi/Cargo.toml +++ b/libs/theseus_ffi/Cargo.toml @@ -1,6 +1,8 @@ [package] name = "theseus_ffi" version = "0.1.0" +authors = ["Klim Tsoutsman "] +description = "Common types for the std linkage boundary" edition = "2021" [dependencies] diff --git a/libs/theseus_ffi/src/lib.rs b/libs/theseus_ffi/src/lib.rs index 6074053306..585132c150 100644 --- a/libs/theseus_ffi/src/lib.rs +++ b/libs/theseus_ffi/src/lib.rs @@ -1,3 +1,5 @@ +//! This crate defines types that are used in both `shim` and `libtheseus`. + #![no_std] #![feature(vec_into_raw_parts, try_trait_v2, never_type, exhaustive_patterns)] #![allow(non_camel_case_types)] diff --git a/shim/Cargo.toml b/shim/Cargo.toml index 9b75b1de85..3671ec66da 100644 --- a/shim/Cargo.toml +++ b/shim/Cargo.toml @@ -1,6 +1,8 @@ [package] name = "theseus-shim" version = "0.1.0" +authors = ["Klim Tsoutsman "] +description = "Declaration of shim functions for std linkage boundary" edition = "2021" [dependencies] diff --git a/shim/src/lib.rs b/shim/src/lib.rs index f3c58fde9d..150191e9f3 100644 --- a/shim/src/lib.rs +++ b/shim/src/lib.rs @@ -1,3 +1,10 @@ +//! Provides Theseus OS functionality without a direct dependency on Theseus +//! kernel crates. +//! +//! It does so by declaring functions that are implemented by the `libtheseus` +//! kernel crate. These functions are referenced as relocations in the dependent +//! (i.e. `std`) object file that `mod_mgmt` then fills in at runtime. + #![no_std] #![feature(extern_types)] @@ -185,7 +192,10 @@ pub fn stderr() -> Result { }) } -// TODO: Mutable reference? +// TODO: Should we be taking a mutable or immutable reference? +// +// Technically, speaking I don't think we need to take a mutable reference, but +// that limits our options in the future. #[inline] pub fn read(reader: &mut Reader, buf: &mut [u8]) -> Result { From df8910309b74c8edc2a0de9557b2aa6d838757b4 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Wed, 15 Nov 2023 13:16:29 +1100 Subject: [PATCH 05/11] Make rust build optional Signed-off-by: Klimenty Tsoutsman --- .gitmodules | 3 --- Cargo.toml | 1 + Makefile | 24 ++++++++++++++++++++---- applications/test_std/Cargo.lock | 7 +++++++ kernel/libtheseus/src/lib.rs | 1 + kernel/panic_entry/src/lib.rs | 3 ++- kernel/thread_local_macro/src/lib.rs | 7 ++++++- theseus_features/Cargo.toml | 6 +++++- 8 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 applications/test_std/Cargo.lock diff --git a/.gitmodules b/.gitmodules index 71729230d5..fa662cb6f2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -22,6 +22,3 @@ path = libs/core2 url = https://github.com/theseus-os/core2.git shallow = true -[submodule "ports/rust"] - path = ports/rust - url = https://github.com/theseus-os/rust.git diff --git a/Cargo.toml b/Cargo.toml index 18a7cbac36..77b501385d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ default-members = [ exclude = [ "shim", + "applications/test_std", ## Exclude the build directories "build", diff --git a/Makefile b/Makefile index fc10f7cb78..6694ca356b 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,11 @@ debug ?= none net ?= none merge_sections ?= yes bootloader ?= grub +std ?= no + +ifeq ($(std),yes) + export override FEATURES+=--features std +endif ## aarch64 only supports booting via UEFI ifeq ($(ARCH),aarch64) @@ -315,7 +320,16 @@ endif ### end of "build" target ### ############################# -std: +## This target invokes the actual Rust build process via `cargo`. +cargo: +ifeq ($(std),yes) + if [ ! -d $(RUST_SOURCE) ] ; then \ + git clone https://github.com/theseus-os/rust.git --branch theseus-std-5 $(RUST_SOURCE); \ + git -c $(RUST_SOURCE) sumbodule update --init; \ + fi + + echo "yo" + ## Cache std/Cargo.toml cp $(RUST_SOURCE)/library/std/Cargo.toml $(ROOT_DIR)/std-cargo.toml ## Remove the last line of std/Cargo.toml @@ -339,9 +353,10 @@ std: @mkdir -p $(ROOT_DIR)/.cargo @echo "[build]" > $(ROOT_DIR)/.cargo/config.toml @echo "rustc = \"$(RUST_SOURCE)/build/$(HOST_TRIPLE)/stage1/bin/rustc\"" >> $(ROOT_DIR)/.cargo/config.toml +else + @rm -f $(ROOT_DIR)/.cargo/config.toml +endif -## This target invokes the actual Rust build process via `cargo`. -cargo: std @mkdir -p $(BUILD_DIR) @mkdir -p $(NANO_CORE_BUILD_DIR) @mkdir -p $(OBJECT_FILES_BUILD_DIR) @@ -652,7 +667,8 @@ else ifeq ($(ARCH),aarch64) clippy : export override FEATURES := $(subst --workspace,,$(FEATURES)) endif clippy : export override RUSTFLAGS = $(patsubst %,--cfg %, $(THESEUS_CONFIG)) -clippy: std +clippy: + @rm -f $(ROOT_DIR)/.cargo/config.toml RUST_TARGET_PATH='$(CFG_DIR)' RUSTFLAGS='$(RUSTFLAGS)' \ cargo clippy \ $(BUILD_STD_CARGOFLAGS) $(FEATURES) \ diff --git a/applications/test_std/Cargo.lock b/applications/test_std/Cargo.lock new file mode 100644 index 0000000000..c98bcfb6a8 --- /dev/null +++ b/applications/test_std/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "test_std" +version = "0.1.0" diff --git a/kernel/libtheseus/src/lib.rs b/kernel/libtheseus/src/lib.rs index 6f61fcc6ba..e0959da902 100644 --- a/kernel/libtheseus/src/lib.rs +++ b/kernel/libtheseus/src/lib.rs @@ -3,6 +3,7 @@ //! See the `shim` crate for more details. #![no_std] +#![allow(clippy::missing_safety_doc)] extern crate alloc; diff --git a/kernel/panic_entry/src/lib.rs b/kernel/panic_entry/src/lib.rs index 977dcb9fb4..eceb1d3555 100644 --- a/kernel/panic_entry/src/lib.rs +++ b/kernel/panic_entry/src/lib.rs @@ -136,12 +136,13 @@ fn oom(_layout: core::alloc::Layout) -> ! { // TODO: Are these truly unreachable? #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn __rust_start_panic() { unreachable!(); } #[no_mangle] -#[allow(improper_ctypes_definitions)] +#[allow(clippy::missing_safety_doc, improper_ctypes_definitions)] pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn core::any::Any + Send + 'static) { unreachable!(); } diff --git a/kernel/thread_local_macro/src/lib.rs b/kernel/thread_local_macro/src/lib.rs index 76ab879a0d..232a9f3afe 100644 --- a/kernel/thread_local_macro/src/lib.rs +++ b/kernel/thread_local_macro/src/lib.rs @@ -84,13 +84,18 @@ pub fn take_current_tls_destructors() -> Vec { /// TLS destructors that should be run when that task exits. /// /// # Arguments -/// * `a`: the pointer to the object that will be destructed. +/// +/// * `object_ptr`: the pointer to the object that will be destructed. /// * `dtor`: the function that should be invoked to destruct the object pointed to by `a`. /// When the current task exits, this function will be invoked with `a` /// as its only argument, at which point the `dtor` function should drop `a`. /// /// Currently the only value of `dtor` that is used is a type-specific monomorphized /// version of the above [`fast::destroy_value()`] function. +/// +/// # Safety +/// +/// `object_ptr` must point to a valid object, and `dtor` must be the destructor for that object. pub unsafe fn register_dtor(object_ptr: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { TLS_DESTRUCTORS.borrow_mut().push(TlsObjectDestructor { object_ptr, dtor }); } diff --git a/theseus_features/Cargo.toml b/theseus_features/Cargo.toml index bfe02ddd63..e4182d5755 100644 --- a/theseus_features/Cargo.toml +++ b/theseus_features/Cargo.toml @@ -102,6 +102,7 @@ everything = [ "theseus_std", "theseus_tests", "wasmtime", + # The `std` feature is intentionally omitted as it is still in development. ] ## Includes `wasmtime`, the WebAssembly (WASM) runtime, in the build. @@ -162,7 +163,6 @@ theseus_tests = [ "test_preemption_counter", "test_restartable", "test_scheduler", - "test_std", "test_std_fs", "test_sync_block", "test_task_cancel", @@ -171,3 +171,7 @@ theseus_tests = [ "test_wasmtime", "unwind_test", ] + +std = [ + "test_std", +] From 06ed1fd8fd87381285056a28818358c1065f38ae Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Wed, 15 Nov 2023 13:19:28 +1100 Subject: [PATCH 06/11] Add specific commit to rust "submodule" Signed-off-by: Klimenty Tsoutsman --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6694ca356b..c503d10f06 100644 --- a/Makefile +++ b/Makefile @@ -325,11 +325,10 @@ cargo: ifeq ($(std),yes) if [ ! -d $(RUST_SOURCE) ] ; then \ git clone https://github.com/theseus-os/rust.git --branch theseus-std-5 $(RUST_SOURCE); \ + git -c $(RUST_SOURCE) checkout 3167b29; \ git -c $(RUST_SOURCE) sumbodule update --init; \ fi - echo "yo" - ## Cache std/Cargo.toml cp $(RUST_SOURCE)/library/std/Cargo.toml $(ROOT_DIR)/std-cargo.toml ## Remove the last line of std/Cargo.toml From e3c5f2b069ffd6381bac33047a248815baa8cdfe Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Wed, 15 Nov 2023 13:20:14 +1100 Subject: [PATCH 07/11] Properly delete rust submodule Signed-off-by: Klimenty Tsoutsman --- ports/rust | 1 - 1 file changed, 1 deletion(-) delete mode 160000 ports/rust diff --git a/ports/rust b/ports/rust deleted file mode 160000 index 3167b29622..0000000000 --- a/ports/rust +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3167b29622044871ba674d7b84f897fa880a7538 From 55b1f96d901eb485c9864ce4a4a7ef95dd94fd50 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Wed, 15 Nov 2023 15:10:12 +1100 Subject: [PATCH 08/11] Final Signed-off-by: Klimenty Tsoutsman --- .gitignore | 1 + Makefile | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 1e6773a7e3..fe0d555879 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ github_pages/doc/ # TODO: Remove once std is upstreamed .cargo/ +/ports/rust diff --git a/Makefile b/Makefile index c503d10f06..1cb0c738a3 100644 --- a/Makefile +++ b/Makefile @@ -323,12 +323,15 @@ endif ## This target invokes the actual Rust build process via `cargo`. cargo: ifeq ($(std),yes) - if [ ! -d $(RUST_SOURCE) ] ; then \ - git clone https://github.com/theseus-os/rust.git --branch theseus-std-5 $(RUST_SOURCE); \ - git -c $(RUST_SOURCE) checkout 3167b29; \ - git -c $(RUST_SOURCE) sumbodule update --init; \ +## We need enough history to include one upstream commit, so that bootstrap can find a commit hash +## for downloading LLVM. + @if [ ! -d $(RUST_SOURCE) ] ; then \ + git clone https://github.com/theseus-os/rust.git --branch theseus-std-5 $(RUST_SOURCE) --depth 50; \ fi + git -C $(RUST_SOURCE) checkout 3167b29 + git -C $(RUST_SOURCE) submodule update --init --depth 1 + ## Cache std/Cargo.toml cp $(RUST_SOURCE)/library/std/Cargo.toml $(ROOT_DIR)/std-cargo.toml ## Remove the last line of std/Cargo.toml From 56af34aa84200b15492a789b496962852a8ca687 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Tue, 21 Nov 2023 03:39:50 +1100 Subject: [PATCH 09/11] Updates Signed-off-by: Klimenty Tsoutsman --- .gitignore | 2 +- .ignore | 2 +- Cargo.toml | 1 + Makefile | 9 ++++++--- cfg/Config.mk | 1 - shim/src/lib.rs | 2 -- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 56bbf03d25..d3a8735668 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,4 @@ github_pages/doc/ # TODO: Remove once std is upstreamed .cargo/ -/ports/rust +/rust diff --git a/.ignore b/.ignore index c5b4d1d786..871732e64f 100644 --- a/.ignore +++ b/.ignore @@ -1 +1 @@ -ports/rust +rust diff --git a/Cargo.toml b/Cargo.toml index 77b501385d..6a4cfd6425 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ default-members = [ exclude = [ "shim", "applications/test_std", + "rust", ## Exclude the build directories "build", diff --git a/Makefile b/Makefile index 1cb0c738a3..73ea27e883 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,9 @@ std ?= no ifeq ($(std),yes) export override FEATURES+=--features std + export override BUILD_STD_CARGOFLAGS += -Zbuild-std +else + export override BUILD_STD_CARGOFLAGS += -Zbuild-std=core,alloc endif ## aarch64 only supports booting via UEFI @@ -74,7 +77,7 @@ THESEUS_CARGO := $(ROOT_DIR)/tools/theseus_cargo THESEUS_CARGO_BIN := $(THESEUS_CARGO)/bin/theseus_cargo EXTRA_FILES := $(ROOT_DIR)/extra_files LIMINE_DIR := $(ROOT_DIR)/limine-prebuilt -RUST_SOURCE := $(ROOT_DIR)/ports/rust +RUST_SOURCE := $(ROOT_DIR)/rust ### Set up tool names/locations for cross-compiling on a Mac OS / macOS host (Darwin). @@ -329,7 +332,7 @@ ifeq ($(std),yes) git clone https://github.com/theseus-os/rust.git --branch theseus-std-5 $(RUST_SOURCE) --depth 50; \ fi - git -C $(RUST_SOURCE) checkout 3167b29 + git -C $(RUST_SOURCE) checkout 2a8008fe9b git -C $(RUST_SOURCE) submodule update --init --depth 1 ## Cache std/Cargo.toml @@ -339,7 +342,7 @@ ifeq ($(std),yes) @mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml ## Add the correct dependency path, since the dependency path in std/Cargo.toml assumes that std is ## being built using -Zbuild-std. - @echo "theseus-shim = { path = \"../../../../shim\", features = ['rustc-dep-of-std'] }" >> $(RUST_SOURCE)/library/std/Cargo.toml + @echo "theseus-shim = { path = \"../../../shim\", features = ['rustc-dep-of-std'] }" >> $(RUST_SOURCE)/library/std/Cargo.toml ## Build the compiler @cd $(RUST_SOURCE) && RUSTFLAGS="" CARGOFLAGS="" ./x.py build library --stage 1 diff --git a/cfg/Config.mk b/cfg/Config.mk index eed72c3723..187cde408a 100644 --- a/cfg/Config.mk +++ b/cfg/Config.mk @@ -70,7 +70,6 @@ endif ## Also ensure that core memory functions (e.g., memcpy) are included in the build and not name-mangled. ## We keep these flags separate from the regular CARGOFLAGS for purposes of easily creating a sysroot directory. BUILD_STD_CARGOFLAGS += -Z unstable-options -BUILD_STD_CARGOFLAGS += -Z build-std BUILD_STD_CARGOFLAGS += -Z build-std-features=compiler-builtins-mem diff --git a/shim/src/lib.rs b/shim/src/lib.rs index 150191e9f3..4c4ae79c62 100644 --- a/shim/src/lib.rs +++ b/shim/src/lib.rs @@ -147,8 +147,6 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { c::register_dtor(t, dtor) } -// TODO: impl Send + Sync for Reader and Writer? - pub struct Reader { inner: FatPointer, } From 0e0c8b4f441d61c4728dca8d09df6f7e3b1d0e82 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Tue, 21 Nov 2023 03:42:18 +1100 Subject: [PATCH 10/11] Update comment Signed-off-by: Klimenty Tsoutsman --- shim/src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/shim/src/lib.rs b/shim/src/lib.rs index 4c4ae79c62..48838fddf4 100644 --- a/shim/src/lib.rs +++ b/shim/src/lib.rs @@ -190,10 +190,7 @@ pub fn stderr() -> Result { }) } -// TODO: Should we be taking a mutable or immutable reference? -// -// Technically, speaking I don't think we need to take a mutable reference, but -// that limits our options in the future. +// NOTE: The references below don't need to be mutable. #[inline] pub fn read(reader: &mut Reader, buf: &mut [u8]) -> Result { From 1e62cc15ab48366aff3b5a3f7120cc23b04ecad4 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Tue, 21 Nov 2023 03:42:46 +1100 Subject: [PATCH 11/11] Update comment 2 Signed-off-by: Klimenty Tsoutsman --- shim/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shim/src/lib.rs b/shim/src/lib.rs index 48838fddf4..9f66859b1e 100644 --- a/shim/src/lib.rs +++ b/shim/src/lib.rs @@ -190,7 +190,7 @@ pub fn stderr() -> Result { }) } -// NOTE: The references below don't need to be mutable. +// NOTE: The references to self below don't need to be mutable. #[inline] pub fn read(reader: &mut Reader, buf: &mut [u8]) -> Result {