mycelium

repository·main·Indexed 20 days ago

https://github.com/hawkw/mycelium

An experimental microkernel-based operating system written in Rust using WebAssembly and WASI. The project includes several supporting libraries: mycelium-bitfield for structured bitfields and integer range packing; Cordyceps for intrusive data structures like doubly-linked lists and lock-free MPSC queues; and maitake-sync, a collection of asynchronous synchronization primitives (Mutex, RwLock, Semaphore) designed for #![no_std] environments.

Tokens
81.9K
Snippets
308
Records
385
Agent score
69%

What's inside mycelium

  1. What is maitake-sync?

    main
    maitake-sync is a collection of asynchronous synchronization primitives for Rust, built on core::task and core::future. It is designed primarily for #![no_std] environments, such as bare-metal operating systems, embedded systems, and OS components. It provides tools to coordinate tasks by allowing them to yield to a runtime scheduler while waiting for resources.
  2. Overview of mycelium-util

    main

    mycelium-util is a "standard library" crate containing general-purpose utility types and traits designed specifically for the Mycelium operating system and its related libraries.

    Note for non-Mycelium users: This crate is primarily intended for use within the Mycelium ecosystem. It may contain Mycelium-specific design decisions or quirks. Because it is tightly coupled to the OS development, breaking changes may occur frequently, and older major versions are generally not supported.

  3. Overview of Mycelium

    main
    Mycelium is a microkernel-ish operating system designed to run on commodity desktop hardware. It executes WebAssembly modules in both kernel- and user-space and uses the WebAssembly System Interface (WASI) as its syscall layer. Currently, the project is in early development and primarily demonstrates execution of a single WebAssembly 'hello world' module.
  4. What is maitake?

    main

    maitake is a modular async runtime construction kit for Rust, specifically designed for #![no_std] and bare-metal projects (like operating systems or embedded systems).

    Unlike complete runtimes such as tokio or async-std, maitake does not provide a single, monolithic runtime. Instead, it offers reusable, low-level components that you can combine to build a custom, application-specific async runtime. This allows for direct control over implementation details, such as how tasks are stored in memory.

  5. What is mycelium-bitfield?

    main

    mycelium-bitfield is a library for defining structured bitfields in Rust. It provides two main ways to work with bitfields:

    1. The bitfield! macro: A declarative macro that automatically generates bitfield types.
    2. The pack module: A set of low-level packing spec types used to define ranges that can be packed and unpacked from an integer value. This allows you to hand-write bitfield logic if the macro-generated code does not meet your needs.

    The library is modular; the bitfield! macro is built using the components found in the pack module.

  6. Overview of mycelium-pci

    main
    mycelium-pci is an abstraction layer designed for interacting with devices on PCI and PCI Express buses. It is specifically implemented for the [Mycelium] operating system and provides a cross-platform (ish) interface for bus interaction.
  7. Overview of maitake API components

    main

    The maitake library is organized into several major functional modules:

    • maitake::task: The task system. Provides Task (an async task/future), TaskRef (a reference-counted, type-erased pointer to a task), JoinHandle (to await task output), and task::Builder (for task configuration).
    • maitake::scheduler: Task execution. Provides Scheduler and StaticScheduler types to drive tasks forward.
    • maitake::time: Time management. Provides Sleep (wait for duration) and Timeout (wrap a future with a deadline) futures. It uses a hierarchical Timer wheel that must be driven by a hardware time source (e.g., interrupts or timestamp counters).
    • maitake::sync: Asynchronous synchronization. Provides Mutex, RwLock, and Semaphore, along with lower-level primitives for custom strategies.
    • maitake::future: General-purpose utility Future types that work without the Rust standard library.
  8. Configure task storage for no-heap environments

    main
    In bare-metal systems where liballoc or a heap allocator is unavailable, maitake allows you to avoid dynamic allocation by overriding the memory container where tasks are stored. You can use the Storage trait to define how tasks are allocated, enabling you to create and schedule a fixed set of tasks stored in 'static memory (compile-time allocation).
  9. Override blocking mutex implementations

    main

    Many async primitives in maitake-sync (like Mutex, RwLock, Semaphore, and WaitQueue) internally use maitake_sync::blocking::Mutex for wait-list synchronization. By default, this uses blocking::DefaultMutex.

    If you need to provide a custom mutex implementation (e.g., a spinlock or a hardware-specific mutex), maitake_sync::blocking::Mutex and the async primitives are generic over a Lock type parameter. You can implement the RawMutex or ScopedRawMutex traits from the mutex-traits crate to allow your custom implementation to be used throughout the library. This also enables compatibility with lock_api and critical-section adapters via the mutex crate.

  10. Compare mycelium-bitfield with bitflags and modular-bitfield

    main

    When choosing a bitfield library, consider these differences:

    vs bitflags

    • bitflags: Best for simple, single-bit boolean flags. It cannot define multi-bit structured ranges.
    • mycelium-bitfield: Supports both single-bit flags and multi-bit structured ranges. It can perform most tasks bitflags can do, but with more advanced range capabilities.

    vs modular-bitfield

    • Macro Type: modular-bitfield uses a procedural macro attribute, whereas mycelium-bitfield uses a declarative macro. Choose mycelium-bitfield if you need to avoid procedural macros.
    • Low-level API: mycelium-bitfield provides the pack module, allowing you to build bitfield types manually. modular-bitfield only provides the procedural macro.
    • Validation: modular-bitfield provides compile-time validation to ensure typed values fit within their bitfield ranges. mycelium-bitfield relies on the correctness of the FromBits trait implementation for user-provided types.
  11. What are intrusive data structures in Cordyceps?

    main

    Cordyceps provides node-based data structures where the node data (pointers to other nodes and associated metadata) is stored within the values themselves, rather than the collection owning the values.

    Key Benefits

    • Zero additional allocation: If an element is already heap-allocated, it can be added to a collection without a new allocation. If elements are at fixed memory locations (like statics or pages in a page allocator), they can be added without any allocation at all.
    • Performance: Avoids allocator overhead common in standard node-based structures.
    • Use cases: Ideal for code that cannot allocate, such as implementing a heap allocator using intrusive lists of memory regions.

    Trade-offs and Requirements

    • Awareness: The stored struct must be aware of the collection. It must include a Links struct as a field and implement the Linked trait.
    • Exclusivity: A single instance of a Linked type cannot be added to multiple intrusive data structures of the same type simultaneously (though it can be part of multiple structures of different types).
    • Safety: Implementing Linked is unsafe. Members of intrusive collections must be pinned in memory; they cannot move or be dropped while they are linked into a collection.