// Copyright 2023 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34use anyhow::anyhow;5use base::Result;6use downcast_rs::impl_downcast;7use serde::Deserialize;8use serde::Serialize;9use vm_memory::GuestAddress;1011use crate::Hypervisor;12use crate::IrqRoute;13use crate::IrqSource;14use crate::IrqSourceChip;15use crate::Vcpu;16use crate::Vm;1718/// A wrapper for using a VM on riscv64 and getting/setting its state.19pub trait VmRiscv64: Vm {20/// Gets the `Hypervisor` that created this VM.21fn get_hypervisor(&self) -> &dyn Hypervisor;2223/// Create a Vcpu with the specified Vcpu ID.24fn create_vcpu(&self, id: usize) -> Result<Box<dyn VcpuRiscv64>>;25}2627/// A wrapper around creating and using a VCPU on riscv64.28pub trait VcpuRiscv64: Vcpu {29/// Sets the value of a register on this VCPU.30fn set_one_reg(&self, reg_id: VcpuRegister, data: u64) -> Result<()>;3132/// Gets the value of a register on this VCPU.33fn get_one_reg(&self, reg_id: VcpuRegister) -> Result<u64>;3435/// Snapshot VCPU36fn snapshot(&self) -> anyhow::Result<VcpuSnapshot> {37Err(anyhow!("not yet implemented"))38}3940/// Restore VCPU41fn restore(&self, _snapshot: &VcpuSnapshot) -> anyhow::Result<()> {42Err(anyhow!("not yet implemented"))43}44}4546/// Riscv64 specific vCPU snapshot.47///48/// Not implemented yet.49#[derive(Clone, Debug, Serialize, Deserialize)]50pub struct VcpuSnapshot {51pub vcpu_id: usize,52}5354impl_downcast!(VcpuRiscv64);5556/// Initial state for Riscv64 VCPUs.57#[derive(Clone)]58pub struct VcpuInitRiscv64 {59/// The address of the FDT60pub fdt_address: GuestAddress,61}6263impl VcpuInitRiscv64 {64pub fn new(fdt_address: GuestAddress) -> Self {65Self { fdt_address }66}67}6869/// Hold the CPU feature configurations that are needed to setup a vCPU.70#[derive(Clone, Debug, PartialEq, Eq)]71pub struct CpuConfigRiscv64 {72/// The address of the FDT73pub fdt_address: GuestAddress,74}7576impl CpuConfigRiscv64 {77pub fn new(fdt_address: GuestAddress) -> Self {78Self { fdt_address }79}80}8182/// Config registers exposed by kvm.83#[repr(u64)]84#[derive(Copy, Clone)]85pub enum ConfigRegister {86Isa = 0,87}8889/// Timer registers exposed by kvm.90#[repr(u64)]91#[derive(Copy, Clone)]92pub enum TimerRegister {93TimebaseFrequency = 0,94}9596/// Core registers exposed by kvm.97#[repr(u64)]98#[derive(Copy, Clone)]99pub enum CoreRegister {100Pc = 0x00, // Program counter101Ra = 0x01, // Return address102Sp = 0x02, // Stack pointer103Gp = 0x03, // Global pointer104Tp = 0x04, // Task pointer105T0 = 0x05, // Caller saved register 0106T1 = 0x06, // Caller saved register 1107T2 = 0x07, // Caller saved register 2108S0 = 0x08, // Callee saved register 0109S1 = 0x09, // Callee saved register 1110A0 = 0x0a, // Function argument (or return value) 0111A1 = 0x0b, // Function argument (or return value) 1112A2 = 0x0c, // Function argument 2113A3 = 0x0d, // Function argument 3114A4 = 0x0e, // Function argument 4115A5 = 0x0f, // Function argument 5116A6 = 0x10, // Function argument 6117A7 = 0x11, // Function argument 7118S2 = 0x12, // Callee saved register 2119S3 = 0x13, // Callee saved register 3120S4 = 0x14, // Callee saved register 4121S5 = 0x15, // Callee saved register 5122S6 = 0x16, // Callee saved register 6123S7 = 0x17, // Callee saved register 7124S8 = 0x18, // Callee saved register 8125S9 = 0x19, // Callee saved register 9126S10 = 0x1a, // Callee saved register 10127S11 = 0x1b, // Callee saved register 11128T3 = 0x1c, // Caller saved register 3129T4 = 0x1d, // Caller saved register 4130T5 = 0x1e, // Caller saved register 5131T6 = 0x1f, // Caller saved register 6132Mode = 0x20, // Privilege mode (1 = S-mode or 0 = U-mode)133}134135/// Registers exposed through `KVM_[GET|SET]_ONE_REG` API.136#[derive(Copy, Clone)]137pub enum VcpuRegister {138Config(ConfigRegister),139Core(CoreRegister),140Timer(TimerRegister),141}142143// Convenience constructors for IrqRoutes144impl IrqRoute {145pub fn aia_irq_route(irq_num: u32) -> IrqRoute {146IrqRoute {147gsi: irq_num,148source: IrqSource::Irqchip {149chip: IrqSourceChip::Aia,150pin: irq_num,151},152}153}154}155156157