Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/riscv/vmm/vmm_sbi.c
109245 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2024-2025 Ruslan Bukin <[email protected]>
5
*
6
* This software was developed by the University of Cambridge Computer
7
* Laboratory (Department of Computer Science and Technology) under Innovate
8
* UK project 105694, "Digital Security by Design (DSbD) Technology Platform
9
* Prototype".
10
*
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
13
* are met:
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
*
20
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
* SUCH DAMAGE.
31
*/
32
33
#include <sys/param.h>
34
#include <sys/kernel.h>
35
#include <sys/proc.h>
36
37
#include <machine/sbi.h>
38
39
#include <dev/vmm/vmm_vm.h>
40
41
#include "riscv.h"
42
#include "vmm_fence.h"
43
44
static int
45
vmm_sbi_handle_rfnc(struct vcpu *vcpu, struct hypctx *hypctx)
46
{
47
struct vmm_fence fence;
48
cpuset_t active_cpus;
49
uint64_t hart_mask;
50
uint64_t hart_mask_base;
51
uint64_t func_id;
52
struct hyp *hyp;
53
uint16_t maxcpus;
54
cpuset_t cpus;
55
int i;
56
57
func_id = hypctx->guest_regs.hyp_a[6];
58
hart_mask = hypctx->guest_regs.hyp_a[0];
59
hart_mask_base = hypctx->guest_regs.hyp_a[1];
60
61
/* Construct vma_fence. */
62
63
fence.start = hypctx->guest_regs.hyp_a[2];
64
fence.size = hypctx->guest_regs.hyp_a[3];
65
fence.asid = hypctx->guest_regs.hyp_a[4];
66
67
switch (func_id) {
68
case SBI_RFNC_REMOTE_FENCE_I:
69
fence.type = VMM_RISCV_FENCE_I;
70
break;
71
case SBI_RFNC_REMOTE_SFENCE_VMA:
72
fence.type = VMM_RISCV_FENCE_VMA;
73
break;
74
case SBI_RFNC_REMOTE_SFENCE_VMA_ASID:
75
fence.type = VMM_RISCV_FENCE_VMA_ASID;
76
break;
77
default:
78
return (SBI_ERR_NOT_SUPPORTED);
79
}
80
81
/* Construct cpuset_t from the mask supplied. */
82
CPU_ZERO(&cpus);
83
hyp = hypctx->hyp;
84
active_cpus = vm_active_cpus(hyp->vm);
85
maxcpus = vm_get_maxcpus(hyp->vm);
86
for (i = 0; i < maxcpus; i++) {
87
vcpu = vm_vcpu(hyp->vm, i);
88
if (vcpu == NULL)
89
continue;
90
if (hart_mask_base != -1UL) {
91
if (i < hart_mask_base)
92
continue;
93
if (!(hart_mask & (1UL << (i - hart_mask_base))))
94
continue;
95
}
96
/*
97
* If either hart_mask_base or at least one hartid from
98
* hart_mask is not valid, then return error.
99
*/
100
if (!CPU_ISSET(i, &active_cpus))
101
return (SBI_ERR_INVALID_PARAM);
102
CPU_SET(i, &cpus);
103
}
104
105
if (CPU_EMPTY(&cpus))
106
return (SBI_ERR_INVALID_PARAM);
107
108
vmm_fence_add(hyp->vm, &cpus, &fence);
109
110
return (SBI_SUCCESS);
111
}
112
113
static int
114
vmm_sbi_handle_time(struct vcpu *vcpu, struct hypctx *hypctx)
115
{
116
uint64_t func_id;
117
uint64_t next_val;
118
119
func_id = hypctx->guest_regs.hyp_a[6];
120
next_val = hypctx->guest_regs.hyp_a[0];
121
122
switch (func_id) {
123
case SBI_TIME_SET_TIMER:
124
vtimer_set_timer(hypctx, next_val);
125
break;
126
default:
127
return (SBI_ERR_NOT_SUPPORTED);
128
}
129
130
return (SBI_SUCCESS);
131
}
132
133
static int
134
vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx)
135
{
136
cpuset_t active_cpus;
137
struct hyp *hyp;
138
uint64_t hart_mask;
139
uint64_t hart_mask_base;
140
uint64_t func_id;
141
cpuset_t cpus;
142
int hart_id;
143
int bit;
144
145
func_id = hypctx->guest_regs.hyp_a[6];
146
hart_mask = hypctx->guest_regs.hyp_a[0];
147
hart_mask_base = hypctx->guest_regs.hyp_a[1];
148
149
dprintf("%s: hart_mask %lx\n", __func__, hart_mask);
150
151
hyp = hypctx->hyp;
152
153
active_cpus = vm_active_cpus(hyp->vm);
154
155
CPU_ZERO(&cpus);
156
switch (func_id) {
157
case SBI_IPI_SEND_IPI:
158
while ((bit = ffs(hart_mask))) {
159
hart_id = (bit - 1);
160
hart_mask &= ~(1u << hart_id);
161
if (hart_mask_base != -1)
162
hart_id += hart_mask_base;
163
if (!CPU_ISSET(hart_id, &active_cpus))
164
return (SBI_ERR_INVALID_PARAM);
165
CPU_SET(hart_id, &cpus);
166
}
167
break;
168
default:
169
dprintf("%s: unknown func %ld\n", __func__, func_id);
170
return (SBI_ERR_NOT_SUPPORTED);
171
}
172
173
if (CPU_EMPTY(&cpus))
174
return (SBI_ERR_INVALID_PARAM);
175
176
riscv_send_ipi(hyp, &cpus);
177
178
return (SBI_SUCCESS);
179
}
180
181
bool
182
vmm_sbi_ecall(struct vcpu *vcpu)
183
{
184
int sbi_extension_id;
185
struct hypctx *hypctx;
186
int error;
187
188
hypctx = riscv_get_active_vcpu();
189
sbi_extension_id = hypctx->guest_regs.hyp_a[7];
190
191
dprintf("%s: args %lx %lx %lx %lx %lx %lx %lx %lx\n", __func__,
192
hypctx->guest_regs.hyp_a[0],
193
hypctx->guest_regs.hyp_a[1],
194
hypctx->guest_regs.hyp_a[2],
195
hypctx->guest_regs.hyp_a[3],
196
hypctx->guest_regs.hyp_a[4],
197
hypctx->guest_regs.hyp_a[5],
198
hypctx->guest_regs.hyp_a[6],
199
hypctx->guest_regs.hyp_a[7]);
200
201
switch (sbi_extension_id) {
202
case SBI_EXT_ID_RFNC:
203
error = vmm_sbi_handle_rfnc(vcpu, hypctx);
204
break;
205
case SBI_EXT_ID_TIME:
206
error = vmm_sbi_handle_time(vcpu, hypctx);
207
break;
208
case SBI_EXT_ID_IPI:
209
error = vmm_sbi_handle_ipi(vcpu, hypctx);
210
break;
211
default:
212
/* Return to handle in userspace. */
213
return (false);
214
}
215
216
hypctx->guest_regs.hyp_a[0] = error;
217
218
/* Request is handled in kernel mode. */
219
return (true);
220
}
221
222