syscall/io/
mmio.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
use core::ops::{BitAnd, BitOr, Not};
use core::{mem::MaybeUninit, ptr};

use super::io::Io;

#[repr(transparent)]
pub struct Mmio<T> {
    value: MaybeUninit<T>,
}

impl<T> Mmio<T> {
    pub unsafe fn zeroed() -> Self {
        Self {
            value: MaybeUninit::zeroed(),
        }
    }
    pub unsafe fn uninit() -> Self {
        Self {
            value: MaybeUninit::uninit(),
        }
    }
    pub const fn from(value: T) -> Self {
        Self {
            value: MaybeUninit::new(value),
        }
    }
}

// Generic implementation (WARNING: requires aligned pointers!)
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
impl<T> Io for Mmio<T>
where
    T: Copy + PartialEq + BitAnd<Output = T> + BitOr<Output = T> + Not<Output = T>,
{
    type Value = T;

    fn read(&self) -> T {
        unsafe { ptr::read_volatile(ptr::addr_of!(self.value).cast::<T>()) }
    }

    fn write(&mut self, value: T) {
        unsafe { ptr::write_volatile(ptr::addr_of_mut!(self.value).cast::<T>(), value) };
    }
}

// x86 u8 implementation
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
impl Io for Mmio<u8> {
    type Value = u8;

    fn read(&self) -> Self::Value {
        unsafe {
            let value: Self::Value;
            let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov {}, [{}]",
                out(reg_byte) value,
                in(reg) ptr
            );
            value
        }
    }

    fn write(&mut self, value: Self::Value) {
        unsafe {
            let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov [{}], {}",
                in(reg) ptr,
                in(reg_byte) value,
            );
        }
    }
}

// x86 u16 implementation
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
impl Io for Mmio<u16> {
    type Value = u16;

    fn read(&self) -> Self::Value {
        unsafe {
            let value: Self::Value;
            let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov {:x}, [{}]",
                out(reg) value,
                in(reg) ptr
            );
            value
        }
    }

    fn write(&mut self, value: Self::Value) {
        unsafe {
            let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov [{}], {:x}",
                in(reg) ptr,
                in(reg) value,
            );
        }
    }
}

// x86 u32 implementation
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
impl Io for Mmio<u32> {
    type Value = u32;

    fn read(&self) -> Self::Value {
        unsafe {
            let value: Self::Value;
            let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov {:e}, [{}]",
                out(reg) value,
                in(reg) ptr
            );
            value
        }
    }

    fn write(&mut self, value: Self::Value) {
        unsafe {
            let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov [{}], {:e}",
                in(reg) ptr,
                in(reg) value,
            );
        }
    }
}

// x86 u64 implementation (x86_64 only)
#[cfg(target_arch = "x86_64")]
impl Io for Mmio<u64> {
    type Value = u64;

    fn read(&self) -> Self::Value {
        unsafe {
            let value: Self::Value;
            let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov {:r}, [{}]",
                out(reg) value,
                in(reg) ptr
            );
            value
        }
    }

    fn write(&mut self, value: Self::Value) {
        unsafe {
            let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
            core::arch::asm!(
                "mov [{}], {:r}",
                in(reg) ptr,
                in(reg) value,
            );
        }
    }
}