/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2015 Mihai Carabas <[email protected]>4* Copyright (c) 2024 Ruslan Bukin <[email protected]>5*6* This software was developed by the University of Cambridge Computer7* Laboratory (Department of Computer Science and Technology) under Innovate8* UK project 105694, "Digital Security by Design (DSbD) Technology Platform9* Prototype".10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19*20* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#ifndef _VMM_RISCV_H_34#define _VMM_RISCV_H_3536#include <machine/reg.h>37#include <machine/pcpu.h>38#include <machine/vmm.h>3940#include <riscv/vmm/vmm_vtimer.h>4142struct hypregs {43uint64_t hyp_ra;44uint64_t hyp_sp;45uint64_t hyp_gp;46uint64_t hyp_tp;47uint64_t hyp_t[7];48uint64_t hyp_s[12];49uint64_t hyp_a[8];50uint64_t hyp_sepc;51uint64_t hyp_sstatus;52uint64_t hyp_hstatus;53};5455struct hypcsr {56uint64_t hvip;57uint64_t vsstatus;58uint64_t vsie;59uint64_t vstvec;60uint64_t vsscratch;61uint64_t vsepc;62uint64_t vscause;63uint64_t vstval;64uint64_t vsatp;65uint64_t scounteren;66uint64_t senvcfg;67};6869enum vmm_fence_type {70VMM_RISCV_FENCE_INVALID = 0,71VMM_RISCV_FENCE_I,72VMM_RISCV_FENCE_VMA,73VMM_RISCV_FENCE_VMA_ASID,74};7576struct vmm_fence {77enum vmm_fence_type type;78size_t start;79size_t size;80uint64_t asid;81};8283struct hypctx {84struct hypregs host_regs;85struct hypregs guest_regs;86struct hypcsr guest_csrs;87uint64_t host_sscratch;88uint64_t host_stvec;89uint64_t host_scounteren;90uint64_t guest_scounteren;91struct hyp *hyp;92struct vcpu *vcpu;93bool has_exception;94int cpu_id;95int ipi_pending;96int interrupts_pending;97struct vtimer vtimer;9899struct vmm_fence *fence_queue;100struct mtx fence_queue_mtx;101int fence_queue_head;102int fence_queue_tail;103#define FENCE_REQ_I (1 << 0)104#define FENCE_REQ_VMA (1 << 1)105int fence_req;106};107108struct hyp {109struct vm *vm;110uint64_t vmid_generation;111bool aplic_attached;112struct aplic *aplic;113struct hypctx *ctx[];114};115116struct hyptrap {117uint64_t sepc;118uint64_t scause;119uint64_t stval;120uint64_t htval;121uint64_t htinst;122};123124#define dprintf(fmt, ...)125126struct hypctx *riscv_get_active_vcpu(void);127void vmm_switch(struct hypctx *);128void vmm_unpriv_trap(struct hyptrap *, uint64_t tmp);129bool vmm_sbi_ecall(struct vcpu *);130131void riscv_send_ipi(struct hyp *hyp, cpuset_t *cpus);132int riscv_check_ipi(struct hypctx *hypctx, bool clear);133bool riscv_check_interrupts_pending(struct hypctx *hypctx);134135#endif /* !_VMM_RISCV_H_ */136137138