/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (C) 2015 Mihai Carabas <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27#ifndef _VMM_ARM64_H_28#define _VMM_ARM64_H_2930#include <machine/reg.h>31#include <machine/hypervisor.h>32#include <machine/pcpu.h>3334#include "mmu.h"35#include "io/vgic_v3.h"36#include "io/vtimer.h"3738struct vgic_v3;39struct vgic_v3_cpu;4041/*42* Per-vCPU hypervisor state.43*/44struct hypctx {45struct trapframe tf;4647/*48* EL1 control registers.49*/50uint64_t elr_el1; /* Exception Link Register */51uint64_t sp_el0; /* Stack pointer */52uint64_t tpidr_el0; /* EL0 Software ID Register */53uint64_t tpidrro_el0; /* Read-only Thread ID Register */54uint64_t tpidr_el1; /* EL1 Software ID Register */55uint64_t vbar_el1; /* Vector Base Address Register */5657uint64_t actlr_el1; /* Auxiliary Control Register */58uint64_t afsr0_el1; /* Auxiliary Fault Status Register 0 */59uint64_t afsr1_el1; /* Auxiliary Fault Status Register 1 */60uint64_t amair_el1; /* Auxiliary Memory Attribute Indirection Register */61uint64_t contextidr_el1; /* Current Process Identifier */62uint64_t cpacr_el1; /* Architectural Feature Access Control Register */63uint64_t csselr_el1; /* Cache Size Selection Register */64uint64_t esr_el1; /* Exception Syndrome Register */65uint64_t far_el1; /* Fault Address Register */66uint64_t mair_el1; /* Memory Attribute Indirection Register */67uint64_t mdccint_el1; /* Monitor DCC Interrupt Enable Register */68uint64_t mdscr_el1; /* Monitor Debug System Control Register */69uint64_t par_el1; /* Physical Address Register */70uint64_t sctlr_el1; /* System Control Register */71uint64_t tcr_el1; /* Translation Control Register */72uint64_t tcr2_el1; /* Translation Control Register 2 */73uint64_t ttbr0_el1; /* Translation Table Base Register 0 */74uint64_t ttbr1_el1; /* Translation Table Base Register 1 */75uint64_t spsr_el1; /* Saved Program Status Register */7677uint64_t pmcr_el0; /* Performance Monitors Control Register */78uint64_t pmccntr_el0;79uint64_t pmccfiltr_el0;80uint64_t pmuserenr_el0;81uint64_t pmselr_el0;82uint64_t pmxevcntr_el0;83uint64_t pmcntenset_el0;84uint64_t pmintenset_el1;85uint64_t pmovsset_el0;86uint64_t pmevcntr_el0[31];87uint64_t pmevtyper_el0[31];8889uint64_t dbgclaimset_el1;90uint64_t dbgbcr_el1[16]; /* Debug Breakpoint Control Registers */91uint64_t dbgbvr_el1[16]; /* Debug Breakpoint Value Registers */92uint64_t dbgwcr_el1[16]; /* Debug Watchpoint Control Registers */93uint64_t dbgwvr_el1[16]; /* Debug Watchpoint Value Registers */9495/* EL2 control registers */96uint64_t cptr_el2; /* Architectural Feature Trap Register */97uint64_t hcr_el2; /* Hypervisor Configuration Register */98uint64_t hcrx_el2; /* Extended Hypervisor Configuration Register */99uint64_t mdcr_el2; /* Monitor Debug Configuration Register */100uint64_t vpidr_el2; /* Virtualization Processor ID Register */101uint64_t vmpidr_el2; /* Virtualization Multiprocessor ID Register */102uint64_t el2_addr; /* The address of this in el2 space */103struct hyp *hyp;104struct vcpu *vcpu;105struct {106uint64_t far_el2; /* Fault Address Register */107uint64_t hpfar_el2; /* Hypervisor IPA Fault Address Register */108} exit_info;109110struct vtimer_cpu vtimer_cpu;111112uint64_t setcaps; /* Currently enabled capabilities. */113114/* vCPU state used to handle guest debugging. */115uint64_t debug_spsr; /* Saved guest SPSR */116uint64_t debug_mdscr; /* Saved guest MDSCR */117118struct vgic_v3_regs vgic_v3_regs;119struct vgic_v3_cpu *vgic_cpu;120bool has_exception;121};122123struct hyp {124struct vm *vm;125struct vtimer vtimer;126uint64_t vmid_generation;127uint64_t vttbr_el2;128uint64_t el2_addr; /* The address of this in el2 space */129uint64_t feats; /* Which features are enabled */130#define HYP_FEAT_HCX (0x1ul << 0)131#define HYP_FEAT_ECV_POFF (0x1ul << 1)132bool vgic_attached;133struct vgic_v3 *vgic;134struct hypctx *ctx[];135};136137#define DEFINE_VMMOPS_IFUNC(ret_type, opname, args) \138ret_type vmmops_##opname args;139140DEFINE_VMMOPS_IFUNC(int, modinit, (int ipinum))141DEFINE_VMMOPS_IFUNC(int, modcleanup, (void))142DEFINE_VMMOPS_IFUNC(void *, init, (struct vm *vm, struct pmap *pmap))143DEFINE_VMMOPS_IFUNC(int, gla2gpa, (void *vcpui, struct vm_guest_paging *paging,144uint64_t gla, int prot, uint64_t *gpa, int *is_fault))145DEFINE_VMMOPS_IFUNC(int, run, (void *vcpui, register_t pc, struct pmap *pmap,146struct vm_eventinfo *info))147DEFINE_VMMOPS_IFUNC(void, cleanup, (void *vmi))148DEFINE_VMMOPS_IFUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu,149int vcpu_id))150DEFINE_VMMOPS_IFUNC(void, vcpu_cleanup, (void *vcpui))151DEFINE_VMMOPS_IFUNC(int, exception, (void *vcpui, uint64_t esr, uint64_t far))152DEFINE_VMMOPS_IFUNC(int, getreg, (void *vcpui, int num, uint64_t *retval))153DEFINE_VMMOPS_IFUNC(int, setreg, (void *vcpui, int num, uint64_t val))154DEFINE_VMMOPS_IFUNC(int, getcap, (void *vcpui, int num, int *retval))155DEFINE_VMMOPS_IFUNC(int, setcap, (void *vcpui, int num, int val))156DEFINE_VMMOPS_IFUNC(struct vmspace *, vmspace_alloc, (vm_offset_t min,157vm_offset_t max))158DEFINE_VMMOPS_IFUNC(void, vmspace_free, (struct vmspace *vmspace))159#ifdef notyet160#ifdef BHYVE_SNAPSHOT161DEFINE_VMMOPS_IFUNC(int, snapshot, (void *vmi, struct vm_snapshot_meta *meta))162DEFINE_VMMOPS_IFUNC(int, vcpu_snapshot, (void *vcpui,163struct vm_snapshot_meta *meta))164DEFINE_VMMOPS_IFUNC(int, restore_tsc, (void *vcpui, uint64_t now))165#endif166#endif167168uint64_t vmm_call_hyp(uint64_t, ...);169170#if 0171#define eprintf(fmt, ...) printf("%s:%d " fmt, __func__, __LINE__, ##__VA_ARGS__)172#else173#define eprintf(fmt, ...) do {} while(0)174#endif175176struct hypctx *arm64_get_active_vcpu(void);177void raise_data_insn_abort(struct hypctx *, uint64_t, bool, int);178179#endif /* !_VMM_ARM64_H_ */180181182