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