1*47e07394SAndrew Turner /* 2*47e07394SAndrew Turner * Copyright (C) 2015 Mihai Carabas <mihai.carabas@gmail.com> 3*47e07394SAndrew Turner * All rights reserved. 4*47e07394SAndrew Turner * 5*47e07394SAndrew Turner * Redistribution and use in source and binary forms, with or without 6*47e07394SAndrew Turner * modification, are permitted provided that the following conditions 7*47e07394SAndrew Turner * are met: 8*47e07394SAndrew Turner * 1. Redistributions of source code must retain the above copyright 9*47e07394SAndrew Turner * notice, this list of conditions and the following disclaimer. 10*47e07394SAndrew Turner * 2. Redistributions in binary form must reproduce the above copyright 11*47e07394SAndrew Turner * notice, this list of conditions and the following disclaimer in the 12*47e07394SAndrew Turner * documentation and/or other materials provided with the distribution. 13*47e07394SAndrew Turner * 14*47e07394SAndrew Turner * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*47e07394SAndrew Turner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*47e07394SAndrew Turner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*47e07394SAndrew Turner * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 18*47e07394SAndrew Turner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*47e07394SAndrew Turner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*47e07394SAndrew Turner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*47e07394SAndrew Turner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*47e07394SAndrew Turner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*47e07394SAndrew Turner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*47e07394SAndrew Turner * SUCH DAMAGE. 25*47e07394SAndrew Turner */ 26*47e07394SAndrew Turner 27*47e07394SAndrew Turner #ifndef _VMM_INSTRUCTION_EMUL_H_ 28*47e07394SAndrew Turner #define _VMM_INSTRUCTION_EMUL_H_ 29*47e07394SAndrew Turner 30*47e07394SAndrew Turner /* 31*47e07394SAndrew Turner * Callback functions to read and write memory regions. 32*47e07394SAndrew Turner */ 33*47e07394SAndrew Turner typedef int (*mem_region_read_t)(struct vcpu *vcpu, uint64_t gpa, 34*47e07394SAndrew Turner uint64_t *rval, int rsize, void *arg); 35*47e07394SAndrew Turner typedef int (*mem_region_write_t)(struct vcpu *vcpu, uint64_t gpa, 36*47e07394SAndrew Turner uint64_t wval, int wsize, void *arg); 37*47e07394SAndrew Turner 38*47e07394SAndrew Turner /* 39*47e07394SAndrew Turner * Callback functions to read and write registers. 40*47e07394SAndrew Turner */ 41*47e07394SAndrew Turner typedef int (*reg_read_t)(struct vcpu *vcpu, uint64_t *rval, void *arg); 42*47e07394SAndrew Turner typedef int (*reg_write_t)(struct vcpu *vcpu, uint64_t wval, void *arg); 43*47e07394SAndrew Turner 44*47e07394SAndrew Turner /* 45*47e07394SAndrew Turner * Emulate the decoded 'vie' instruction when it contains a memory operation. 46*47e07394SAndrew Turner * 47*47e07394SAndrew Turner * The callbacks 'mrr' and 'mrw' emulate reads and writes to the memory region 48*47e07394SAndrew Turner * containing 'gpa'. 'mrarg' is an opaque argument that is passed into the 49*47e07394SAndrew Turner * callback functions. 50*47e07394SAndrew Turner * 51*47e07394SAndrew Turner * 'void *vm' should be 'struct vm *' when called from kernel context and 52*47e07394SAndrew Turner * 'struct vmctx *' when called from user context. 53*47e07394SAndrew Turner * 54*47e07394SAndrew Turner */ 55*47e07394SAndrew Turner int vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie, 56*47e07394SAndrew Turner struct vm_guest_paging *paging, mem_region_read_t mrr, 57*47e07394SAndrew Turner mem_region_write_t mrw, void *mrarg); 58*47e07394SAndrew Turner 59*47e07394SAndrew Turner /* 60*47e07394SAndrew Turner * Emulate the decoded 'vre' instruction when it contains a register access. 61*47e07394SAndrew Turner * 62*47e07394SAndrew Turner * The callbacks 'regread' and 'regwrite' emulate reads and writes to the 63*47e07394SAndrew Turner * register from 'vie'. 'regarg' is an opaque argument that is passed into the 64*47e07394SAndrew Turner * callback functions. 65*47e07394SAndrew Turner * 66*47e07394SAndrew Turner * 'void *vm' should be 'struct vm *' when called from kernel context and 67*47e07394SAndrew Turner * 'struct vmctx *' when called from user context. 68*47e07394SAndrew Turner * 69*47e07394SAndrew Turner */ 70*47e07394SAndrew Turner int vmm_emulate_register(struct vcpu *vcpu, struct vre *vre, reg_read_t regread, 71*47e07394SAndrew Turner reg_write_t regwrite, void *regarg); 72*47e07394SAndrew Turner 73*47e07394SAndrew Turner #ifdef _KERNEL 74*47e07394SAndrew Turner void vm_register_reg_handler(struct vm *vm, uint64_t iss, uint64_t mask, 75*47e07394SAndrew Turner reg_read_t reg_read, reg_write_t reg_write, void *arg); 76*47e07394SAndrew Turner void vm_deregister_reg_handler(struct vm *vm, uint64_t iss, uint64_t mask); 77*47e07394SAndrew Turner 78*47e07394SAndrew Turner void vm_register_inst_handler(struct vm *vm, uint64_t start, uint64_t size, 79*47e07394SAndrew Turner mem_region_read_t mmio_read, mem_region_write_t mmio_write); 80*47e07394SAndrew Turner void vm_deregister_inst_handler(struct vm *vm, uint64_t start, uint64_t size); 81*47e07394SAndrew Turner #endif 82*47e07394SAndrew Turner 83*47e07394SAndrew Turner #endif /* _VMM_INSTRUCTION_EMUL_H_ */ 84