-
Notifications
You must be signed in to change notification settings - Fork 14
feat: enhance project documentation and add unit tests #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,167 @@ | ||
| # AxVCpu | ||
|
|
||
| Virtual CPU interface and wrapper for [`ArceOS`](https://github.com/arceos-org/arceos). | ||
| [](https://github.com/arceos-hypervisor/x86_vcpu/actions/workflows/ci.yml) | ||
|
|
||
| AxVCpu is a virtual CPU abstraction library for ArceOS hypervisors, providing a unified, architecture-independent interface for managing virtual CPUs in hypervisor environments. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Architecture Agnostic**: Unified interface supporting multiple architectures (x86_64, ARM64, RISC-V) | ||
| - **State Management**: Robust VCpu lifecycle management with clear state transitions | ||
| - **Per-CPU Virtualization**: Efficient per-CPU state management and resource isolation | ||
| - **Hardware Abstraction**: Clean separation between architecture-specific and common operations | ||
| - **CPU Affinity**: Support for CPU binding and affinity management | ||
| - **Exit Handling**: Comprehensive VM exit reason handling and processing | ||
|
|
||
| ## Architecture | ||
|
|
||
| AxVCpu follows a layered architecture design: | ||
|
|
||
| ``` | ||
| ┌─────────────────────────────────────────┐ | ||
| │ Application Layer │ ← Hypervisor/VMM | ||
| ├─────────────────────────────────────────┤ | ||
| │ AxVCpu Core Interface │ ← Main API | ||
| ├─────────────────────────────────────────┤ | ||
| │ Architecture Abstraction │ ← AxArchVCpu trait | ||
| ├─────────────────────────────────────────┤ | ||
| │ Hardware Abstraction Layer │ ← AxVCpuHal trait | ||
| ├─────────────────────────────────────────┤ | ||
| │ Architecture-Specific Backends │ ← x86_64, ARM64, etc. | ||
| └─────────────────────────────────────────┘ | ||
| ``` | ||
|
|
||
| ## Core Components | ||
|
|
||
| ### VCpu State Machine | ||
|
|
||
| ``` | ||
| Created → Free → Ready → Running → Blocked | ||
| ↓ ↓ ↓ ↓ ↓ | ||
| └───────┴──────┴────────┴────────┘ | ||
| Invalid | ||
| ``` | ||
|
|
||
| - **Created**: Initial state after VCpu creation | ||
| - **Free**: Initialized and ready to be bound to a physical CPU | ||
| - **Ready**: Bound to a physical CPU and ready for execution | ||
| - **Running**: Currently executing on a physical CPU | ||
| - **Blocked**: Execution blocked (waiting for I/O, etc.) | ||
| - **Invalid**: Error state when transitions fail | ||
|
|
||
| ### Key Traits | ||
|
|
||
| - `AxArchVCpu`: Architecture-specific VCpu implementation interface | ||
| - `AxVCpuHal`: Hardware abstraction layer for hypervisor operations | ||
| - `AxVCpuExitReason`: VM exit reason enumeration and handling | ||
|
|
||
| ## Quick Start | ||
|
|
||
| Add AxVCpu to your `Cargo.toml`: | ||
|
|
||
| ```toml | ||
| [dependencies] | ||
| axvcpu = "0.1.0" | ||
| ``` | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| ```rust | ||
| use axvcpu::{AxVCpu, VCpuState}; | ||
|
|
||
| // Mock implementation for example | ||
| struct MyArchVCpu; | ||
| impl AxArchVCpu for MyArchVCpu { | ||
| // Implement required methods... | ||
| } | ||
|
|
||
| // Create a new virtual CPU | ||
| let vcpu = AxVCpu::<MyArchVCpu>::new( | ||
| vm_id, // VM identifier | ||
| vcpu_id, // VCpu identifier | ||
| favor_cpu, // Preferred physical CPU | ||
| cpu_set, // CPU affinity mask | ||
| config // Architecture-specific config | ||
| )?; | ||
|
|
||
| // Check VCpu state | ||
| assert_eq!(vcpu.state(), VCpuState::Created); | ||
|
|
||
| // Setup the VCpu | ||
| vcpu.setup(entry_addr, ept_root, setup_config)?; | ||
|
|
||
| // Bind to current physical CPU and run | ||
| vcpu.bind()?; | ||
| let exit_reason = vcpu.run()?; | ||
|
|
||
| // Handle VM exit | ||
| match exit_reason { | ||
| AxVCpuExitReason::Halt => { | ||
| println!("Guest halted"); | ||
| }, | ||
| AxVCpuExitReason::Io { port, is_write, .. } => { | ||
| println!("I/O access on port {}", port); | ||
| }, | ||
| // ... handle other exit reasons | ||
| } | ||
| ``` | ||
|
|
||
| ## Architecture Implementation | ||
|
|
||
| To implement AxVCpu for a new architecture: | ||
|
|
||
| ```rust | ||
| use axvcpu::AxArchVCpu; | ||
|
|
||
| struct MyArchVCpu { | ||
| // Architecture-specific fields | ||
| } | ||
|
|
||
| impl AxArchVCpu for MyArchVCpu { | ||
| type CreateConfig = MyCreateConfig; | ||
| type SetupConfig = MySetupConfig; | ||
|
|
||
| fn new(vm_id: VMId, vcpu_id: VCpuId, config: Self::CreateConfig) -> AxResult<Self> { | ||
| // Initialize architecture-specific VCpu | ||
| Ok(Self { /* ... */ }) | ||
| } | ||
|
|
||
| fn set_entry(&mut self, entry: GuestPhysAddr) -> AxResult { | ||
| // Set guest entry point | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn set_ept_root(&mut self, ept_root: HostPhysAddr) -> AxResult { | ||
| // Configure memory virtualization | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn setup(&mut self, config: Self::SetupConfig) -> AxResult { | ||
| // Complete VCpu initialization | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn run(&mut self) -> AxResult<AxVCpuExitReason> { | ||
| // Execute guest code until VM exit | ||
| Ok(AxVCpuExitReason::Halt) | ||
| } | ||
|
|
||
| // Implement other required methods... | ||
| } | ||
| ``` | ||
|
|
||
| ## Related Projects | ||
|
|
||
| - [ArceOS](https://github.com/arceos-org/arceos) - A component-based OS kernel | ||
| - [AxVisor](https://github.com/arceos-hypervisor/axvisor) - A hypervisor implemented based on the ArceOS unikernel framework. | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under multiple licenses. You may choose to use this project under any of the following licenses: | ||
|
|
||
| - **[GPL-3.0-or-later](LICENSE.GPLv3)** - GNU General Public License v3.0 or later | ||
| - **[Apache-2.0](LICENSE.Apache2)** - Apache License 2.0 | ||
| - **[MulanPubL-2.0](LICENSE.MulanPubL2)** - Mulan Public License 2.0 | ||
| - **[MulanPSL2](LICENSE.MulanPSL2)** - Mulan Permissive Software License v2 | ||
|
|
||
| You may use this software under the terms of any of these licenses at your option. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.