Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/riscv/vmm/riscv.h
108059 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2015 Mihai Carabas <[email protected]>
5
* Copyright (c) 2024 Ruslan Bukin <[email protected]>
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 dprintf(fmt, ...)
126
127
struct hypctx *riscv_get_active_vcpu(void);
128
void vmm_switch(struct hypctx *);
129
void vmm_unpriv_trap(struct hyptrap *, uint64_t tmp);
130
bool vmm_sbi_ecall(struct vcpu *);
131
132
void riscv_send_ipi(struct hyp *hyp, cpuset_t *cpus);
133
int riscv_check_ipi(struct hypctx *hypctx, bool clear);
134
bool riscv_check_interrupts_pending(struct hypctx *hypctx);
135
136
#endif /* !_VMM_RISCV_H_ */
137
138