Path: blob/main/sys/riscv/vmm/vmm_instruction_emul.c
39478 views
/*-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#ifdef _KERNEL34#include <sys/param.h>35#include <sys/pcpu.h>36#include <sys/systm.h>37#include <sys/proc.h>3839#include <vm/vm.h>4041#include <machine/machdep.h>42#include <machine/vmm.h>43#else44#include <sys/types.h>45#include <sys/errno.h>46#include <sys/_iovec.h>4748#include <machine/vmm.h>4950#include <assert.h>51#include <stdio.h>52#include <stdlib.h>53#include <vmmapi.h>54#endif5556#include <machine/vmm_instruction_emul.h>5758int59vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,60struct vm_guest_paging *paging __unused, mem_region_read_t memread,61mem_region_write_t memwrite, void *memarg)62{63uint64_t val;64int error;6566if (vie->dir == VM_DIR_READ) {67error = memread(vcpu, gpa, &val, vie->access_size, memarg);68if (error)69goto out;70if ((vie->sign_extend == 0) && (vie->access_size < 8))71val &= (1ul << (vie->access_size * 8)) - 1;72error = vm_set_register(vcpu, vie->reg, val);73} else {74error = vm_get_register(vcpu, vie->reg, &val);75if (error)76goto out;77/* Mask any unneeded bits from the register */78if (vie->access_size < 8)79val &= (1ul << (vie->access_size * 8)) - 1;80error = memwrite(vcpu, gpa, val, vie->access_size, memarg);81}8283out:84return (error);85}8687int88vmm_emulate_register(struct vcpu *vcpu, struct vre *vre, reg_read_t regread,89reg_write_t regwrite, void *regarg)90{91uint64_t val;92int error;9394if (vre->dir == VM_DIR_READ) {95error = regread(vcpu, &val, regarg);96if (error)97goto out;98error = vm_set_register(vcpu, vre->reg, val);99} else {100error = vm_get_register(vcpu, vre->reg, &val);101if (error)102goto out;103error = regwrite(vcpu, val, regarg);104}105106out:107return (error);108}109110111