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 #ifdef _KERNEL
30 #include <sys/param.h>
31 #include <sys/pcpu.h>
32 #include <sys/systm.h>
33 #include <sys/proc.h>
34
35 #include <vm/vm.h>
36
37 #include <machine/machdep.h>
38 #include <machine/vmm.h>
39 #else
40 #include <sys/types.h>
41 #include <sys/errno.h>
42 #include <sys/_iovec.h>
43
44 #include <machine/vmm.h>
45
46 #include <assert.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <vmmapi.h>
50 #endif
51
52 #include <machine/vmm_instruction_emul.h>
53
54 int
vmm_emulate_instruction(struct vcpu * vcpu,uint64_t gpa,struct vie * vie,struct vm_guest_paging * paging __unused,mem_region_read_t memread,mem_region_write_t memwrite,void * memarg)55 vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,
56 struct vm_guest_paging *paging __unused, mem_region_read_t memread,
57 mem_region_write_t memwrite, void *memarg)
58 {
59 uint64_t val;
60 int error;
61
62 if (vie->dir == VM_DIR_READ) {
63 error = memread(vcpu, gpa, &val, vie->access_size, memarg);
64 if (error)
65 goto out;
66 error = vm_set_register(vcpu, vie->reg, val);
67 } else {
68 error = vm_get_register(vcpu, vie->reg, &val);
69 if (error)
70 goto out;
71 /* Mask any unneeded bits from the register */
72 if (vie->access_size < 8)
73 val &= (1ul << (vie->access_size * 8)) - 1;
74 error = memwrite(vcpu, gpa, val, vie->access_size, memarg);
75 }
76
77 out:
78 return (error);
79 }
80
81 int
vmm_emulate_register(struct vcpu * vcpu,struct vre * vre,reg_read_t regread,reg_write_t regwrite,void * regarg)82 vmm_emulate_register(struct vcpu *vcpu, struct vre *vre, reg_read_t regread,
83 reg_write_t regwrite, void *regarg)
84 {
85 uint64_t val;
86 int error;
87
88 if (vre->dir == VM_DIR_READ) {
89 error = regread(vcpu, &val, regarg);
90 if (error)
91 goto out;
92 error = vm_set_register(vcpu, vre->reg, val);
93 } else {
94 error = vm_get_register(vcpu, vre->reg, &val);
95 if (error)
96 goto out;
97 error = regwrite(vcpu, val, regarg);
98 }
99
100 out:
101 return (error);
102 }
103