tartan_arch/x86_common/
io.rs

1//! Access to I/O space
2
3use core::arch::asm;
4
5/// Read an 8-bit value from the port at the given address in I/O space.
6///
7/// # Safety
8/// This is an extremely low-level operation that can do anything: trigger an interrupt,
9/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
10/// caution.
11pub unsafe fn in_u8(addr: u16) -> u8 {
12    let data: u8;
13    asm!(
14        "in al, dx",
15        out("al") data,
16        in("dx") addr,
17    );
18    data
19}
20
21/// Read a 16-bit value from the port at the given address in I/O space.
22///
23/// # Safety
24/// This is an extremely low-level operation that can do anything: trigger an interrupt,
25/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
26/// caution.
27pub unsafe fn in_u16(addr: u16) -> u16 {
28    let data: u16;
29    asm!(
30        "in ax, dx",
31        out("ax") data,
32        in("dx") addr,
33    );
34    data
35}
36
37/// Read a 32-bit value from the port at the given address in I/O space.
38///
39/// # Safety
40/// This is an extremely low-level operation that can do anything: trigger an interrupt,
41/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
42/// caution.
43pub unsafe fn in_u32(addr: u16) -> u32 {
44    let data: u32;
45    asm!(
46        "in eax, dx",
47        out("eax") data,
48        in("dx") addr,
49    );
50    data
51}
52
53/// Write an 8-bit value to the port at the given address in I/O space.
54///
55/// # Safety
56/// This is an extremely low-level operation that can do anything: trigger an interrupt,
57/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
58/// caution.
59pub unsafe fn out_u8(addr: u16, data: u8) {
60    asm!(
61        "out dx, al",
62        in("al") data,
63        in("dx") addr,
64    );
65}
66
67/// Write a 16-bit value to the port at the given address in I/O space.
68///
69/// # Safety
70/// This is an extremely low-level operation that can do anything: trigger an interrupt,
71/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
72/// caution.
73pub unsafe fn out_u16(addr: u16, data: u16) {
74    asm!(
75        "out dx, ax",
76        in("ax") data,
77        in("dx") addr,
78    );
79}
80
81/// Write a 16-bit value to the port at the given address in I/O space.
82///
83/// # Safety
84/// This is an extremely low-level operation that can do anything: trigger an interrupt,
85/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
86/// caution.
87pub unsafe fn out_u32(addr: u16, data: u32) {
88    asm!(
89        "out dx, eax",
90        in("eax") data,
91        in("dx") addr,
92    );
93}