Skip to main content

KernelFunc

Trait KernelFunc 

pub trait KernelFunc {
    // Required methods
    fn virt_to_phys(addr: usize) -> usize;
    fn phys_to_virt(addr: usize) -> usize;
    fn dma_alloc_coherent(pages: usize) -> (usize, usize);
    fn dma_free_coherent(vaddr: usize, pages: usize);
    fn dma_request_irq(irq: usize, handler: fn());
}
Available on crate feature fxmac only.
Expand description

Kernel function interface required by the FXMAC Ethernet driver.

This trait defines the platform-specific functions that must be implemented by the host system to support the FXMAC driver. These functions handle address translation, DMA memory management, and interrupt registration.

§Implementation Requirements

All implementations must be #[crate_interface::impl_interface] compatible and provide thread-safe operations where applicable.

§Example

pub struct MyPlatform;

#[crate_interface::impl_interface]
impl fxmac_rs::KernelFunc for MyPlatform {
    fn virt_to_phys(addr: usize) -> usize {
        // Platform-specific virtual to physical address translation
        addr - KERNEL_OFFSET
    }

    fn phys_to_virt(addr: usize) -> usize {
        // Platform-specific physical to virtual address translation
        addr + KERNEL_OFFSET
    }

    fn dma_alloc_coherent(pages: usize) -> (usize, usize) {
        // Allocate DMA-capable coherent memory
        // Returns (virtual_address, physical_address)
        allocator.alloc_dma(pages)
    }

    fn dma_free_coherent(vaddr: usize, pages: usize) {
        // Free previously allocated DMA memory
        allocator.free_dma(vaddr, pages)
    }

    fn dma_request_irq(irq: usize, handler: fn()) {
        // Register interrupt handler for the specified IRQ
        interrupt_controller.register(irq, handler)
    }
}

Required Methods§

fn virt_to_phys(addr: usize) -> usize

Converts a virtual address to its corresponding physical address.

This function is used by the driver to obtain physical addresses for DMA operations, as the hardware requires physical addresses for buffer descriptors.

§Arguments
  • addr - The virtual address to convert.
§Returns

The corresponding physical address.

fn phys_to_virt(addr: usize) -> usize

Converts a physical address to its corresponding virtual address.

This function is used by the driver to access hardware registers and DMA buffers through virtual addresses.

§Arguments
  • addr - The physical address to convert.
§Returns

The corresponding virtual address.

fn dma_alloc_coherent(pages: usize) -> (usize, usize)

Allocates DMA-coherent memory pages.

Allocates physically contiguous memory that is suitable for DMA operations. The memory should be cache-coherent or properly managed for DMA access.

§Arguments
  • pages - The number of pages (typically 4KB each) to allocate.
§Returns

A tuple containing (virtual_address, physical_address) of the allocated memory region.

fn dma_free_coherent(vaddr: usize, pages: usize)

Frees previously allocated DMA-coherent memory.

§Arguments
  • vaddr - The virtual address of the memory region to free.
  • pages - The number of pages to free.

fn dma_request_irq(irq: usize, handler: fn())

Registers an interrupt handler for DMA/network interrupts.

This function should configure the interrupt controller to route the specified IRQ to the provided handler function.

§Arguments
  • irq - The IRQ number to register.
  • handler - The interrupt handler function to call when the IRQ fires.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§