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
#![feature(core_intrinsics)]
#![cfg_attr(unix, feature(libc))]
#![no_std]
#[cfg_attr(unix, macro_use)]
#[cfg(unix)]
extern crate std;
#[cfg(unix)]
extern crate byteorder;
#[cfg(unix)]
extern crate libc;
#[cfg_attr(unix, macro_use(matches, assert_matches))]
#[cfg(unix)]
extern crate matches;
#[cfg(unix)]
extern crate mmap;
#[cfg(target_os = "barrelfish")]
extern crate libbarrelfish;
#[cfg_attr(unix, macro_use)]
extern crate log;
extern crate bit_field;
extern crate x86;
use core::fmt;
use core::intrinsics::{volatile_load, volatile_store};
use core::ops::{BitAnd, BitOr, Not};
#[macro_use]
pub mod bitops;
#[cfg(unix)]
pub mod timedops;
pub mod net;
#[cfg(target_os = "barrelfish")]
mod barrelfish;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "barrelfish")]
pub use barrelfish::*;
#[cfg(target_os = "linux")]
pub use linux::*;
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum DriverState {
Uninitialized,
Initialized,
Attached(usize),
Detached,
Destroyed,
}
pub trait DriverControl: Sized {
fn init(&mut self) {
assert!(self.state() == DriverState::Uninitialized);
self.set_state(DriverState::Initialized);
}
fn attach(&mut self) {
#[cfg(unix)]
assert!(
self.state() == DriverState::Initialized
|| self.state() == DriverState::Detached
|| matches!(self.state(), DriverState::Attached(_))
);
self.set_state(DriverState::Attached(0));
}
fn detach(&mut self) {
#[cfg(unix)]
assert!(matches!(self.state(), DriverState::Attached(_)));
self.set_state(DriverState::Detached);
}
fn set_sleep_level(&mut self, level: usize) {
#[cfg(unix)]
assert_matches!(self.state(), DriverState::Attached(_));
self.set_state(DriverState::Attached(level));
}
fn destroy(mut self) {
#[cfg(unix)]
assert!(matches!(self.state(), DriverState::Attached(_)));
self.set_state(DriverState::Destroyed);
}
fn state(&self) -> DriverState;
fn set_state(&mut self, ds: DriverState);
}
#[repr(C, packed)]
pub struct Volatile<T> {
value: T,
}
impl<T> Volatile<T>
where
T: Copy + PartialEq + BitAnd<Output = T> + BitOr<Output = T> + Not<Output = T>,
{
pub fn new() -> Self {
Volatile {
value: unsafe { core::mem::MaybeUninit::zeroed().assume_init() },
}
}
pub fn with_value(value: T) -> Volatile<T> {
Volatile { value: value }
}
#[inline]
pub fn get(&self) -> T {
unsafe { volatile_load(&self.value) }
}
#[inline]
pub fn set(&mut self, value: T) {
unsafe { volatile_store(&self.value as *const T as *mut T, value) }
}
}
impl<T: fmt::Debug> fmt::Debug for Volatile<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
unsafe { write!(f, "{:?}", self.value) }
}
}
impl<T: fmt::Display> fmt::Display for Volatile<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
unsafe { write!(f, "{}", self.value) }
}
}
pub trait MsrInterface {
unsafe fn write(&mut self, msr: u32, value: u64) {
x86::msr::wrmsr(msr, value);
}
unsafe fn read(&mut self, msr: u32) -> u64 {
x86::msr::rdmsr(msr)
}
}