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
//! Pio handlers to test pio instructions correctly implemented.
use crate::vmexit::pio::{Direction, PioHandler};
use alloc::{boxed::Box, collections::LinkedList};
use core::fmt::Write;
use keos::spin_lock::SpinLock;
use kev::{
    vcpu::{GenericVCpuState, VmexitResult},
    Probe, VmError,
};

/// emulation of the device that prints the port & direction.
pub struct PioHandlerDummy;
impl PioHandler for PioHandlerDummy {
    fn handle(
        &self,
        port: u16,
        direction: Direction,
        _p: &dyn Probe,
        _generic_vcpu_state: &mut GenericVCpuState,
    ) -> Result<VmexitResult, VmError> {
        let _ = writeln!(
            &mut crate::PrinterProxy,
            "port {} direction {:?}",
            port,
            direction
        );
        Ok(VmexitResult::Ok)
    }
}

/// emulation of the device that print the character of the operand in Out instruction.
pub struct PioHandlerPrint;
impl PioHandler for PioHandlerPrint {
    fn handle(
        &self,
        _port: u16,
        direction: Direction,
        _p: &dyn Probe,
        _generic_vcpu_state: &mut GenericVCpuState,
    ) -> Result<VmexitResult, VmError> {
        let char = match direction {
            Direction::Outb(byte) => byte,
            _ => unreachable!(),
        };
        let b = core::char::from_u32(char as u32).unwrap();
        let _ = write!(&mut crate::PrinterProxy, "{}", b);
        Ok(VmexitResult::Ok)
    }
}

/// emulation of device that tests all In/Out/Ins/Outs instruction family with three queues.
pub struct PioHandlerQueue {
    byte_queue: SpinLock<LinkedList<u8>>,
    word_queue: SpinLock<LinkedList<u16>>,
    dword_queue: SpinLock<LinkedList<u32>>,
}
impl PioHandlerQueue {
    /// Create a new PioHandlerQueue.
    pub fn new() -> Self {
        Self {
            byte_queue: SpinLock::new(LinkedList::new()),
            word_queue: SpinLock::new(LinkedList::new()),
            dword_queue: SpinLock::new(LinkedList::new()),
        }
    }
}
impl PioHandler for PioHandlerQueue {
    fn handle(
        &self,
        _port: u16,
        direction: Direction,
        p: &dyn Probe,
        GenericVCpuState { vmcs, gprs, .. }: &mut GenericVCpuState,
    ) -> Result<VmexitResult, VmError> {
        match direction {
            Direction::InbAl => {
                if let Some(byte) = self.byte_queue.lock().pop_front() {
                    gprs.rax = byte as usize;
                } else {
                    return Err(VmError::ControllerError(Box::new("Empty byte queue")));
                }
            }
            Direction::InwAx => {
                if let Some(word) = self.word_queue.lock().pop_front() {
                    gprs.rax = word as usize;
                } else {
                    return Err(VmError::ControllerError(Box::new("Empty word queue")));
                }
            }
            Direction::IndEax => {
                if let Some(dword) = self.dword_queue.lock().pop_front() {
                    gprs.rax = dword as usize;
                } else {
                    return Err(VmError::ControllerError(Box::new("Empty dword queue")));
                }
            }
            Direction::Inbm(gva) => {
                if let Some(byte) = self.byte_queue.lock().pop_front() {
                    unsafe {
                        *(p.gva2hva(vmcs, gva).unwrap().into_usize() as *mut u8) = byte;
                    }
                } else {
                    return Err(VmError::ControllerError(Box::new("Empty byte queue")));
                }
            }
            Direction::Inwm(gva) => {
                if let Some(word) = self.word_queue.lock().pop_front() {
                    unsafe {
                        *(p.gva2hva(vmcs, gva).unwrap().into_usize() as *mut u16) = word;
                    }
                } else {
                    return Err(VmError::ControllerError(Box::new("Empty word queue")));
                }
            }
            Direction::Indm(gva) => {
                if let Some(dword) = self.dword_queue.lock().pop_front() {
                    unsafe {
                        *(p.gva2hva(vmcs, gva).unwrap().into_usize() as *mut u32) = dword;
                    }
                } else {
                    return Err(VmError::ControllerError(Box::new("Empty dword queue")));
                }
            }
            Direction::Outb(byte) => {
                self.byte_queue.lock().push_back(byte);
            }
            Direction::Outw(word) => {
                self.word_queue.lock().push_back(word);
            }
            Direction::Outd(dword) => {
                self.dword_queue.lock().push_back(dword);
            }
        }
        Ok(VmexitResult::Ok)
    }
}