1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#[cfg(feature = "gpu")]
pub(crate) mod gpu;
use std::path::Path;
use std::time::Duration;
use base::error;
use base::AsRawDescriptor;
use base::Descriptor;
use base::Error as SysError;
use base::MemoryMappingArena;
use base::MmapError;
use base::Protection;
use base::SafeDescriptor;
use base::Tube;
use base::UnixSeqpacket;
use hypervisor::MemSlot;
use hypervisor::Vm;
use libc::EINVAL;
use libc::ERANGE;
use once_cell::sync::Lazy;
use resources::Alloc;
use resources::SystemAllocator;
use serde::Deserialize;
use serde::Serialize;
use vm_memory::GuestAddress;
use crate::client::HandleRequestResult;
use crate::VmRequest;
use crate::VmResponse;
pub fn handle_request<T: AsRef<Path> + std::fmt::Debug>(
request: &VmRequest,
socket_path: T,
) -> HandleRequestResult {
handle_request_with_timeout(request, socket_path, None)
}
pub fn handle_request_with_timeout<T: AsRef<Path> + std::fmt::Debug>(
request: &VmRequest,
socket_path: T,
timeout: Option<Duration>,
) -> HandleRequestResult {
match UnixSeqpacket::connect(&socket_path) {
Ok(s) => {
let socket = Tube::new_from_unix_seqpacket(s);
if timeout.is_some() {
if let Err(e) = socket.set_recv_timeout(timeout) {
error!(
"failed to set recv timeout on socket at '{:?}': {}",
socket_path, e
);
return Err(());
}
}
if let Err(e) = socket.send(request) {
error!(
"failed to send request to socket at '{:?}': {}",
socket_path, e
);
return Err(());
}
match socket.recv() {
Ok(response) => Ok(response),
Err(e) => {
error!(
"failed to recv response from socket at '{:?}': {}",
socket_path, e
);
Err(())
}
}
}
Err(e) => {
error!("failed to connect to socket at '{:?}': {}", socket_path, e);
Err(())
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub enum VmMsyncRequest {
MsyncArena {
slot: MemSlot,
offset: usize,
size: usize,
},
}
#[derive(Serialize, Deserialize, Debug)]
pub enum VmMsyncResponse {
Ok,
Err(SysError),
}
impl VmMsyncRequest {
pub fn execute(&self, vm: &mut impl Vm) -> VmMsyncResponse {
use self::VmMsyncRequest::*;
match *self {
MsyncArena { slot, offset, size } => match vm.msync_memory_region(slot, offset, size) {
Ok(()) => VmMsyncResponse::Ok,
Err(e) => VmMsyncResponse::Err(e),
},
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub enum FsMappingRequest {
AllocateSharedMemoryRegion(Alloc),
CreateMemoryMapping {
slot: u32,
fd: SafeDescriptor,
size: usize,
file_offset: u64,
prot: Protection,
mem_offset: usize,
},
RemoveMemoryMapping {
slot: u32,
offset: usize,
size: usize,
},
}
pub fn prepare_shared_memory_region(
vm: &mut dyn Vm,
allocator: &mut SystemAllocator,
alloc: Alloc,
) -> Result<(u64, MemSlot), SysError> {
if !matches!(alloc, Alloc::PciBar { .. }) {
return Err(SysError::new(EINVAL));
}
match allocator.mmio_allocator_any().get(&alloc) {
Some((range, _)) => {
let size: usize = match range.len().and_then(|x| x.try_into().ok()) {
Some(v) => v,
None => return Err(SysError::new(ERANGE)),
};
let arena = match MemoryMappingArena::new(size) {
Ok(a) => a,
Err(MmapError::SystemCallFailed(e)) => return Err(e),
_ => return Err(SysError::new(EINVAL)),
};
match vm.add_memory_region(GuestAddress(range.start), Box::new(arena), false, false) {
Ok(slot) => Ok((range.start >> 12, slot)),
Err(e) => Err(e),
}
}
None => Err(SysError::new(EINVAL)),
}
}
static SHOULD_PREPARE_MEMORY_REGION: Lazy<bool> = Lazy::new(|| {
if cfg!(target_arch = "x86_64") {
match std::fs::read("/sys/module/kvm/parameters/tdp_mmu") {
Ok(bytes) if !bytes.is_empty() => bytes[0] == b'Y',
_ => false,
}
} else if cfg!(target_pointer_width = "64") {
true
} else {
false
}
});
pub fn should_prepare_memory_region() -> bool {
*SHOULD_PREPARE_MEMORY_REGION
}
impl FsMappingRequest {
pub fn execute(&self, vm: &mut dyn Vm, allocator: &mut SystemAllocator) -> VmResponse {
use self::FsMappingRequest::*;
match *self {
AllocateSharedMemoryRegion(alloc) => {
match prepare_shared_memory_region(vm, allocator, alloc) {
Ok((pfn, slot)) => VmResponse::RegisterMemory { pfn, slot },
Err(e) => VmResponse::Err(e),
}
}
CreateMemoryMapping {
slot,
ref fd,
size,
file_offset,
prot,
mem_offset,
} => {
let raw_fd: Descriptor = Descriptor(fd.as_raw_descriptor());
match vm.add_fd_mapping(slot, mem_offset, size, &raw_fd, file_offset, prot) {
Ok(()) => VmResponse::Ok,
Err(e) => VmResponse::Err(e),
}
}
RemoveMemoryMapping { slot, offset, size } => {
match vm.remove_mapping(slot, offset, size) {
Ok(()) => VmResponse::Ok,
Err(e) => VmResponse::Err(e),
}
}
}
}
}