/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 Arm Ltd4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/types.h>28#include <sys/systm.h>2930#include <machine/ifunc.h>3132#include "arm64.h"33#include "vmm_handlers.h"3435/* Read an EL2 register */36static uint64_t37vmm_nvhe_read_reg(uint64_t reg)38{39return (vmm_call_hyp(HYP_READ_REGISTER, reg));40}4142DEFINE_IFUNC(, uint64_t, vmm_read_reg, (uint64_t reg))43{44if (in_vhe())45return (vmm_vhe_read_reg);46return (vmm_nvhe_read_reg);47}4849/* Enter the guest */50static uint64_t51vmm_nvhe_enter_guest(struct hyp *hyp, struct hypctx *hypctx)52{53return (vmm_call_hyp(HYP_ENTER_GUEST, hyp->el2_addr, hypctx->el2_addr));54}5556DEFINE_IFUNC(, uint64_t, vmm_enter_guest,57(struct hyp *hyp, struct hypctx *hypctx))58{59if (in_vhe())60return (vmm_vhe_enter_guest);61return (vmm_nvhe_enter_guest);62}6364/* Clean the TLB for all guests */65static void66vmm_nvhe_clean_s2_tlbi(void)67{68vmm_call_hyp(HYP_CLEAN_S2_TLBI);69}7071DEFINE_IFUNC(, void, vmm_clean_s2_tlbi, (void))72{73if (in_vhe())74return (vmm_vhe_clean_s2_tlbi);75return (vmm_nvhe_clean_s2_tlbi);76}7778/*79* Switch to a guest vttbr and clean the TLB for a range of guest80* virtual address space.81*/82static void83vmm_nvhe_s2_tlbi_range(uint64_t vttbr, vm_offset_t sva, vm_offset_t eva,84bool final_only)85{86vmm_call_hyp(HYP_S2_TLBI_RANGE, vttbr, sva, eva, final_only);87}8889DEFINE_IFUNC(, void, vmm_s2_tlbi_range,90(uint64_t vttbr, vm_offset_t sva, vm_offset_t eva, bool final_only))91{92if (in_vhe())93return (vmm_vhe_s2_tlbi_range);94return (vmm_nvhe_s2_tlbi_range);95}9697/*98* Switch to a guest vttbr and clean the TLB for all the guest99* virtual address space.100*/101static void102vmm_nvhe_s2_tlbi_all(uint64_t vttbr)103{104vmm_call_hyp(HYP_S2_TLBI_ALL, vttbr);105}106107DEFINE_IFUNC(, void, vmm_s2_tlbi_all, (uint64_t vttbr))108{109if (in_vhe())110return (vmm_vhe_s2_tlbi_all);111return (vmm_nvhe_s2_tlbi_all);112}113114115