xref: /freebsd/sys/riscv/vmm/vmm_instruction_emul.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Mihai Carabas <mihai.carabas@gmail.com>
5  * Copyright (c) 2024 Ruslan Bukin <br@bsdpad.com>
6  *
7  * This software was developed by the University of Cambridge Computer
8  * Laboratory (Department of Computer Science and Technology) under Innovate
9  * UK project 105694, "Digital Security by Design (DSbD) Technology Platform
10  * Prototype".
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifdef _KERNEL
35 #include <sys/param.h>
36 #include <sys/pcpu.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 
40 #include <vm/vm.h>
41 
42 #include <machine/machdep.h>
43 #include <machine/vmm.h>
44 #else
45 #include <sys/types.h>
46 #include <sys/errno.h>
47 #include <sys/_iovec.h>
48 
49 #include <machine/vmm.h>
50 
51 #include <assert.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <vmmapi.h>
55 #endif
56 
57 #include <machine/vmm_instruction_emul.h>
58 
59 int
60 vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie,
61     struct vm_guest_paging *paging __unused, mem_region_read_t memread,
62     mem_region_write_t memwrite, void *memarg)
63 {
64 	uint64_t val;
65 	int error;
66 
67 	if (vie->dir == VM_DIR_READ) {
68 		error = memread(vcpu, gpa, &val, vie->access_size, memarg);
69 		if (error)
70 			goto out;
71 		if ((vie->sign_extend == 0) && (vie->access_size < 8))
72 			val &= (1ul << (vie->access_size * 8)) - 1;
73 		error = vm_set_register(vcpu, vie->reg, val);
74 	} else {
75 		error = vm_get_register(vcpu, vie->reg, &val);
76 		if (error)
77 			goto out;
78 		/* Mask any unneeded bits from the register */
79 		if (vie->access_size < 8)
80 			val &= (1ul << (vie->access_size * 8)) - 1;
81 		error = memwrite(vcpu, gpa, val, vie->access_size, memarg);
82 	}
83 
84 out:
85 	return (error);
86 }
87 
88 int
89 vmm_emulate_register(struct vcpu *vcpu, struct vre *vre, reg_read_t regread,
90     reg_write_t regwrite, void *regarg)
91 {
92 	uint64_t val;
93 	int error;
94 
95 	if (vre->dir == VM_DIR_READ) {
96 		error = regread(vcpu, &val, regarg);
97 		if (error)
98 			goto out;
99 		error = vm_set_register(vcpu, vre->reg, val);
100 	} else {
101 		error = vm_get_register(vcpu, vre->reg, &val);
102 		if (error)
103 			goto out;
104 		error = regwrite(vcpu, val, regarg);
105 	}
106 
107 out:
108 	return (error);
109 }
110