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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! PL011 UART driver for QEMU virt (AArch64).
//!
//! The QEMU virt machine provides a PL011 UART at base address 0x0900_0000.
//! This driver provides minimal early-boot serial output for diagnostics.
//!
//! All functions in this module use raw pointer writes to MMIO addresses.
//! This is the hardware boundary where unsafe is acceptable and expected.
/// QEMU virt PL011 base address.
const UART_BASE: usize = 0x0900_0000;
/// Data Register offset.
const UART_DR: usize = 0x000;
/// Flag Register offset.
const UART_FR: usize = 0x018;
/// Integer Baud Rate Register offset.
const UART_IBRD: usize = 0x024;
/// Fractional Baud Rate Register offset.
const UART_FBRD: usize = 0x028;
/// Line Control Register offset.
const UART_LCR_H: usize = 0x02C;
/// Control Register offset.
const UART_CR: usize = 0x030;
/// Interrupt Mask Set/Clear Register offset.
const UART_IMSC: usize = 0x038;
// Flag register bits
/// Transmit FIFO full.
const FR_TXFF: u32 = 1 << 5;
/// Receive FIFO empty.
const FR_RXFE: u32 = 1 << 4;
/// UART busy.
const FR_BUSY: u32 = 1 << 3;
// Control register bits
/// UART enable.
const CR_UARTEN: u32 = 1 << 0;
/// Transmit enable.
const CR_TXE: u32 = 1 << 8;
/// Receive enable.
const CR_RXE: u32 = 1 << 9;
// Line control bits
/// Enable FIFOs.
const LCR_FEN: u32 = 1 << 4;
/// 8-bit word length (WLEN = 0b11).
const LCR_WLEN_8: u32 = 3 << 5;
/// Maximum number of iterations to spin waiting for the TX FIFO.
///
/// If the UART hardware is wedged or the MMIO mapping is broken, an
/// unbounded loop would hang the hypervisor. This timeout is generous
/// enough for real hardware (PL011 drains at baud rate) while still
/// preventing an infinite hang.
const UART_TX_TIMEOUT: u32 = 1_000_000;
/// Write a 32-bit value to a UART register.
///
/// # Safety
///
/// `offset` must be a valid PL011 register offset. The UART base address
/// must be mapped as device memory (nGnRnE) before calling this function.
unsafe
/// Read a 32-bit value from a UART register.
///
/// # Safety
///
/// `offset` must be a valid PL011 register offset. The UART base address
/// must be mapped as device memory before calling this function.
unsafe
/// Initialize the PL011 UART for 115200 baud, 8N1.
///
/// QEMU's PL011 emulation accepts output immediately, but this function
/// performs a proper initialization sequence for hardware correctness:
/// 1. Disable the UART
/// 2. Set baud rate (based on 24 MHz UARTCLK typical for virt)
/// 3. Configure line control (8 bits, FIFO enabled)
/// 4. Enable UART, TX, and RX
///
/// # Safety
///
/// Must be called at most once during boot. The UART MMIO region
/// (0x0900_0000) must be accessible (identity-mapped or pre-MMU).
pub unsafe
/// Write a single byte to the UART.
///
/// Spins until the transmit FIFO has space (up to [`UART_TX_TIMEOUT`]
/// iterations), then writes the byte. If the timeout is exceeded the
/// character is silently dropped to prevent hanging the hypervisor.
///
/// # Safety
///
/// The UART must have been initialized via [`uart_init`]. The MMIO
/// region must be accessible.
pub unsafe
/// Write a string to the UART, byte by byte.
///
/// Converts `\n` to `\r\n` for terminal compatibility.
///
/// # Safety
///
/// The UART must have been initialized via [`uart_init`].
pub unsafe
/// Write a 64-bit value as hexadecimal to the UART.
///
/// Outputs "0x" followed by 16 hex digits.
///
/// # Safety
///
/// The UART must have been initialized via [`uart_init`].
pub unsafe
/// Write a 32-bit value as hexadecimal to the UART.
///
/// Outputs "0x" followed by 8 hex digits.
///
/// # Safety
///
/// The UART must have been initialized via [`uart_init`].
pub unsafe