axvm/hal.rs
1// Copyright 2025 The Axvisor Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use axaddrspace::{HostPhysAddr, HostVirtAddr};
16use axerrno::AxResult;
17
18/// The interfaces which the underlying software (kernel or hypervisor) must implement.
19pub trait AxVMHal: Sized {
20 /// The low-level **OS-dependent** helpers that must be provided for physical address management.
21 type PagingHandler: page_table_multiarch::PagingHandler;
22
23 /// Converts a virtual address to the corresponding physical address.
24 fn virt_to_phys(vaddr: HostVirtAddr) -> HostPhysAddr;
25
26 /// Current time in nanoseconds.
27 fn current_time_nanos() -> u64;
28
29 /// Current VM ID.
30 fn current_vm_id() -> usize;
31
32 /// Current Virtual CPU ID.
33 fn current_vcpu_id() -> usize;
34
35 /// Current Physical CPU ID.
36 fn current_pcpu_id() -> usize;
37
38 /// Get the Physical CPU ID where the specified VCPU of the current VM resides.
39 ///
40 /// Returns an error if the VCPU is not found.
41 fn vcpu_resides_on(vm_id: usize, vcpu_id: usize) -> AxResult<usize>;
42
43 /// Inject an IRQ to the specified VCPU.
44 ///
45 /// This method should find the physical CPU where the specified VCPU resides and inject the IRQ
46 /// to it on that physical CPU with [`axvcpu::AxVCpu::inject_interrupt`].
47 ///
48 /// Returns an error if the VCPU is not found.
49 fn inject_irq_to_vcpu(vm_id: usize, vcpu_id: usize, irq: usize) -> AxResult;
50}