1 // Copyright 2020 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use crate::{Bus, IrqEdgeEvent, IrqLevelEvent}; 6 use base::{error, Error, Event, Result}; 7 use hypervisor::kvm::KvmVcpu; 8 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] 9 use hypervisor::VmAArch64; 10 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 11 use hypervisor::VmX86_64; 12 use hypervisor::{HypervisorCap, IrqRoute, MPState, Vcpu}; 13 use kvm_sys::kvm_mp_state; 14 use resources::SystemAllocator; 15 16 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 17 mod x86_64; 18 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 19 pub use x86_64::*; 20 21 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] 22 mod aarch64; 23 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] 24 pub use aarch64::*; 25 26 use crate::{IrqChip, IrqChipCap, IrqEventIndex, VcpuRunState}; 27 28 /// This IrqChip only works with Kvm so we only implement it for KvmVcpu. 29 impl IrqChip for KvmKernelIrqChip { 30 /// Add a vcpu to the irq chip. add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()>31 fn add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()> { 32 let vcpu: &KvmVcpu = vcpu 33 .downcast_ref() 34 .expect("KvmKernelIrqChip::add_vcpu called with non-KvmVcpu"); 35 self.vcpus.lock()[vcpu_id] = Some(vcpu.try_clone()?); 36 Ok(()) 37 } 38 39 /// Register an event with edge-trigger semantic that can trigger an interrupt 40 /// for a particular GSI. register_edge_irq_event( &mut self, irq: u32, irq_event: &IrqEdgeEvent, ) -> Result<Option<IrqEventIndex>>41 fn register_edge_irq_event( 42 &mut self, 43 irq: u32, 44 irq_event: &IrqEdgeEvent, 45 ) -> Result<Option<IrqEventIndex>> { 46 self.vm.register_irqfd(irq, irq_event.get_trigger(), None)?; 47 Ok(None) 48 } 49 50 /// Unregister an event with edge-trigger semantic for a particular GSI. unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()>51 fn unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()> { 52 self.vm.unregister_irqfd(irq, irq_event.get_trigger()) 53 } 54 55 /// Register an event with level-trigger semantic that can trigger an interrupt 56 /// for a particular GSI. register_level_irq_event( &mut self, irq: u32, irq_event: &IrqLevelEvent, ) -> Result<Option<IrqEventIndex>>57 fn register_level_irq_event( 58 &mut self, 59 irq: u32, 60 irq_event: &IrqLevelEvent, 61 ) -> Result<Option<IrqEventIndex>> { 62 self.vm 63 .register_irqfd(irq, irq_event.get_trigger(), Some(irq_event.get_resample()))?; 64 Ok(None) 65 } 66 67 /// Unregister an event with level-trigger semantic for a particular GSI. unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()>68 fn unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()> { 69 self.vm.unregister_irqfd(irq, irq_event.get_trigger()) 70 } 71 72 /// Route an IRQ line to an interrupt controller, or to a particular MSI vector. route_irq(&mut self, route: IrqRoute) -> Result<()>73 fn route_irq(&mut self, route: IrqRoute) -> Result<()> { 74 let mut routes = self.routes.lock(); 75 routes.retain(|r| r.gsi != route.gsi); 76 77 routes.push(route); 78 79 self.vm.set_gsi_routing(&*routes) 80 } 81 82 /// Replace all irq routes with the supplied routes set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()>83 fn set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()> { 84 let mut current_routes = self.routes.lock(); 85 *current_routes = routes.to_vec(); 86 87 self.vm.set_gsi_routing(&*current_routes) 88 } 89 90 /// Return a vector of all registered irq numbers and their associated events and event 91 /// indices. These should be used by the main thread to wait for irq events. 92 /// For the KvmKernelIrqChip, the kernel handles listening to irq events being triggered by 93 /// devices, so this function always returns an empty Vec. irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, u32, Event)>>94 fn irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, u32, Event)>> { 95 Ok(Vec::new()) 96 } 97 98 /// Either assert or deassert an IRQ line. Sends to either an interrupt controller, or does 99 /// a send_msi if the irq is associated with an MSI. 100 /// For the KvmKernelIrqChip this simply calls the KVM_SET_IRQ_LINE ioctl. service_irq(&mut self, irq: u32, level: bool) -> Result<()>101 fn service_irq(&mut self, irq: u32, level: bool) -> Result<()> { 102 self.vm.set_irq_line(irq, level) 103 } 104 105 /// Service an IRQ event by asserting then deasserting an IRQ line. The associated Event 106 /// that triggered the irq event will be read from. If the irq is associated with a resample 107 /// Event, then the deassert will only happen after an EOI is broadcast for a vector 108 /// associated with the irq line. 109 /// This function should never be called on KvmKernelIrqChip. service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()>110 fn service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()> { 111 error!("service_irq_event should never be called for KvmKernelIrqChip"); 112 Ok(()) 113 } 114 115 /// Broadcast an end of interrupt. 116 /// This should never be called on a KvmKernelIrqChip because a KVM vcpu should never exit 117 /// with the KVM_EXIT_EOI_BROADCAST reason when an in-kernel irqchip exists. broadcast_eoi(&self, _vector: u8) -> Result<()>118 fn broadcast_eoi(&self, _vector: u8) -> Result<()> { 119 error!("broadcast_eoi should never be called for KvmKernelIrqChip"); 120 Ok(()) 121 } 122 123 /// Injects any pending interrupts for `vcpu`. 124 /// For KvmKernelIrqChip this is a no-op because KVM is responsible for injecting all 125 /// interrupts. inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()>126 fn inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()> { 127 Ok(()) 128 } 129 130 /// Notifies the irq chip that the specified VCPU has executed a halt instruction. 131 /// For KvmKernelIrqChip this is a no-op because KVM handles VCPU blocking. halted(&self, _vcpu_id: usize)132 fn halted(&self, _vcpu_id: usize) {} 133 134 /// Blocks until `vcpu` is in a runnable state or until interrupted by 135 /// `IrqChip::kick_halted_vcpus`. Returns `VcpuRunState::Runnable if vcpu is runnable, or 136 /// `VcpuRunState::Interrupted` if the wait was interrupted. 137 /// For KvmKernelIrqChip this is a no-op and always returns Runnable because KVM handles VCPU 138 /// blocking. wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState>139 fn wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState> { 140 Ok(VcpuRunState::Runnable) 141 } 142 143 /// Makes unrunnable VCPUs return immediately from `wait_until_runnable`. 144 /// For KvmKernelIrqChip this is a no-op because KVM handles VCPU blocking. kick_halted_vcpus(&self)145 fn kick_halted_vcpus(&self) {} 146 147 /// Get the current MP state of the specified VCPU. get_mp_state(&self, vcpu_id: usize) -> Result<MPState>148 fn get_mp_state(&self, vcpu_id: usize) -> Result<MPState> { 149 match self.vcpus.lock().get(vcpu_id) { 150 Some(Some(vcpu)) => Ok(MPState::from(&vcpu.get_mp_state()?)), 151 _ => Err(Error::new(libc::ENOENT)), 152 } 153 } 154 155 /// Set the current MP state of the specified VCPU. set_mp_state(&mut self, vcpu_id: usize, state: &MPState) -> Result<()>156 fn set_mp_state(&mut self, vcpu_id: usize, state: &MPState) -> Result<()> { 157 match self.vcpus.lock().get(vcpu_id) { 158 Some(Some(vcpu)) => vcpu.set_mp_state(&kvm_mp_state::from(state)), 159 _ => Err(Error::new(libc::ENOENT)), 160 } 161 } 162 163 /// Attempt to clone this IrqChip instance. try_clone(&self) -> Result<Self>164 fn try_clone(&self) -> Result<Self> { 165 // Because the KvmKernelIrqchip struct contains arch-specific fields we leave the 166 // cloning to arch-specific implementations 167 self.arch_try_clone() 168 } 169 170 /// Finalize irqchip setup. Should be called once all devices have registered irq events and 171 /// been added to the io_bus and mmio_bus. 172 /// KvmKernelIrqChip does not need to do anything here. finalize_devices( &mut self, _resources: &mut SystemAllocator, _io_bus: &Bus, _mmio_bus: &Bus, ) -> Result<()>173 fn finalize_devices( 174 &mut self, 175 _resources: &mut SystemAllocator, 176 _io_bus: &Bus, 177 _mmio_bus: &Bus, 178 ) -> Result<()> { 179 Ok(()) 180 } 181 182 /// The KvmKernelIrqChip doesn't process irq events itself so this function does nothing. process_delayed_irq_events(&mut self) -> Result<()>183 fn process_delayed_irq_events(&mut self) -> Result<()> { 184 Ok(()) 185 } 186 irq_delayed_event_token(&self) -> Result<Option<Event>>187 fn irq_delayed_event_token(&self) -> Result<Option<Event>> { 188 Ok(None) 189 } 190 check_capability(&self, c: IrqChipCap) -> bool191 fn check_capability(&self, c: IrqChipCap) -> bool { 192 match c { 193 IrqChipCap::TscDeadlineTimer => self 194 .vm 195 .get_hypervisor() 196 .check_capability(HypervisorCap::TscDeadlineTimer), 197 IrqChipCap::X2Apic => true, 198 } 199 } 200 } 201 202 #[cfg(test)] 203 mod tests { 204 use hypervisor::kvm::{Kvm, KvmVm}; 205 use hypervisor::{MPState, ProtectionType, Vm}; 206 use vm_memory::GuestMemory; 207 208 use crate::irqchip::{IrqChip, KvmKernelIrqChip}; 209 210 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] 211 use hypervisor::VmAArch64; 212 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 213 use hypervisor::VmX86_64; 214 215 #[test] create_kvm_kernel_irqchip()216 fn create_kvm_kernel_irqchip() { 217 let kvm = Kvm::new().expect("failed to instantiate Kvm"); 218 let mem = GuestMemory::new(&[]).unwrap(); 219 let vm = 220 KvmVm::new(&kvm, mem, ProtectionType::Unprotected).expect("failed to instantiate vm"); 221 222 let mut chip = KvmKernelIrqChip::new(vm.try_clone().expect("failed to clone vm"), 1) 223 .expect("failed to instantiate KvmKernelIrqChip"); 224 225 let vcpu = vm.create_vcpu(0).expect("failed to instantiate vcpu"); 226 chip.add_vcpu(0, vcpu.as_vcpu()) 227 .expect("failed to add vcpu"); 228 } 229 230 #[test] mp_state()231 fn mp_state() { 232 let kvm = Kvm::new().expect("failed to instantiate Kvm"); 233 let mem = GuestMemory::new(&[]).unwrap(); 234 let vm = 235 KvmVm::new(&kvm, mem, ProtectionType::Unprotected).expect("failed to instantiate vm"); 236 237 let mut chip = KvmKernelIrqChip::new(vm.try_clone().expect("failed to clone vm"), 1) 238 .expect("failed to instantiate KvmKernelIrqChip"); 239 240 let vcpu = vm.create_vcpu(0).expect("failed to instantiate vcpu"); 241 chip.add_vcpu(0, vcpu.as_vcpu()) 242 .expect("failed to add vcpu"); 243 244 let state = chip.get_mp_state(0).expect("failed to get mp state"); 245 assert_eq!(state, MPState::Runnable); 246 247 chip.set_mp_state(0, &MPState::Stopped) 248 .expect("failed to set mp state"); 249 250 let state = chip.get_mp_state(0).expect("failed to get mp state"); 251 assert_eq!(state, MPState::Stopped); 252 } 253 } 254