-
Notifications
You must be signed in to change notification settings - Fork 204
/
mod.rs
428 lines (381 loc) · 14 KB
/
mod.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use crate::binary::legacy_memory_region::{LegacyFrameAllocator, LegacyMemoryRegion};
use crate::boot_info::{BootInfo, FrameBuffer, FrameBufferInfo, TlsTemplate};
use crate::memory_region::MemoryRegion;
use core::{
mem::{self, MaybeUninit},
slice,
};
use level_4_entries::UsedLevel4Entries;
use parsed_config::CONFIG;
use usize_conversions::FromUsize;
use x86_64::{
registers,
structures::paging::{
FrameAllocator, Mapper, OffsetPageTable, Page, PageTableFlags, PageTableIndex, PhysFrame,
Size2MiB, Size4KiB,
},
PhysAddr, VirtAddr,
};
#[cfg(feature = "bios_bin")]
pub mod bios;
#[cfg(feature = "uefi_bin")]
pub mod uefi;
pub mod legacy_memory_region;
pub mod level_4_entries;
pub mod load_kernel;
pub mod logger;
// Contains the parsed configuration table from the kernel's Cargo.toml.
//
// The layout of the file is the following:
//
// ```
// mod parsed_config {
// pub const CONFIG: Config = Config { … };
// }
// ```
//
// The module file is created by the build script.
include!(concat!(env!("OUT_DIR"), "/bootloader_config.rs"));
const PAGE_SIZE: u64 = 4096;
pub fn init_logger(framebuffer: &'static mut [u8], info: FrameBufferInfo) {
let logger = logger::LOGGER.get_or_init(move || logger::LockedLogger::new(framebuffer, info));
log::set_logger(logger).expect("logger already set");
log::set_max_level(log::LevelFilter::Trace);
}
#[derive(Debug, Copy, Clone)]
pub struct SystemInfo {
pub framebuffer_addr: PhysAddr,
pub framebuffer_info: FrameBufferInfo,
pub rsdp_addr: Option<PhysAddr>,
}
pub fn load_and_switch_to_kernel<I, D>(
kernel_bytes: &[u8],
mut frame_allocator: LegacyFrameAllocator<I, D>,
mut page_tables: PageTables,
system_info: SystemInfo,
) -> !
where
I: ExactSizeIterator<Item = D> + Clone,
D: LegacyMemoryRegion,
{
let mut mappings = set_up_mappings(
kernel_bytes,
&mut frame_allocator,
&mut page_tables,
system_info.framebuffer_addr,
system_info.framebuffer_info.byte_len,
);
let (boot_info, two_frames) = create_boot_info(
frame_allocator,
&mut page_tables,
&mut mappings,
system_info,
);
switch_to_kernel(page_tables, mappings, boot_info, two_frames);
}
/// Sets up mappings for a kernel stack and the framebuffer
pub fn set_up_mappings<I, D>(
kernel_bytes: &[u8],
frame_allocator: &mut LegacyFrameAllocator<I, D>,
page_tables: &mut PageTables,
framebuffer_addr: PhysAddr,
framebuffer_size: usize,
) -> Mappings
where
I: ExactSizeIterator<Item = D> + Clone,
D: LegacyMemoryRegion,
{
let kernel_page_table = &mut page_tables.kernel;
// Enable support for the no-execute bit in page tables.
enable_nxe_bit();
// Make the kernel respect the write-protection bits even when in ring 0 by default
enable_write_protect_bit();
let (entry_point, tls_template, mut used_entries) =
load_kernel::load_kernel(kernel_bytes, kernel_page_table, frame_allocator)
.expect("no entry point");
log::info!("Entry point at: {:#x}", entry_point.as_u64());
// create a stack
let stack_start_addr = kernel_stack_start_location(&mut used_entries);
let stack_start: Page = Page::containing_address(stack_start_addr);
let stack_end = {
let end_addr = stack_start_addr + CONFIG.kernel_stack_size.unwrap_or(20 * PAGE_SIZE);
Page::containing_address(end_addr - 1u64)
};
for page in Page::range_inclusive(stack_start, stack_end) {
let frame = frame_allocator
.allocate_frame()
.expect("frame allocation failed when mapping a kernel stack");
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
unsafe { kernel_page_table.map_to(page, frame, flags, frame_allocator) }
.unwrap()
.flush();
}
// map framebuffer
let framebuffer_virt_addr = if CONFIG.map_framebuffer {
log::info!("Map framebuffer");
let framebuffer_start_frame: PhysFrame = PhysFrame::containing_address(framebuffer_addr);
let framebuffer_end_frame =
PhysFrame::containing_address(framebuffer_addr + framebuffer_size - 1u64);
let start_page = Page::containing_address(frame_buffer_location(&mut used_entries));
for (i, frame) in
PhysFrame::range_inclusive(framebuffer_start_frame, framebuffer_end_frame).enumerate()
{
let page = start_page + u64::from_usize(i);
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
unsafe { kernel_page_table.map_to(page, frame, flags, frame_allocator) }
.unwrap()
.flush();
}
let framebuffer_virt_addr = start_page.start_address();
Some(framebuffer_virt_addr)
} else {
None
};
let physical_memory_offset = if CONFIG.map_physical_memory {
log::info!("Map physical memory");
let offset = CONFIG
.physical_memory_offset
.map(VirtAddr::new)
.unwrap_or_else(|| used_entries.get_free_address());
let start_frame = PhysFrame::containing_address(PhysAddr::new(0));
let max_phys = frame_allocator.max_phys_addr();
let end_frame: PhysFrame<Size2MiB> = PhysFrame::containing_address(max_phys - 1u64);
for frame in PhysFrame::range_inclusive(start_frame, end_frame) {
let page = Page::containing_address(offset + frame.start_address().as_u64());
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
unsafe { kernel_page_table.map_to(page, frame, flags, frame_allocator) }
.unwrap()
.ignore();
}
Some(offset)
} else {
None
};
let recursive_index = if CONFIG.map_page_table_recursively {
log::info!("Map page table recursively");
let index = CONFIG
.recursive_index
.map(PageTableIndex::new)
.unwrap_or_else(|| used_entries.get_free_entry());
let entry = &mut kernel_page_table.level_4_table()[index];
if !entry.is_unused() {
panic!(
"Could not set up recursive mapping: index {} already in use",
u16::from(index)
);
}
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
entry.set_frame(page_tables.kernel_level_4_frame, flags);
Some(index)
} else {
None
};
Mappings {
framebuffer: framebuffer_virt_addr,
entry_point,
stack_end,
used_entries,
physical_memory_offset,
recursive_index,
tls_template,
}
}
pub struct Mappings {
pub entry_point: VirtAddr,
pub stack_end: Page,
pub used_entries: UsedLevel4Entries,
pub framebuffer: Option<VirtAddr>,
pub physical_memory_offset: Option<VirtAddr>,
pub recursive_index: Option<PageTableIndex>,
pub tls_template: Option<TlsTemplate>,
}
/// Allocates and initializes the boot info struct and the memory map
pub fn create_boot_info<I, D>(
mut frame_allocator: LegacyFrameAllocator<I, D>,
page_tables: &mut PageTables,
mappings: &mut Mappings,
system_info: SystemInfo,
) -> (&'static mut BootInfo, TwoFrames)
where
I: ExactSizeIterator<Item = D> + Clone,
D: LegacyMemoryRegion,
{
log::info!("Allocate bootinfo");
// allocate and map space for the boot info
let (boot_info, memory_regions) = {
let boot_info_addr = boot_info_location(&mut mappings.used_entries);
let boot_info_end = boot_info_addr + mem::size_of::<BootInfo>();
let memory_map_regions_addr =
boot_info_end.align_up(u64::from_usize(mem::align_of::<MemoryRegion>()));
let regions = frame_allocator.len() + 1; // one region might be split into used/unused
let memory_map_regions_end =
memory_map_regions_addr + regions * mem::size_of::<MemoryRegion>();
let start_page = Page::containing_address(boot_info_addr);
let end_page = Page::containing_address(memory_map_regions_end - 1u64);
for page in Page::range_inclusive(start_page, end_page) {
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
let frame = frame_allocator
.allocate_frame()
.expect("frame allocation for boot info failed");
unsafe {
page_tables
.kernel
.map_to(page, frame, flags, &mut frame_allocator)
}
.unwrap()
.flush();
// we need to be able to access it too
unsafe {
page_tables
.bootloader
.map_to(page, frame, flags, &mut frame_allocator)
}
.unwrap()
.flush();
}
let boot_info: &'static mut MaybeUninit<BootInfo> =
unsafe { &mut *boot_info_addr.as_mut_ptr() };
let memory_regions: &'static mut [MaybeUninit<MemoryRegion>] =
unsafe { slice::from_raw_parts_mut(memory_map_regions_addr.as_mut_ptr(), regions) };
(boot_info, memory_regions)
};
// reserve two unused frames for context switch
let two_frames = TwoFrames::new(&mut frame_allocator);
log::info!("Create Memory Map");
// build memory map
let memory_regions = frame_allocator.construct_memory_map(memory_regions);
log::info!("Create bootinfo");
// create boot info
let boot_info = boot_info.write(BootInfo {
version_major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
version_minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
version_patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
pre_release: !env!("CARGO_PKG_VERSION_PRE").is_empty(),
memory_regions,
framebuffer: mappings.framebuffer.map(|addr| FrameBuffer {
buffer_start: addr.as_u64(),
buffer_byte_len: system_info.framebuffer_info.byte_len,
info: system_info.framebuffer_info,
}),
physical_memory_offset: mappings.physical_memory_offset.map(VirtAddr::as_u64),
recursive_index: mappings.recursive_index.map(Into::into),
rsdp_addr: system_info.rsdp_addr.map(|addr| addr.as_u64()),
tls_template: mappings.tls_template,
_non_exhaustive: (),
});
(boot_info, two_frames)
}
/// Switches to the kernel address space and jumps to the kernel entry point.
pub fn switch_to_kernel(
page_tables: PageTables,
mappings: Mappings,
boot_info: &'static mut BootInfo,
two_frames: TwoFrames,
) -> ! {
let PageTables {
kernel_level_4_frame,
kernel: kernel_page_table,
..
} = page_tables;
let addresses = Addresses {
page_table: kernel_level_4_frame,
stack_top: mappings.stack_end.start_address(),
entry_point: mappings.entry_point,
boot_info,
};
log::info!(
"Jumping to kernel entry point at {:?}",
addresses.entry_point
);
unsafe {
context_switch(addresses, kernel_page_table, two_frames);
}
}
pub struct PageTables {
pub bootloader: OffsetPageTable<'static>,
pub kernel: OffsetPageTable<'static>,
pub kernel_level_4_frame: PhysFrame,
}
/// Performs the actual context switch
///
/// This function should stay small because it needs to be identity-mapped.
unsafe fn context_switch(
addresses: Addresses,
mut kernel_page_table: OffsetPageTable,
mut frame_allocator: impl FrameAllocator<Size4KiB>,
) -> ! {
// identity-map current and next frame, so that we don't get an immediate pagefault
// after switching the active page table
let current_addr = PhysAddr::new(registers::read_rip());
let current_frame: PhysFrame = PhysFrame::containing_address(current_addr);
for frame in PhysFrame::range_inclusive(current_frame, current_frame + 1) {
unsafe {
kernel_page_table.identity_map(frame, PageTableFlags::PRESENT, &mut frame_allocator)
}
.unwrap()
.flush();
}
// we don't need the kernel page table anymore
mem::drop(kernel_page_table);
// do the context switch
unsafe {
asm!(
"mov cr3, {}; mov rsp, {}; push 0; jmp {}",
in(reg) addresses.page_table.start_address().as_u64(),
in(reg) addresses.stack_top.as_u64(),
in(reg) addresses.entry_point.as_u64(),
in("rdi") addresses.boot_info as *const _ as usize,
);
}
unreachable!();
}
pub struct Addresses {
page_table: PhysFrame,
stack_top: VirtAddr,
entry_point: VirtAddr,
boot_info: &'static mut crate::boot_info::BootInfo,
}
pub struct TwoFrames {
frames: [Option<PhysFrame>; 2],
}
impl TwoFrames {
pub fn new(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> Self {
TwoFrames {
frames: [
Some(frame_allocator.allocate_frame().unwrap()),
Some(frame_allocator.allocate_frame().unwrap()),
],
}
}
}
unsafe impl FrameAllocator<Size4KiB> for TwoFrames {
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
self.frames.iter_mut().find_map(|f| f.take())
}
}
fn boot_info_location(used_entries: &mut UsedLevel4Entries) -> VirtAddr {
CONFIG
.boot_info_address
.map(VirtAddr::new)
.unwrap_or_else(|| used_entries.get_free_address())
}
fn frame_buffer_location(used_entries: &mut UsedLevel4Entries) -> VirtAddr {
CONFIG
.framebuffer_address
.map(VirtAddr::new)
.unwrap_or_else(|| used_entries.get_free_address())
}
fn kernel_stack_start_location(used_entries: &mut UsedLevel4Entries) -> VirtAddr {
CONFIG
.kernel_stack_address
.map(VirtAddr::new)
.unwrap_or_else(|| used_entries.get_free_address())
}
fn enable_nxe_bit() {
use x86_64::registers::control::{Efer, EferFlags};
unsafe { Efer::update(|efer| *efer |= EferFlags::NO_EXECUTE_ENABLE) }
}
fn enable_write_protect_bit() {
use x86_64::registers::control::{Cr0, Cr0Flags};
unsafe { Cr0::update(|cr0| *cr0 |= Cr0Flags::WRITE_PROTECT) };
}