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
use kev::{
    vcpu::{GenericVCpuState, VmexitResult},
    vmcs::Field,
    Probe, VmError,
};
use project2::vmexit::{
    msr::Msr,
    pio::{Direction, PioHandler},
};

#[derive(Default)]
pub struct EferMsr;

impl Msr for EferMsr {
    fn rdmsr(
        &self,
        _index: u32,
        _p: &dyn Probe,
        generic_vcpu_state: &mut GenericVCpuState,
    ) -> Result<u64, VmError> {
        generic_vcpu_state.vmcs.read(Field::GuestIa32Efer)
    }

    fn wrmsr(
        &mut self,
        _index: u32,
        value: u64,
        _p: &dyn Probe,
        generic_vcpu_state: &mut GenericVCpuState,
    ) -> Result<(), VmError> {
        generic_vcpu_state.vmcs.write(Field::GuestIa32Efer, value)
    }
}

// Address: 0xCF8.
// output: 0xCFC.
pub struct PciPio;
impl PioHandler for PciPio {
    fn handle(
        &self,
        _port: u16,
        direction: Direction,
        p: &dyn Probe,
        GenericVCpuState { vmcs, gprs, .. }: &mut GenericVCpuState,
    ) -> Result<VmexitResult, VmError> {
        match direction {
            // On every out, make it no ops.
            Direction::Outb(_) | Direction::Outd(_) | Direction::Outw(_) => (),
            // On every in, just returns 0xffff.
            Direction::InbAl => {
                gprs.rax = 0xff;
            }
            Direction::InwAx | Direction::IndEax => {
                gprs.rax = 0xffff;
            }
            Direction::Inbm(gva) => unsafe {
                *p.gva2hva(vmcs, gva).unwrap().as_mut::<u8>().unwrap() = 0xff;
            },
            Direction::Inwm(gva) => unsafe {
                *p.gva2hva(vmcs, gva).unwrap().as_mut::<u16>().unwrap() = 0xffff;
            },
            Direction::Indm(gva) => unsafe {
                *p.gva2hva(vmcs, gva).unwrap().as_mut::<u32>().unwrap() = 0xffff;
            },
        };
        Ok(VmexitResult::Ok)
    }
}

pub struct CmosPio;
impl PioHandler for CmosPio {
    fn handle(
        &self,
        _port: u16,
        _direction: Direction,
        _p: &dyn Probe,
        _generic_vcpu_state: &mut GenericVCpuState,
    ) -> Result<VmexitResult, VmError> {
        // ignore.
        Ok(VmexitResult::Ok)
    }
}


pub struct ExitPio;
impl PioHandler for ExitPio {
    fn handle(
        &self,
        _port: u16,
        direction: Direction,
        _p: &dyn Probe,
        generic_vcpu_state: &mut GenericVCpuState,
    ) -> Result<VmexitResult, VmError> {
        // Pio::new(0x604).write_u32(0 | 0x2000);
        if let Direction::Outd(v) = direction {
            if v == 0 | 0x2000 {
                generic_vcpu_state.vm.upgrade().unwrap().exit(0);
                return Ok(VmexitResult::Exited(0));
            }
        }
        Ok(VmexitResult::Ok)
    }
}