Skip to content

refine: Refine code#116

Merged
junyu0312 merged 3 commits intomainfrom
refine
Apr 2, 2026
Merged

refine: Refine code#116
junyu0312 merged 3 commits intomainfrom
refine

Conversation

@junyu0312
Copy link
Copy Markdown
Owner

@junyu0312 junyu0312 commented Apr 2, 2026

Summary by CodeRabbit

  • Refactor
    • Virtualization layer reorganized; VM trait and memory-permission flags relocated to a dedicated module
    • Introduced a VM-exit handler abstraction and wired it into vCPU creation
    • vCPU manager now captures thread handles for spawned vCPU threads
    • Device manager exposes PIO/MMIO managers; device I/O APIs made shared-access and backed by internal mutexes
    • UART implementation switched to an internal mutex-guarded state
  • Chores
    • Removed an unused architecture constant

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 2, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Reworks VM and VM-exit abstractions: moves the Vm trait and flags into virt::vm, replaces DeviceVmExitHandler with a unified VmExit/VmExitHandler flow, updates VCPU APIs and wiring to accept a shared VM-exit handler, and adjusts many device PIO/MMIO APIs to use shared references with interior mutability.

Changes

Cohort / File(s) Summary
Virt module / Vm trait
crates/vm-core/src/virt.rs, crates/vm-core/src/virt/vm.rs
Extracted Vm trait and SetUserMemoryRegionFlags into new virt/vm.rs; virt.rs now declares pub mod vm; and imports the relocated Vm.
VmExit abstraction & Vcpu surface
crates/vm-core/src/vcpu/vm_exit.rs, crates/vm-core/src/vcpu/vcpu.rs, crates/vm-core/src/vcpu/error.rs, crates/vm-core/src/vcpu/vcpu_manager.rs, crates/vm-core/src/arch/aarch64/vm_exit.rs
Renamed DeviceVmExitHandlerVmExit/VmExitHandlerError; replaced per-vCPU device exit fields with vm_exit_handler: Arc<dyn VmExit>; updated VcpuManager APIs and handle_vm_exit signature to accept a borrowed &dyn VmExit; adjusted Vcpu errors to convert from VmExitHandlerError.
DeviceManager & VmExitHandler wiring
crates/vm-core/src/device_manager/manager.rs, crates/vm-core/src/device_manager.rs, crates/vm-machine/src/vm/vm_exit_handler.rs, crates/vm-machine/src/vm.rs, crates/vm-machine/src/vmm.rs
Removed DeviceVmExitHandler impl from DeviceManager and made internal managers public; added vm_exit_handler module in vm-machine that implements VmExit by delegating to DeviceManager (PIO/MMIO lookup, bounds checks) and optional PSCI; VM construction now creates and passes a shared VmExitHandler.
Pio/MMIO APIs: mutability → interior mutability
crates/vm-core/src/device/pio/pio_as_manager.rs, crates/vm-core/src/device/pio/pio_device.rs, crates/vm-pci/src/root_complex/pio.rs, crates/vm-device/src/device/*, crates/vm-device/src/device/uart8250.rs
Changed many PIO/MMIO trait method receivers from &mut self to &self and wrapped internal mutable state with Mutex where needed (e.g., Uart8250, PciRootComplexPio), updating implementations to lock before mutating.
Vcpu import path updates
crates/vm-core/src/virt/kvm/vcpu/arch/aarch64.rs, crates/vm-core/src/virt/kvm/vcpu/arch/x86_64.rs
Updated use paths to reference crate::virt::vcpu::Vcpu instead of the old location.
Arch / cleanup
crates/vm-core/src/arch/x86_64.rs
Removed an exported constant BASE_ADDRESS.

Sequence Diagram(s)

sequenceDiagram
  participant Vcpu as Vcpu
  participant VmExitHandler as VmExitHandler
  participant DeviceMgr as DeviceManager
  participant Pio as PIO
  participant Mmio as MMIO
  participant Psci as PSCI

  Vcpu->>VmExitHandler: on VM exit (reason, vcpu state)
  alt IO port access
    VmExitHandler->>DeviceMgr: pio lookup (port)
    DeviceMgr->>Pio: call io_in/io_out (with mutex inside)
    Pio-->>VmExitHandler: result / error
  else MMIO access
    VmExitHandler->>DeviceMgr: mmio lookup (addr)
    DeviceMgr->>Mmio: mmio_read/mmio_write (bounds checked)
    Mmio-->>VmExitHandler: result / error
  else SMC (aarch64)
    VmExitHandler->>Psci: call_smc(vcpu)
    Psci-->>VmExitHandler: result / error
  end
  VmExitHandler-->>Vcpu: apply results / resume or propagate error
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I hopped through code with nimble paws,

Moved traits and handlers without a pause.
VmExit listens, devices reply,
Mutexes hum as bytes flutter by.
A tiny rabbit cheers—refactor, hi-five!

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 2.04% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title "Refine code" is vague and generic, using non-descriptive language that fails to convey the specific nature of the substantial architectural changes made in this pull request. Provide a more specific title that captures the main architectural change, such as "Refactor VM-exit handler abstraction to centralize device management" or "Extract Vm trait to separate module and refactor VmExit interface."
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refine

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
crates/vm-core/src/virt.rs (1)

1-17: ⚠️ Potential issue | 🔴 Critical

Missing re-exports break downstream imports.

The module declares pub mod vm; but doesn't re-export Vm or SetUserMemoryRegionFlags. This breaks hvp.rs which imports use crate::virt::Vm; (line 27 per the relevant code snippet).

Add re-exports for API convenience and to fix the broken import:

🔧 Proposed fix
 use std::sync::Arc;

 use crate::error::Error;
 use crate::virt::vm::Vm;

 #[cfg(feature = "kvm")]
 pub mod kvm;

 #[cfg(feature = "hvp")]
 pub mod hvp;

 pub mod vcpu;
 pub mod vm;

+pub use vm::SetUserMemoryRegionFlags;
+pub use vm::Vm;
+
 pub trait Virt {
     fn create_vm(&self) -> Result<Arc<dyn Vm>, Error>;
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm-core/src/virt.rs` around lines 1 - 17, The downstream import fails
because virt.rs exposes pub mod vm but does not re-export the Vm type or the
SetUserMemoryRegionFlags symbol; update virt.rs to add public re-exports for
these API symbols (e.g., add pub use for Vm and SetUserMemoryRegionFlags) so
files like hvp.rs can import use crate::virt::Vm; and use
crate::virt::SetUserMemoryRegionFlags; without breaking.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/vm-core/src/virt/hvp.rs`:
- Around line 26-31: The import for Vm is broken because virt does not re-export
Vm; update the use statement to import the concrete module path (use
crate::virt::vm::Vm) instead of use crate::virt::Vm so references to Vm in this
file (and similarly in crates/vm-core/src/virt/kvm.rs where Vm is imported the
same way) resolve correctly; modify the import near the top of hvp.rs
referencing symbols Virt, Vm, HvpGicV3, and HvpVcpu to use crate::virt::vm::Vm,
and make the same change in kvm.rs to fix the compilation error.

---

Outside diff comments:
In `@crates/vm-core/src/virt.rs`:
- Around line 1-17: The downstream import fails because virt.rs exposes pub mod
vm but does not re-export the Vm type or the SetUserMemoryRegionFlags symbol;
update virt.rs to add public re-exports for these API symbols (e.g., add pub use
for Vm and SetUserMemoryRegionFlags) so files like hvp.rs can import use
crate::virt::Vm; and use crate::virt::SetUserMemoryRegionFlags; without
breaking.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bee93dfa-1197-4e5d-baed-8f8749dc444e

📥 Commits

Reviewing files that changed from the base of the PR and between 7bf0fc8 and acfd551.

📒 Files selected for processing (9)
  • crates/vm-core/src/arch/x86_64.rs
  • crates/vm-core/src/vcpu/vcpu_manager.rs
  • crates/vm-core/src/virt.rs
  • crates/vm-core/src/virt/hvp.rs
  • crates/vm-core/src/virt/kvm/vcpu/arch/aarch64.rs
  • crates/vm-core/src/virt/kvm/vcpu/arch/x86_64.rs
  • crates/vm-core/src/virt/vm.rs
  • crates/vm-machine/src/vm.rs
  • crates/vm-machine/src/vmm.rs
💤 Files with no reviewable changes (1)
  • crates/vm-core/src/arch/x86_64.rs

Comment on lines 26 to +31
use crate::virt::Virt;
use crate::virt::Vm;
use crate::virt::hvp::irq_chip::HvpGicV3;
use crate::virt::hvp::vcpu::HvpVcpu;
use crate::virt::vcpu::Vcpu;
use crate::virt::vm::SetUserMemoryRegionFlags;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify if there's a re-export of Vm in virt.rs
rg -n 'pub use.*Vm' crates/vm-core/src/virt.rs

# Check all files importing crate::virt::Vm (without ::vm::)
rg -n 'use crate::virt::Vm;' crates/

Repository: junyu0312/rust-vm

Length of output: 170


🏁 Script executed:

cat -n crates/vm-core/src/virt.rs

Repository: junyu0312/rust-vm

Length of output: 428


🏁 Script executed:

rg -n 'pub use' crates/vm-core/src/virt.rs

Repository: junyu0312/rust-vm

Length of output: 43


🏁 Script executed:

# Check if there's a Cargo.lock or build artifacts
ls -la crates/vm-core/ | grep -E '(Cargo|target)'

# Also verify the exact structure - maybe there's a re-export I missed
cat -n crates/vm-core/src/virt/vm.rs | head -50

Repository: junyu0312/rust-vm

Length of output: 845


Broken import: crate::virt::Vm is not re-exported from the virt module.

Line 27 imports use crate::virt::Vm;, but virt.rs only contains a private use crate::virt::vm::Vm; without a public re-export. This will fail to compile.

Update the import to use the full path:

Proposed fix
 use crate::virt::Virt;
-use crate::virt::Vm;
+use crate::virt::vm::Vm;
 use crate::virt::hvp::irq_chip::HvpGicV3;

Note: The same issue exists in crates/vm-core/src/virt/kvm.rs line 10, which also imports use crate::virt::Vm;.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
use crate::virt::Virt;
use crate::virt::Vm;
use crate::virt::hvp::irq_chip::HvpGicV3;
use crate::virt::hvp::vcpu::HvpVcpu;
use crate::virt::vcpu::Vcpu;
use crate::virt::vm::SetUserMemoryRegionFlags;
use crate::virt::Virt;
use crate::virt::vm::Vm;
use crate::virt::hvp::irq_chip::HvpGicV3;
use crate::virt::hvp::vcpu::HvpVcpu;
use crate::virt::vcpu::Vcpu;
use crate::virt::vm::SetUserMemoryRegionFlags;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm-core/src/virt/hvp.rs` around lines 26 - 31, The import for Vm is
broken because virt does not re-export Vm; update the use statement to import
the concrete module path (use crate::virt::vm::Vm) instead of use
crate::virt::Vm so references to Vm in this file (and similarly in
crates/vm-core/src/virt/kvm.rs where Vm is imported the same way) resolve
correctly; modify the import near the top of hvp.rs referencing symbols Virt,
Vm, HvpGicV3, and HvpVcpu to use crate::virt::vm::Vm, and make the same change
in kvm.rs to fix the compilation error.

@junyu0312 junyu0312 merged commit 9dd0640 into main Apr 2, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant