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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#[cfg(feature = "acpi")]
pub mod acpi;
pub mod apic;
pub mod gdt;
pub mod idt;
pub mod irq;
#[cfg(feature = "pci")]
pub mod pci;
#[cfg(feature = "pci")]
mod pci_ids;
pub mod percore;
pub mod pic;
pub mod pit;
pub mod processor;
pub mod scheduler;
pub mod serial;
#[cfg(not(test))]
mod smp_boot_code;
#[cfg(not(test))]
mod start;
pub mod switch;
pub mod systemtime;
#[cfg(feature = "vga")]
mod vga;
use arch::x86_64::kernel::percore::*;
use arch::x86_64::kernel::serial::SerialPort;
#[cfg(feature = "newlib")]
use core::slice;
use core::{intrinsics, ptr};
use environment;
use kernel_message_buffer;
const SERIAL_PORT_BAUDRATE: u32 = 115_200;
#[repr(C)]
pub struct BootInfo {
magic_number: u32,
version: u32,
base: u64,
limit: u64,
image_size: u64,
tls_start: u64,
tls_filesz: u64,
tls_memsz: u64,
current_stack_address: u64,
current_percore_address: u64,
host_logical_addr: u64,
boot_gtod: u64,
mb_info: u64,
cmdline: u64,
cmdsize: u64,
cpu_freq: u32,
boot_processor: u32,
cpu_online: u32,
possible_cpus: u32,
current_boot_id: u32,
uartport: u16,
single_kernel: u8,
uhyve: u8,
hcip: [u8; 4],
hcgateway: [u8; 4],
hcmask: [u8; 4],
}
#[cfg(test)]
static mut BOOT_INFO: *mut BootInfo = ptr::null_mut();
#[cfg(all(not(test), not(feature = "newlib")))]
#[link_section = ".data"]
static mut BOOT_INFO: *mut BootInfo = ptr::null_mut();
#[cfg(all(not(test), feature = "newlib"))]
#[link_section = ".mboot"]
static mut BOOT_INFO: *mut BootInfo = ptr::null_mut();
static mut COM1: SerialPort = SerialPort::new(0x3f8);
pub fn has_ipdevice() -> bool {
let ip = unsafe { intrinsics::volatile_load(&(*BOOT_INFO).hcip) };
if ip[0] == 255 && ip[1] == 255 && ip[2] == 255 && ip[3] == 255 {
false
} else {
true
}
}
#[no_mangle]
#[cfg(not(feature = "newlib"))]
pub fn uhyve_get_ip() -> [u8; 4] {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).hcip) }
}
#[no_mangle]
#[cfg(not(feature = "newlib"))]
pub fn uhyve_get_gateway() -> [u8; 4] {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).hcgateway) }
}
#[no_mangle]
#[cfg(not(feature = "newlib"))]
pub fn uhyve_get_mask() -> [u8; 4] {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).hcmask) }
}
#[no_mangle]
#[cfg(feature = "newlib")]
pub unsafe extern "C" fn uhyve_get_ip(ip: *mut u8) {
let data = intrinsics::volatile_load(&(*BOOT_INFO).hcip);
slice::from_raw_parts_mut(ip, 4).copy_from_slice(&data);
}
#[no_mangle]
#[cfg(feature = "newlib")]
pub unsafe extern "C" fn uhyve_get_gateway(gw: *mut u8) {
let data = intrinsics::volatile_load(&(*BOOT_INFO).hcgateway);
slice::from_raw_parts_mut(gw, 4).copy_from_slice(&data);
}
#[no_mangle]
#[cfg(feature = "newlib")]
pub unsafe extern "C" fn uhyve_get_mask(mask: *mut u8) {
let data = intrinsics::volatile_load(&(*BOOT_INFO).hcmask);
slice::from_raw_parts_mut(mask, 4).copy_from_slice(&data);
}
pub fn get_base_address() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).base) as usize }
}
pub fn get_image_size() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).image_size) as usize }
}
pub fn get_limit() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).limit) as usize }
}
pub fn get_tls_start() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).tls_start) as usize }
}
pub fn get_tls_filesz() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).tls_filesz) as usize }
}
pub fn get_tls_memsz() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).tls_memsz) as usize }
}
pub fn get_mbinfo() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).mb_info) as usize }
}
pub fn get_processor_count() -> u32 {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).cpu_online) as u32 }
}
pub fn is_uhyve() -> bool {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).uhyve) & 0x1 == 0x1 }
}
pub fn is_uhyve_with_pci() -> bool {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).uhyve) & 0x3 == 0x3 }
}
pub fn is_single_kernel() -> bool {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).single_kernel) != 0 }
}
pub fn get_cmdsize() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).cmdsize) as usize }
}
pub fn get_cmdline() -> usize {
unsafe { intrinsics::volatile_load(&(*BOOT_INFO).cmdline) as usize }
}
pub fn message_output_init() {
percore::init();
unsafe {
COM1.port_address = intrinsics::volatile_load(&(*BOOT_INFO).uartport);
}
if environment::is_single_kernel() {
unsafe {
COM1.init(SERIAL_PORT_BAUDRATE);
}
}
}
#[cfg(all(test, not(target_os = "windows")))]
pub fn output_message_byte(byte: u8) {
extern "C" {
fn write(fd: i32, buf: *const u8, count: usize) -> isize;
}
unsafe {
let _ = write(2, &byte as *const _, 1);
}
}
#[cfg(all(test, target_os = "windows"))]
pub fn output_message_byte(byte: u8) {
extern "C" {
fn _write(fd: i32, buf: *const u8, count: u32) -> isize;
}
unsafe {
let _ = _write(2, &byte as *const _, 1);
}
}
#[test]
fn test_output() {
output_message_byte('t' as u8);
output_message_byte('e' as u8);
output_message_byte('s' as u8);
output_message_byte('t' as u8);
output_message_byte('\n' as u8);
}
#[cfg(not(test))]
pub fn output_message_byte(byte: u8) {
if environment::is_single_kernel() {
unsafe {
COM1.write_byte(byte);
}
#[cfg(feature = "vga")]
vga::write_byte(byte);
} else {
kernel_message_buffer::write_byte(byte);
}
}
#[cfg(not(test))]
pub fn boot_processor_init() {
processor::detect_features();
processor::configure();
if cfg!(feature = "vga") && environment::is_single_kernel() && !environment::is_uhyve() {
#[cfg(feature = "vga")]
vga::init();
}
::mm::init();
::mm::print_information();
environment::init();
gdt::init();
gdt::add_current_core();
idt::install();
pic::init();
irq::install();
processor::detect_frequency();
processor::print_information();
systemtime::init();
if environment::is_single_kernel() {
if is_uhyve_with_pci() || !is_uhyve() {
#[cfg(feature = "pci")]
pci::init();
#[cfg(feature = "pci")]
pci::print_information();
}
if !environment::is_uhyve() {
#[cfg(feature = "acpi")]
acpi::init();
}
}
apic::init();
scheduler::install_timer_handler();
finish_processor_init();
irq::enable();
}
#[cfg(not(test))]
pub fn boot_application_processors() {
apic::boot_application_processors();
apic::print_information();
}
#[cfg(not(test))]
pub fn application_processor_init() {
percore::init();
processor::configure();
gdt::add_current_core();
idt::install();
apic::init_x2apic();
apic::init_local_apic();
irq::enable();
finish_processor_init();
}
fn finish_processor_init() {
if environment::is_uhyve() {
apic::add_local_apic_id(core_id() as u8);
apic::init_next_processor_variables(core_id() + 1);
}
unsafe {
let _ = intrinsics::atomic_xadd(&mut (*BOOT_INFO).cpu_online as *mut u32, 1);
}
}