xref: /freebsd/sys/riscv/vmm/riscv.h (revision 4eee1381396714175495b395bd6e74b263b2b16d)
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 #ifndef _VMM_RISCV_H_
35 #define _VMM_RISCV_H_
36 
37 #include <machine/reg.h>
38 #include <machine/pcpu.h>
39 #include <machine/vmm.h>
40 
41 #include <riscv/vmm/vmm_vtimer.h>
42 
43 struct hypregs {
44 	uint64_t hyp_ra;
45 	uint64_t hyp_sp;
46 	uint64_t hyp_gp;
47 	uint64_t hyp_tp;
48 	uint64_t hyp_t[7];
49 	uint64_t hyp_s[12];
50 	uint64_t hyp_a[8];
51 	uint64_t hyp_sepc;
52 	uint64_t hyp_sstatus;
53 	uint64_t hyp_hstatus;
54 };
55 
56 struct hypcsr {
57 	uint64_t hvip;
58 	uint64_t vsstatus;
59 	uint64_t vsie;
60 	uint64_t vstvec;
61 	uint64_t vsscratch;
62 	uint64_t vsepc;
63 	uint64_t vscause;
64 	uint64_t vstval;
65 	uint64_t vsatp;
66 	uint64_t scounteren;
67 	uint64_t senvcfg;
68 };
69 
70 enum vmm_fence_type {
71 	VMM_RISCV_FENCE_INVALID = 0,
72 	VMM_RISCV_FENCE_I,
73 	VMM_RISCV_FENCE_VMA,
74 	VMM_RISCV_FENCE_VMA_ASID,
75 };
76 
77 struct vmm_fence {
78 	enum vmm_fence_type type;
79 	size_t start;
80 	size_t size;
81 	uint64_t asid;
82 };
83 
84 struct hypctx {
85 	struct hypregs host_regs;
86 	struct hypregs guest_regs;
87 	struct hypcsr guest_csrs;
88 	uint64_t host_sscratch;
89 	uint64_t host_stvec;
90 	uint64_t host_scounteren;
91 	uint64_t guest_scounteren;
92 	struct hyp *hyp;
93 	struct vcpu *vcpu;
94 	bool has_exception;
95 	int cpu_id;
96 	int ipi_pending;
97 	int interrupts_pending;
98 	struct vtimer vtimer;
99 
100 	struct vmm_fence *fence_queue;
101 	struct mtx fence_queue_mtx;
102 	int fence_queue_head;
103 	int fence_queue_tail;
104 #define	FENCE_REQ_I	(1 << 0)
105 #define	FENCE_REQ_VMA	(1 << 1)
106 	int fence_req;
107 };
108 
109 struct hyp {
110 	struct vm	*vm;
111 	uint64_t	vmid_generation;
112 	bool		aplic_attached;
113 	struct aplic	*aplic;
114 	struct hypctx	*ctx[];
115 };
116 
117 struct hyptrap {
118 	uint64_t sepc;
119 	uint64_t scause;
120 	uint64_t stval;
121 	uint64_t htval;
122 	uint64_t htinst;
123 };
124 
125 #define	DEFINE_VMMOPS_IFUNC(ret_type, opname, args)	\
126     ret_type vmmops_##opname args;
127 
128 DEFINE_VMMOPS_IFUNC(int, modinit, (void))
129 DEFINE_VMMOPS_IFUNC(int, modcleanup, (void))
130 DEFINE_VMMOPS_IFUNC(void *, init, (struct vm *vm, struct pmap *pmap))
131 DEFINE_VMMOPS_IFUNC(int, gla2gpa, (void *vcpui, struct vm_guest_paging *paging,
132     uint64_t gla, int prot, uint64_t *gpa, int *is_fault))
133 DEFINE_VMMOPS_IFUNC(int, run, (void *vcpui, register_t pc, struct pmap *pmap,
134     struct vm_eventinfo *info))
135 DEFINE_VMMOPS_IFUNC(void, cleanup, (void *vmi))
136 DEFINE_VMMOPS_IFUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu,
137     int vcpu_id))
138 DEFINE_VMMOPS_IFUNC(void, vcpu_cleanup, (void *vcpui))
139 DEFINE_VMMOPS_IFUNC(int, exception, (void *vcpui, uint64_t scause))
140 DEFINE_VMMOPS_IFUNC(int, getreg, (void *vcpui, int num, uint64_t *retval))
141 DEFINE_VMMOPS_IFUNC(int, setreg, (void *vcpui, int num, uint64_t val))
142 DEFINE_VMMOPS_IFUNC(int, getcap, (void *vcpui, int num, int *retval))
143 DEFINE_VMMOPS_IFUNC(int, setcap, (void *vcpui, int num, int val))
144 DEFINE_VMMOPS_IFUNC(struct vmspace *, vmspace_alloc, (vm_offset_t min,
145     vm_offset_t max))
146 DEFINE_VMMOPS_IFUNC(void, vmspace_free, (struct vmspace *vmspace))
147 
148 #define	dprintf(fmt, ...)
149 
150 struct hypctx *riscv_get_active_vcpu(void);
151 void vmm_switch(struct hypctx *);
152 void vmm_unpriv_trap(struct hyptrap *, uint64_t tmp);
153 bool vmm_sbi_ecall(struct vcpu *);
154 
155 void riscv_send_ipi(struct hyp *hyp, cpuset_t *cpus);
156 int riscv_check_ipi(struct hypctx *hypctx, bool clear);
157 bool riscv_check_interrupts_pending(struct hypctx *hypctx);
158 
159 #endif /* !_VMM_RISCV_H_ */
160