Path: blob/main/sys/arm64/vmm/vmm_instruction_emul.c
39478 views
/*-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*/2728#ifdef _KERNEL29#include <sys/param.h>30#include <sys/pcpu.h>31#include <sys/systm.h>32#include <sys/proc.h>3334#include <vm/vm.h>3536#include <machine/machdep.h>37#include <machine/vmm.h>38#else39#include <sys/types.h>40#include <sys/errno.h>41#include <sys/_iovec.h>4243#include <machine/vmm.h>4445#include <assert.h>46#include <stdio.h>47#include <stdlib.h>48#include <vmmapi.h>49#endif5051#include <machine/vmm_instruction_emul.h>5253int54vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,55struct vm_guest_paging *paging __unused, mem_region_read_t memread,56mem_region_write_t memwrite, void *memarg)57{58uint64_t val;59int error;6061if (vie->dir == VM_DIR_READ) {62error = memread(vcpu, gpa, &val, vie->access_size, memarg);63if (error)64goto out;65error = vm_set_register(vcpu, vie->reg, val);66} else {67error = vm_get_register(vcpu, vie->reg, &val);68if (error)69goto out;70/* Mask any unneeded bits from the register */71if (vie->access_size < 8)72val &= (1ul << (vie->access_size * 8)) - 1;73error = memwrite(vcpu, gpa, val, vie->access_size, memarg);74}7576out:77return (error);78}7980int81vmm_emulate_register(struct vcpu *vcpu, struct vre *vre, reg_read_t regread,82reg_write_t regwrite, void *regarg)83{84uint64_t val;85int error;8687if (vre->dir == VM_DIR_READ) {88error = regread(vcpu, &val, regarg);89if (error)90goto out;91error = vm_set_register(vcpu, vre->reg, val);92} else {93error = vm_get_register(vcpu, vre->reg, &val);94if (error)95goto out;96error = regwrite(vcpu, val, regarg);97}9899out:100return (error);101}102103104