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
use {
crate::{
events::event_channel_op,
memory::{MachineFrameNumber, VirtualAddress},
scheduler::{schedule_operation, Command},
xen_sys::{evtchn_port_t, evtchn_send, start_info_t, xencons_interface, EVTCHNOP_send},
},
core::{
convert::TryInto,
fmt,
sync::atomic::{fence, Ordering},
},
spin::Mutex,
};
static WRITER: Mutex<Option<Writer>> = Mutex::new(None);
pub struct Writer<'a> {
console: &'a mut xencons_interface,
console_evt: evtchn_port_t,
}
impl<'a> Writer<'a> {
pub fn init(start: &start_info_t) {
if WRITER.lock().is_some() {
panic!("WRITER already initialized");
}
let dom_u = unsafe { start.console.domU };
let console = {
let virt = VirtualAddress::from(MachineFrameNumber(
dom_u
.mfn
.try_into()
.expect("Failed to convert u64 to usize"),
));
unsafe { &mut *(virt.0 as *mut xencons_interface) }
};
*WRITER.lock() = Some(Writer {
console,
console_evt: dom_u.evtchn,
});
}
pub fn flush() {
match *WRITER.lock() {
Some(ref mut w) => {
while (w.console).out_cons < (w.console).out_prod {
schedule_operation(Command::Yield);
fence(Ordering::SeqCst);
}
}
None => panic!("WRITER not initialized"),
}
}
fn write_bytes(&mut self, mut bytes: &[u8]) {
while bytes.len() > 0 {
let sent = self.xencons_write_bytes(bytes);
self.event_send();
bytes = &bytes[sent..];
}
self.event_send();
}
fn xencons_write_bytes(&mut self, bytes: &[u8]) -> usize {
let mut sent = 0;
let intf = &mut self.console;
let mut prod = intf.out_prod;
fence(Ordering::SeqCst);
while (sent < bytes.len()) && (((prod - intf.out_cons) as usize) < intf.out.len()) {
intf.out[((prod) & (intf.out.len() as u32 - 1)) as usize] = bytes[sent] as i8;
prod += 1;
sent += 1;
}
fence(Ordering::SeqCst);
intf.out_prod = prod;
sent
}
fn event_send(&self) {
let mut op = evtchn_send {
port: self.console_evt,
};
event_channel_op(EVTCHNOP_send, &mut op as *mut _ as u64);
}
}
impl<'a> fmt::Write for Writer<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_bytes(s.as_bytes());
Ok(())
}
}
#[macro_export]
macro_rules! dbg {
() => {
$crate::println!("[{}:{}]", core::file!(), core::line!());
};
($val:expr $(,)?) => {
match $val {
tmp => {
$crate::println!("[{}:{}] {} = {:#?}",
core::file!(), core::line!(), core::stringify!($val), &tmp);
tmp
}
}
};
($($val:expr),+ $(,)?) => {
($($crate::dbg!($val)),+,)
};
}
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n\r"));
($($arg:tt)*) => ($crate::print!("{}\n\r", format_args!($($arg)*)));
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::console::_print(format_args!($($arg)*)));
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
match *WRITER.lock() {
Some(ref mut w) => w.write_fmt(format_args!("{}\0", args)).unwrap(),
None => panic!("WRITER not initialized"),
}
}