-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathmain.rs
300 lines (269 loc) · 9.94 KB
/
main.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
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
#![no_std]
#![no_main]
use crate::memory_descriptor::MemoryRegion;
use bootloader_api::info::{FrameBufferInfo, PixelFormat};
use bootloader_boot_config::{BootConfig, LevelFilter};
use bootloader_x86_64_bios_common::{BiosFramebufferInfo, BiosInfo, E820MemoryRegion};
use bootloader_x86_64_common::RawFrameBufferInfo;
use bootloader_x86_64_common::{
legacy_memory_region::LegacyFrameAllocator, load_and_switch_to_kernel, Kernel, PageTables,
SystemInfo,
};
use core::{cmp, slice};
use usize_conversions::usize_from;
use x86_64::structures::paging::{FrameAllocator, OffsetPageTable};
use x86_64::structures::paging::{
Mapper, PageTable, PageTableFlags, PhysFrame, Size2MiB, Size4KiB,
};
use x86_64::{PhysAddr, VirtAddr};
const GIGABYTE: u64 = 4096 * 512 * 512;
mod memory_descriptor;
#[no_mangle]
#[link_section = ".start"]
pub extern "C" fn _start(info: &mut BiosInfo) -> ! {
let memory_map: &mut [E820MemoryRegion] = unsafe {
core::slice::from_raw_parts_mut(
info.memory_map_addr as *mut _,
info.memory_map_len.try_into().unwrap(),
)
};
memory_map.sort_unstable_by_key(|e| e.start_addr);
let max_phys_addr = {
let max = memory_map
.iter()
.map(|r| {
log::info!("start: {:#x}, len: {:#x}", r.start_addr, r.len);
r.start_addr + r.len
})
.max()
.expect("no physical memory regions found");
// Don't consider addresses > 4GiB when determining the maximum physical
// address for the bootloader, as we are in protected mode and cannot
// address more than 4 GiB of memory anyway.
cmp::min(max, 4 * GIGABYTE)
};
let kernel_start = {
assert!(info.kernel.start != 0, "kernel start address must be set");
PhysAddr::new(info.kernel.start)
};
let kernel_size = info.kernel.len;
let next_free_frame = PhysFrame::containing_address(PhysAddr::new(info.last_used_addr)) + 1;
let mut frame_allocator = LegacyFrameAllocator::new_starting_at(
next_free_frame,
memory_map.iter().copied().map(MemoryRegion),
);
// We identity-mapped all memory, so the offset between physical and virtual addresses is 0
let phys_offset = VirtAddr::new(0);
let mut bootloader_page_table = {
let frame = x86_64::registers::control::Cr3::read().0;
let table: *mut PageTable = (phys_offset + frame.start_address().as_u64()).as_mut_ptr();
unsafe { OffsetPageTable::new(&mut *table, phys_offset) }
};
// identity-map remaining physical memory (first 10 gigabytes are already identity-mapped)
{
let start_frame: PhysFrame<Size2MiB> =
PhysFrame::containing_address(PhysAddr::new(GIGABYTE * 10));
let end_frame = PhysFrame::containing_address(PhysAddr::new(max_phys_addr - 1));
for frame in PhysFrame::range_inclusive(start_frame, end_frame) {
let flusher = unsafe {
bootloader_page_table
.identity_map(
frame,
PageTableFlags::PRESENT | PageTableFlags::WRITABLE,
&mut frame_allocator,
)
.unwrap()
};
// skip flushing the entry from the TLB for now, as we will
// flush the entire TLB at the end of the loop.
flusher.ignore();
}
}
// once all the physical memory is mapped, flush the TLB by reloading the
// CR3 register.
//
// we perform a single flush here rather than flushing each individual entry as
// it's mapped using `invlpg`, for efficiency.
x86_64::instructions::tlb::flush_all();
let page_tables = create_page_tables(&mut frame_allocator);
let kernel_slice = {
let ptr = kernel_start.as_u64() as *const u8;
unsafe { slice::from_raw_parts(ptr, usize_from(kernel_size)) }
};
let kernel = Kernel::parse(kernel_slice);
let mut config_file_slice: Option<&[u8]> = None;
if info.config_file.len != 0 {
config_file_slice = {
let ptr = info.config_file.start as *mut u8;
unsafe {
Some(slice::from_raw_parts_mut(
ptr,
usize_from(info.config_file.len),
))
}
};
}
let mut error_loading_config: Option<serde_json_core::de::Error> = None;
let mut config: BootConfig = match config_file_slice
.map(serde_json_core::from_slice)
.transpose()
{
Ok(data) => data.unwrap_or_default().0,
Err(err) => {
error_loading_config = Some(err);
Default::default()
}
};
#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_height.is_none() {
config.frame_buffer.minimum_framebuffer_height =
kernel.config.frame_buffer.minimum_framebuffer_height;
}
#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_width.is_none() {
config.frame_buffer.minimum_framebuffer_width =
kernel.config.frame_buffer.minimum_framebuffer_width;
}
let framebuffer_info = init_logger(
info.framebuffer,
config.log_level,
config.frame_buffer_logging,
config.serial_logging,
);
if let Some(err) = error_loading_config {
log::warn!("Failed to deserialize the config file {:?}", err);
}
log::info!("4th Stage");
log::info!("{info:x?}");
log::info!("BIOS boot");
let system_info = SystemInfo {
framebuffer: Some(RawFrameBufferInfo {
addr: PhysAddr::new(info.framebuffer.region.start),
info: framebuffer_info,
}),
rsdp_addr: detect_rsdp(),
ramdisk_addr: match info.ramdisk.len {
0 => None,
_ => Some(info.ramdisk.start),
},
ramdisk_len: info.ramdisk.len,
rt_table_addr: None,
};
load_and_switch_to_kernel(kernel, config, frame_allocator, page_tables, system_info);
}
fn init_logger(
info: BiosFramebufferInfo,
log_level: LevelFilter,
frame_buffer_logger_status: bool,
serial_logger_status: bool,
) -> FrameBufferInfo {
let framebuffer_info = FrameBufferInfo {
byte_len: info.region.len.try_into().unwrap(),
width: info.width.into(),
height: info.height.into(),
pixel_format: match info.pixel_format {
bootloader_x86_64_bios_common::PixelFormat::Rgb => PixelFormat::Rgb,
bootloader_x86_64_bios_common::PixelFormat::Bgr => PixelFormat::Bgr,
bootloader_x86_64_bios_common::PixelFormat::Unknown {
red_position,
green_position,
blue_position,
} => PixelFormat::Unknown {
red_position,
green_position,
blue_position,
},
},
bytes_per_pixel: info.bytes_per_pixel.into(),
stride: info.stride.into(),
};
let framebuffer = unsafe {
core::slice::from_raw_parts_mut(
info.region.start as *mut u8,
info.region.len.try_into().unwrap(),
)
};
bootloader_x86_64_common::init_logger(
framebuffer,
framebuffer_info,
log_level,
frame_buffer_logger_status,
serial_logger_status,
);
framebuffer_info
}
/// Creates page table abstraction types for both the bootloader and kernel page tables.
fn create_page_tables(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> PageTables {
// We identity-mapped all memory, so the offset between physical and virtual addresses is 0
let phys_offset = VirtAddr::new(0);
// copy the currently active level 4 page table, because it might be read-only
let bootloader_page_table = {
let frame = x86_64::registers::control::Cr3::read().0;
let table: *mut PageTable = (phys_offset + frame.start_address().as_u64()).as_mut_ptr();
unsafe { OffsetPageTable::new(&mut *table, phys_offset) }
};
// create a new page table hierarchy for the kernel
let (kernel_page_table, kernel_level_4_frame) = {
// get an unused frame for new level 4 page table
let frame: PhysFrame = frame_allocator.allocate_frame().expect("no unused frames");
log::info!("New page table at: {frame:#?}");
// get the corresponding virtual address
let addr = phys_offset + frame.start_address().as_u64();
// initialize a new page table
let ptr: *mut PageTable = addr.as_mut_ptr();
unsafe { ptr.write(PageTable::new()) };
let level_4_table = unsafe { &mut *ptr };
(
unsafe { OffsetPageTable::new(level_4_table, phys_offset) },
frame,
)
};
PageTables {
bootloader: bootloader_page_table,
kernel: kernel_page_table,
kernel_level_4_frame,
}
}
fn detect_rsdp() -> Option<PhysAddr> {
use core::ptr::NonNull;
use rsdp::{
handler::{AcpiHandler, PhysicalMapping},
Rsdp,
};
#[derive(Clone)]
struct IdentityMapped;
impl AcpiHandler for IdentityMapped {
unsafe fn map_physical_region<T>(
&self,
physical_address: usize,
size: usize,
) -> PhysicalMapping<Self, T> {
PhysicalMapping::new(
physical_address,
NonNull::new(physical_address as *mut _).unwrap(),
size,
size,
Self,
)
}
fn unmap_physical_region<T>(_region: &PhysicalMapping<Self, T>) {}
}
unsafe {
Rsdp::search_for_on_bios(IdentityMapped)
.ok()
.map(|mapping| PhysAddr::new(mapping.physical_start() as u64))
}
}
#[cfg(target_os = "none")]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
unsafe {
bootloader_x86_64_common::logger::LOGGER
.get()
.map(|l| l.force_unlock())
};
log::error!("{info}");
loop {
unsafe { core::arch::asm!("cli; hlt") };
}
}