Skip to content

Interface for OS- or CPU-specific implementations #1420

@qinsoon

Description

@qinsoon

We currently only have util::memory that is designed to be a module that abstracts out the memory-related system calls. Based on the observation of the changes in #1418, clearly we have other parts that is OS dependent. We probably want a more proper OS interface.

We could use traits to require each OS to implement certain functions and constants, similar to VMBinding. However, we don't need it as a type argument. Instead, we can just statically include one of such implementations based on the host. Elsewhere In the code base, we use the specific implementation and traits.

For example

trait OperatingSystem {
  type Memory;
  type Process;
}

#[cfg(target_os = "linux")]
mod linux {
  struct Linux;
  impl OperatingSystem for Linux {
    ...
  }
}
#[cfg(target_os = "linux")]
pub use linux::Linux as OS;

Elsewhere in the code

// Hopefully Rust allows us to do the following rather than `<OS::Memory as Memory>::mmap_fixed()`
OS::Memory::mmap_fixed(...);
OS::Process::get_total_num_cpus();

In addition to operating systems, there are parts of mmtk-core that are specific to the CPU, or the combination of CPU and OS.

Similar to the OS, we can have modules specific to the CPU or CPU+OS.

mod cpu {
    trait CPU {
        // insert CPU-specific properties or methods here
    }
    #[cfg(target_arch = "x86_64")]
    mod x86_64 {
        struct X86_64;
        impl CPU for X86_64 { ... }
        ...
    }
    #[cfg(target_arch = "aarch64")]
    mod aarch64 {
        ...
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions