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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use std::collections::HashMap;
use std::sync::mpsc;
use std::sync::Arc;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use anyhow::Error;
use arch::RunnableLinuxVm;
use arch::VcpuArch;
use arch::VmArch;
use devices::HotPlugBus;
use devices::HotPlugKey;
use devices::IrqEventSource;
use devices::IrqLevelEvent;
use devices::PciAddress;
use devices::PciInterruptPin;
use devices::PciRootCommand;
use devices::ResourceCarrier;
use resources::SystemAllocator;
#[cfg(feature = "swap")]
use swap::SwapDeviceHelper;
use sync::Mutex;
use vm_memory::GuestMemory;
use crate::crosvm::sys::unix::JailWarden;
use crate::crosvm::sys::unix::JailWardenImpl;
use crate::crosvm::sys::unix::PermissiveJailWarden;
use crate::Config;
pub type Result<T> = std::result::Result<T, Error>;
pub struct PciHotPlugManager {
ports: HashMap<u8, PortStub>,
jail_warden: Box<dyn JailWarden>,
rootbus_controller: Option<mpsc::Sender<PciRootCommand>>,
}
struct PortStub {
port: Arc<Mutex<dyn HotPlugBus>>,
devices: HashMap<PciAddress, RecoverableResource>,
}
struct RecoverableResource {
irq_num: u32,
irq_evt: IrqLevelEvent,
}
impl PciHotPlugManager {
pub fn new(
guest_memory: GuestMemory,
config: &Config,
#[cfg(feature = "swap")] swap_device_helper: Option<SwapDeviceHelper>,
) -> Result<Self> {
let jail_warden: Box<dyn JailWarden> = match config.jail_config {
Some(_) => Box::new(
JailWardenImpl::new(
guest_memory,
config,
#[cfg(feature = "swap")]
swap_device_helper,
)
.context("jail warden construction")?,
),
None => Box::new(
PermissiveJailWarden::new(
guest_memory,
config,
#[cfg(feature = "swap")]
swap_device_helper,
)
.context("jail warden construction")?,
),
};
Ok(Self {
ports: HashMap::new(),
jail_warden,
rootbus_controller: None,
})
}
pub fn set_rootbus_controller(
&mut self,
rootbus_controller: mpsc::Sender<PciRootCommand>,
) -> Result<()> {
self.rootbus_controller = Some(rootbus_controller);
Ok(())
}
pub fn add_port(&mut self, port: Arc<Mutex<dyn HotPlugBus>>) -> Result<()> {
if !port.lock().is_empty() {
bail!("invalid hotplug bus");
}
let downstream_bus = port
.lock()
.get_secondary_bus_number()
.ok_or(anyhow!("cannot get downstream bus"))?;
if self.ports.get(&downstream_bus).is_some() {
Err(anyhow!("downstream bus already used for hotplug"))
} else {
self.ports.insert(
downstream_bus,
PortStub {
port,
devices: HashMap::new(),
},
);
Ok(())
}
}
pub fn hotplug_device<V: VmArch, Vcpu: VcpuArch>(
&mut self,
resource_carriers: Vec<ResourceCarrier>,
linux: &mut RunnableLinuxVm<V, Vcpu>,
resources: &mut SystemAllocator,
) -> Result<u8> {
if resource_carriers.len() > 8 || resource_carriers.is_empty() {
bail!("PCI function has to be 1-8 inclusive");
}
let bus = self.find_empty_bus()?;
for (func_num, mut resource_carrier) in resource_carriers.into_iter().enumerate() {
let device_address = PciAddress::new(0, bus as u32, 0, func_num as u32)?;
let hotplug_key = HotPlugKey::GuestDevice {
guest_addr: device_address,
};
resource_carrier.allocate_address(device_address, resources)?;
let irq_evt = IrqLevelEvent::new()?;
let (pin, irq_num) = match bus % 4 {
0 => (PciInterruptPin::IntA, 0),
1 => (PciInterruptPin::IntB, 1),
2 => (PciInterruptPin::IntC, 2),
_ => (PciInterruptPin::IntD, 3),
};
resource_carrier.assign_irq(irq_evt.try_clone()?, pin, irq_num);
let (proxy_device, pid) = self
.jail_warden
.make_proxy_device(resource_carrier)
.context("make proxy device")?;
let device_id = proxy_device.lock().device_id();
let device_name = proxy_device.lock().debug_label();
linux.irq_chip.as_irq_chip_mut().register_level_irq_event(
irq_num,
&irq_evt,
IrqEventSource {
device_id,
queue_id: 0,
device_name: device_name.clone(),
},
)?;
let pid: u32 = pid.try_into().context("fork fail")?;
if pid > 0 {
linux.pid_debug_label_map.insert(pid, device_name);
}
self.rootbus_controller
.as_ref()
.ok_or(anyhow!("root bus controller not initialized"))?
.send(PciRootCommand::Add(device_address, proxy_device))?;
let port_stub = self
.ports
.get_mut(&bus)
.ok_or(anyhow!("invalid hotplug bus number"))?;
port_stub
.port
.lock()
.add_hotplug_device(hotplug_key, device_address);
port_stub
.devices
.insert(device_address, RecoverableResource { irq_num, irq_evt });
}
self.send_hotplug_signal(bus)?;
Ok(bus)
}
pub fn remove_hotplug_device<V: VmArch, Vcpu: VcpuArch>(
&mut self,
bus: u8,
linux: &mut RunnableLinuxVm<V, Vcpu>,
resources: &mut SystemAllocator,
) -> Result<()> {
self.remove_device_from_port(bus)?;
let port_stub = self
.ports
.get_mut(&bus)
.ok_or(anyhow!("invalid hotplug bus number"))?;
for (pci_address, recoverable_resource) in port_stub.devices.drain() {
self.rootbus_controller
.as_ref()
.ok_or(anyhow!("root bus controller not initialized"))?
.send(PciRootCommand::Remove(pci_address))?;
resources.release_pci(pci_address.bus, pci_address.dev, pci_address.func);
linux.irq_chip.unregister_level_irq_event(
recoverable_resource.irq_num,
&recoverable_resource.irq_evt,
)?;
}
Ok(())
}
fn find_empty_bus(&self) -> Result<u8> {
for (bus_num, port_stub) in &self.ports {
if port_stub.devices.is_empty() {
return Ok(*bus_num);
}
}
bail!("no ports available");
}
fn send_hotplug_signal(&self, bus: u8) -> Result<()> {
if let Some(port_stub) = self.ports.get(&bus) {
let base_pci_address = PciAddress::new(0, bus as u32, 0, 0)?;
port_stub.port.lock().hot_plug(base_pci_address);
Ok(())
} else {
bail!("invalid hotplug bus number");
}
}
fn remove_device_from_port(&self, bus: u8) -> Result<()> {
if let Some(port_stub) = self.ports.get(&bus) {
if port_stub.devices.is_empty() {
bail!("invalid hotplug bus number");
}
let base_pci_address = PciAddress::new(0, bus as u32, 0, 0)?;
port_stub.port.lock().hot_unplug(base_pci_address);
Ok(())
} else {
bail!("invalid hotplug bus number");
}
}
}