-
Notifications
You must be signed in to change notification settings - Fork 7
/
io_port.rs
90 lines (81 loc) · 2.5 KB
/
io_port.rs
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
// The intention is to write the names in full for clarity
use x86;
// An IOPort grants outb/inb access to an X86 I/O Port
#[derive(Debug)]
pub struct IOPort {
// Private fields
_port: u16,
_can_read: bool,
_can_write: bool,
}
impl IOPort {
pub unsafe fn new(port: u16, can_read: bool, can_write: bool) -> IOPort {
// FIXME: Have a standarized data structure to declare capabilities
IOPort {
_port: port,
_can_read: can_read,
_can_write: can_write,
}
}
/// Whether the port can be read
pub fn can_read(&self) -> bool {
self._can_read
}
/// Whether the port can be written to
pub fn can_write(&self) -> bool {
self._can_write
}
/// Write 8 bits to port
pub fn outb(&mut self, val: u8) -> Result<u8, &'static str> {
if !self._can_write {
Err("Policy forbids writing to port")
} else {
unsafe {
x86::io::outb(self._port, val);
}
Ok(val)
}
}
/// Read 8 bits from port
pub fn inb(&self) -> Result<u8, &'static str> {
if !self._can_read {
Err("Policy forbids reading from port")
} else {
let val = unsafe { x86::io::inb(self._port) };
Ok(val)
}
}
// FIXME: The following methods should be implemented in the x86 crate
/// Write dword string to port
pub fn outsl(&self, val: &[u32]) -> Result<(), &'static str> {
if !self._can_write {
Err("Policy forbids writing to port")
} else {
unsafe {
llvm_asm!("cld; rep; outsd"
: // output
: "{dx}"(self._port), "{esi}"(val as *const _ as *const () as usize), "{ecx}"(val.len() as u32) // input
: // clobber
: "intel" // options
);
}
Ok(())
}
}
/// Read dword string from port
pub fn insl(&self, val: &mut [u32]) -> Result<(), &'static str> {
if !self._can_read {
Err("Policy forbids reading from port")
} else {
unsafe {
llvm_asm!("cld; rep; insd"
: "={esi}"(val as *const _ as *const () as usize) // output
: "{dx}"(self._port), "{ecx}"(val.len() as u32) // input
: // clobber
: "intel" // options
);
}
Ok(())
}
}
}