1use std::result;
6
7use devices::IrqChipX86_64;
8use remain::sorted;
9use thiserror::Error;
10
11#[sorted]
12#[derive(Error, Debug)]
13pub enum Error {
14 #[error("GetLapic ioctl failed: {0}")]
15 GetLapic(base::Error),
16 #[error("SetLapic ioctl failed: {0}")]
17 SetLapic(base::Error),
18}
19
20pub type Result<T> = result::Result<T, Error>;
21
22const APIC_LVT0_OFFSET: usize = 0x350;
26const APIC_LVT1_OFFSET: usize = 0x360;
27
28const APIC_LVT0_REGISTER: usize = lapic_byte_offset_to_register(APIC_LVT0_OFFSET);
30const APIC_LVT1_REGISTER: usize = lapic_byte_offset_to_register(APIC_LVT1_OFFSET);
31
32const APIC_MODE_NMI: u32 = 0x4;
33const APIC_MODE_EXTINT: u32 = 0x7;
34
35const fn lapic_byte_offset_to_register(offset_bytes: usize) -> usize {
37 offset_bytes / 16
39}
40
41fn set_apic_delivery_mode(reg: u32, mode: u32) -> u32 {
42 ((reg) & !0x700) | ((mode) << 8)
43}
44
45pub fn set_lint(vcpu_id: usize, irqchip: &mut dyn IrqChipX86_64) -> Result<()> {
51 let mut lapic = irqchip.get_lapic_state(vcpu_id).map_err(Error::GetLapic)?;
52
53 for (reg, mode) in &[
54 (APIC_LVT0_REGISTER, APIC_MODE_EXTINT),
55 (APIC_LVT1_REGISTER, APIC_MODE_NMI),
56 ] {
57 lapic.regs[*reg] = set_apic_delivery_mode(lapic.regs[*reg], *mode);
58 }
59
60 irqchip
61 .set_lapic_state(vcpu_id, &lapic)
62 .map_err(Error::SetLapic)
63}