Skip to main content

strat9_kernel/arch/x86_64/
io.rs

1//! Port I/O functions for x86_64 (inspired by Maestro OS)
2//!
3//! These functions allow communication with hardware via I/O ports.
4
5use core::arch::asm;
6
7/// Read a byte from the specified port.
8///
9/// # Safety
10/// Reading from an invalid port has undefined behavior.
11#[inline(always)]
12pub unsafe fn inb(port: u16) -> u8 {
13    let ret: u8;
14    unsafe {
15        asm!("in al, dx", out("al") ret, in("dx") port, options(nostack, nomem));
16    }
17    ret
18}
19
20/// Read a word from the specified port.
21///
22/// # Safety
23/// Reading from an invalid port has undefined behavior.
24#[inline(always)]
25pub unsafe fn inw(port: u16) -> u16 {
26    let ret: u16;
27    unsafe {
28        asm!("in ax, dx", out("ax") ret, in("dx") port, options(nostack, nomem));
29    }
30    ret
31}
32
33/// Read a dword from the specified port.
34///
35/// # Safety
36/// Reading from an invalid port has undefined behavior.
37#[inline(always)]
38pub unsafe fn inl(port: u16) -> u32 {
39    let ret: u32;
40    unsafe {
41        asm!("in eax, dx", out("eax") ret, in("dx") port, options(nostack, nomem));
42    }
43    ret
44}
45
46/// Write a byte to the specified port.
47///
48/// # Safety
49/// Writing to an invalid port has undefined behavior.
50#[inline(always)]
51pub unsafe fn outb(port: u16, value: u8) {
52    unsafe {
53        asm!("out dx, al", in("al") value, in("dx") port, options(nostack, nomem));
54    }
55}
56
57/// Write a word to the specified port.
58///
59/// # Safety
60/// Writing to an invalid port has undefined behavior.
61#[inline(always)]
62pub unsafe fn outw(port: u16, value: u16) {
63    unsafe {
64        asm!("out dx, ax", in("ax") value, in("dx") port, options(nostack, nomem));
65    }
66}
67
68/// Write a dword to the specified port.
69///
70/// # Safety
71/// Writing to an invalid port has undefined behavior.
72#[inline(always)]
73pub unsafe fn outl(port: u16, value: u32) {
74    unsafe {
75        asm!("out dx, eax", in("eax") value, in("dx") port, options(nostack, nomem));
76    }
77}
78
79/// Short delay for I/O operations (port 0x80 trick)
80#[inline(always)]
81pub fn io_wait() {
82    unsafe {
83        outb(0x80, 0);
84    }
85}