morpheus_network/asm/core/
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
//! MMIO (Memory-Mapped I/O) bindings.
//!
//! # Safety
//! - Address must be valid MMIO address
//! - Address must be properly aligned
//! - Address must be mapped with appropriate attributes
//!
//! # Reference
//! NETWORK_IMPL_GUIDE.md ยง2.2.1

#[cfg(target_arch = "x86_64")]
extern "win64" {
    fn asm_mmio_read8(addr: u64) -> u8;
    fn asm_mmio_write8(addr: u64, value: u8);
    fn asm_mmio_read16(addr: u64) -> u16;
    fn asm_mmio_write16(addr: u64, value: u16);
    fn asm_mmio_read32(addr: u64) -> u32;
    fn asm_mmio_write32(addr: u64, value: u32);
}

/// Read 8-bit value from MMIO address.
///
/// # Safety
/// Address must be valid MMIO address.
#[cfg(target_arch = "x86_64")]
#[inline]
pub unsafe fn read8(addr: u64) -> u8 {
    asm_mmio_read8(addr)
}

/// Write 8-bit value to MMIO address.
#[cfg(target_arch = "x86_64")]
#[inline]
pub unsafe fn write8(addr: u64, value: u8) {
    asm_mmio_write8(addr, value)
}

/// Read 16-bit value from MMIO address.
///
/// # Safety
/// Address must be valid, 2-byte aligned MMIO address.
#[cfg(target_arch = "x86_64")]
#[inline]
pub unsafe fn read16(addr: u64) -> u16 {
    asm_mmio_read16(addr)
}

/// Write 16-bit value to MMIO address.
#[cfg(target_arch = "x86_64")]
#[inline]
pub unsafe fn write16(addr: u64, value: u16) {
    asm_mmio_write16(addr, value)
}

/// Read 32-bit value from MMIO address.
///
/// # Safety
/// Address must be valid, 4-byte aligned MMIO address.
#[cfg(target_arch = "x86_64")]
#[inline]
pub unsafe fn read32(addr: u64) -> u32 {
    asm_mmio_read32(addr)
}

/// Write 32-bit value to MMIO address.
#[cfg(target_arch = "x86_64")]
#[inline]
pub unsafe fn write32(addr: u64, value: u32) {
    asm_mmio_write32(addr, value)
}

// Stubs for non-x86_64
#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub unsafe fn read8(_addr: u64) -> u8 {
    0
}
#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub unsafe fn write8(_addr: u64, _value: u8) {}
#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub unsafe fn read16(_addr: u64) -> u16 {
    0
}
#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub unsafe fn write16(_addr: u64, _value: u16) {}
#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub unsafe fn read32(_addr: u64) -> u32 {
    0
}
#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub unsafe fn write32(_addr: u64, _value: u32) {}