Skip to content

Commit 25f48f7

Browse files
committed
Remove used_slice concept from LegacyFrameAllocator
1 parent 3b07272 commit 25f48f7

File tree

5 files changed

+32
-86
lines changed

5 files changed

+32
-86
lines changed

bios/common/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub mod racy_cell;
55
#[cfg_attr(feature = "debug", derive(Debug))]
66
#[repr(C)]
77
pub struct BiosInfo {
8-
pub stage_3: Region,
98
pub stage_4: Region,
109
pub kernel: Region,
1110
pub ramdisk: Region,

bios/stage-2/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ fn start(disk_number: u16, partition_table_start: *const u8) -> ! {
145145
vesa_mode.enable().unwrap();
146146

147147
let mut info = BiosInfo {
148-
stage_3: Region {
149-
start: STAGE_3_DST as u64,
150-
len: stage_3_len,
151-
},
152148
stage_4: Region {
153149
start: stage_4_dst as u64,
154150
len: stage_4_len,

bios/stage-4/src/main.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
use crate::memory_descriptor::MemoryRegion;
55
use bootloader_api::info::{FrameBufferInfo, PixelFormat};
66
use bootloader_boot_config::{BootConfig, LevelFilter};
7-
use bootloader_x86_64_bios_common::{BiosFramebufferInfo, BiosInfo, E820MemoryRegion, Region};
8-
use bootloader_x86_64_common::legacy_memory_region::UsedMemorySlice;
7+
use bootloader_x86_64_bios_common::{BiosFramebufferInfo, BiosInfo, E820MemoryRegion};
98
use bootloader_x86_64_common::RawFrameBufferInfo;
109
use bootloader_x86_64_common::{
1110
legacy_memory_region::LegacyFrameAllocator, load_and_switch_to_kernel, Kernel, PageTables,
@@ -56,10 +55,9 @@ pub extern "C" fn _start(info: &mut BiosInfo) -> ! {
5655
};
5756
let kernel_size = info.kernel.len;
5857
let next_free_frame = PhysFrame::containing_address(PhysAddr::new(info.last_used_addr)) + 1;
59-
let mut frame_allocator = LegacyFrameAllocator::new_with_used_slices(
58+
let mut frame_allocator = LegacyFrameAllocator::new_starting_at(
6059
next_free_frame,
6160
memory_map.iter().copied().map(MemoryRegion),
62-
used_memory_slices(info),
6361
);
6462

6563
// We identity-mapped all memory, so the offset between physical and virtual addresses is 0
@@ -218,14 +216,6 @@ fn init_logger(
218216
framebuffer_info
219217
}
220218

221-
fn used_memory_slices(info: &BiosInfo) -> impl Iterator<Item = UsedMemorySlice> + Clone {
222-
// skip kernel and ramdisk because they are handled individually by the
223-
// uefi/bios common code
224-
[info.stage_3, info.stage_4, info.config_file]
225-
.into_iter()
226-
.map(|region| UsedMemorySlice::new_from_len(region.start, region.len))
227-
}
228-
229219
/// Creates page table abstraction types for both the bootloader and kernel page tables.
230220
fn create_page_tables(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> PageTables {
231221
// We identity-mapped all memory, so the offset between physical and virtual addresses is 0

common/src/legacy_memory_region.rs

+24-59
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ pub trait LegacyMemoryRegion: Copy + core::fmt::Debug {
4848
}
4949

5050
/// A physical frame allocator based on a BIOS or UEFI provided memory map.
51-
pub struct LegacyFrameAllocator<I, D, S> {
51+
pub struct LegacyFrameAllocator<I, D> {
5252
original: I,
5353
memory_map: I,
5454
current_descriptor: Option<D>,
5555
next_frame: PhysFrame,
5656
min_frame: PhysFrame,
57-
used_slices: S,
5857
}
5958

6059
/// Start address of the first frame that is not part of the lower 1MB of frames
6160
const LOWER_MEMORY_END_PAGE: u64 = 0x10_0000;
6261

63-
impl<I, D> LegacyFrameAllocator<I, D, Empty<UsedMemorySlice>>
62+
impl<I, D> LegacyFrameAllocator<I, D>
6463
where
6564
I: ExactSizeIterator<Item = D> + Clone,
6665
I::Item: LegacyMemoryRegion,
@@ -82,28 +81,16 @@ where
8281
/// before the given `frame` or `0x10000`(1MB) whichever is higher, there are use cases that require
8382
/// lower conventional memory access (Such as SMP SIPI).
8483
pub fn new_starting_at(frame: PhysFrame, memory_map: I) -> Self {
85-
Self::new_with_used_slices(frame, memory_map, empty())
86-
}
87-
}
88-
89-
impl<I, D, S> LegacyFrameAllocator<I, D, S>
90-
where
91-
I: ExactSizeIterator<Item = D> + Clone,
92-
I::Item: LegacyMemoryRegion,
93-
S: Iterator<Item = UsedMemorySlice> + Clone,
94-
{
95-
pub fn new_with_used_slices(start_frame: PhysFrame, memory_map: I, used_slices: S) -> Self {
9684
// skip frame 0 because the rust core library does not see 0 as a valid address
9785
// Also skip at least the lower 1MB of frames, there are use cases that require lower conventional memory access (Such as SMP SIPI).
9886
let lower_mem_end = PhysFrame::containing_address(PhysAddr::new(LOWER_MEMORY_END_PAGE));
99-
let frame = core::cmp::max(start_frame, lower_mem_end);
87+
let frame = core::cmp::max(frame, lower_mem_end);
10088
Self {
10189
original: memory_map.clone(),
10290
memory_map,
10391
current_descriptor: None,
10492
next_frame: frame,
10593
min_frame: frame,
106-
used_slices,
10794
}
10895
}
10996

@@ -154,10 +141,10 @@ where
154141

155142
/// Calculate the maximum number of regions produced by [Self::construct_memory_map]
156143
pub fn memory_map_max_region_count(&self) -> usize {
157-
// every used slice can split an original region into 3 new regions,
158-
// this means we need to reserve 2 extra spaces for each slice.
159-
// There are 3 additional slices (kernel, ramdisk and the bootloader heap)
160-
self.len() + (3 + self.used_slices.clone().count()) * 2
144+
// every used region can split an original region into 3 new regions,
145+
// this means we need to reserve 2 extra spaces for each region.
146+
// There are 3 used regions: kernel, ramdisk and the bootloader heap
147+
self.len() + 6
161148
}
162149

163150
/// Converts this type to a boot info memory map.
@@ -174,15 +161,22 @@ where
174161
ramdisk_slice_start: Option<PhysAddr>,
175162
ramdisk_slice_len: u64,
176163
) -> &mut [MemoryRegion] {
177-
let used_slices = Self::used_regions_iter(
178-
self.min_frame,
179-
self.next_frame,
180-
kernel_slice_start,
181-
kernel_slice_len,
182-
ramdisk_slice_start,
183-
ramdisk_slice_len,
184-
self.used_slices,
185-
);
164+
let used_slices = [
165+
UsedMemorySlice {
166+
start: self.min_frame.start_address().as_u64(),
167+
end: self.next_frame.start_address().as_u64(),
168+
},
169+
UsedMemorySlice::new_from_len(kernel_slice_start.as_u64(), kernel_slice_len),
170+
]
171+
.into_iter()
172+
.chain(
173+
ramdisk_slice_start
174+
.map(|start| UsedMemorySlice::new_from_len(start.as_u64(), ramdisk_slice_len)),
175+
)
176+
.map(|slice| UsedMemorySlice {
177+
start: align_down(slice.start, 0x1000),
178+
end: align_up(slice.end, 0x1000),
179+
});
186180

187181
let mut next_index = 0;
188182
for descriptor in self.original {
@@ -219,34 +213,6 @@ where
219213
}
220214
}
221215

222-
fn used_regions_iter(
223-
min_frame: PhysFrame,
224-
next_free: PhysFrame,
225-
kernel_slice_start: PhysAddr,
226-
kernel_slice_len: u64,
227-
ramdisk_slice_start: Option<PhysAddr>,
228-
ramdisk_slice_len: u64,
229-
used_slices: S,
230-
) -> impl Iterator<Item = UsedMemorySlice> + Clone {
231-
[
232-
UsedMemorySlice {
233-
start: min_frame.start_address().as_u64(),
234-
end: next_free.start_address().as_u64(),
235-
},
236-
UsedMemorySlice::new_from_len(kernel_slice_start.as_u64(), kernel_slice_len),
237-
]
238-
.into_iter()
239-
.chain(
240-
ramdisk_slice_start
241-
.map(|start| UsedMemorySlice::new_from_len(start.as_u64(), ramdisk_slice_len)),
242-
)
243-
.chain(used_slices)
244-
.map(|slice| UsedMemorySlice {
245-
start: align_down(slice.start, 0x1000),
246-
end: align_up(slice.end, 0x1000),
247-
})
248-
}
249-
250216
fn split_and_add_region<'a, U>(
251217
mut region: MemoryRegion,
252218
regions: &mut [MaybeUninit<MemoryRegion>],
@@ -325,11 +291,10 @@ where
325291
}
326292
}
327293

328-
unsafe impl<I, D, S> FrameAllocator<Size4KiB> for LegacyFrameAllocator<I, D, S>
294+
unsafe impl<I, D> FrameAllocator<Size4KiB> for LegacyFrameAllocator<I, D>
329295
where
330296
I: ExactSizeIterator<Item = D> + Clone,
331297
I::Item: LegacyMemoryRegion,
332-
S: Iterator<Item = UsedMemorySlice> + Clone,
333298
{
334299
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
335300
if let Some(current_descriptor) = self.current_descriptor {

common/src/lib.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use bootloader_api::{
1010
};
1111
use bootloader_boot_config::{BootConfig, LevelFilter};
1212
use core::{alloc::Layout, arch::asm, mem::MaybeUninit, slice};
13-
use legacy_memory_region::UsedMemorySlice;
1413
use level_4_entries::UsedLevel4Entries;
1514
use usize_conversions::FromUsize;
1615
use x86_64::{
@@ -124,17 +123,16 @@ impl<'a> Kernel<'a> {
124123
/// This function is a convenience function that first calls [`set_up_mappings`], then
125124
/// [`create_boot_info`], and finally [`switch_to_kernel`]. The given arguments are passed
126125
/// directly to these functions, so see their docs for more info.
127-
pub fn load_and_switch_to_kernel<I, D, S>(
126+
pub fn load_and_switch_to_kernel<I, D>(
128127
kernel: Kernel,
129128
boot_config: BootConfig,
130-
mut frame_allocator: LegacyFrameAllocator<I, D, S>,
129+
mut frame_allocator: LegacyFrameAllocator<I, D>,
131130
mut page_tables: PageTables,
132131
system_info: SystemInfo,
133132
) -> !
134133
where
135134
I: ExactSizeIterator<Item = D> + Clone,
136135
D: LegacyMemoryRegion,
137-
S: Iterator<Item = UsedMemorySlice> + Clone,
138136
{
139137
let config = kernel.config;
140138
let mut mappings = set_up_mappings(
@@ -170,9 +168,9 @@ where
170168
///
171169
/// This function reacts to unexpected situations (e.g. invalid kernel ELF file) with a panic, so
172170
/// errors are not recoverable.
173-
pub fn set_up_mappings<I, D, S>(
171+
pub fn set_up_mappings<I, D>(
174172
kernel: Kernel,
175-
frame_allocator: &mut LegacyFrameAllocator<I, D, S>,
173+
frame_allocator: &mut LegacyFrameAllocator<I, D>,
176174
page_tables: &mut PageTables,
177175
framebuffer: Option<&RawFrameBufferInfo>,
178176
config: &BootloaderConfig,
@@ -181,7 +179,6 @@ pub fn set_up_mappings<I, D, S>(
181179
where
182180
I: ExactSizeIterator<Item = D> + Clone,
183181
D: LegacyMemoryRegion,
184-
S: Iterator<Item = UsedMemorySlice> + Clone,
185182
{
186183
let kernel_page_table = &mut page_tables.kernel;
187184

@@ -469,18 +466,17 @@ pub struct Mappings {
469466
/// address space at the same address. This makes it possible to return a Rust
470467
/// reference that is valid in both address spaces. The necessary physical frames
471468
/// are taken from the given `frame_allocator`.
472-
pub fn create_boot_info<I, D, S>(
469+
pub fn create_boot_info<I, D>(
473470
config: &BootloaderConfig,
474471
boot_config: &BootConfig,
475-
mut frame_allocator: LegacyFrameAllocator<I, D, S>,
472+
mut frame_allocator: LegacyFrameAllocator<I, D>,
476473
page_tables: &mut PageTables,
477474
mappings: &mut Mappings,
478475
system_info: SystemInfo,
479476
) -> &'static mut BootInfo
480477
where
481478
I: ExactSizeIterator<Item = D> + Clone,
482479
D: LegacyMemoryRegion,
483-
S: Iterator<Item = UsedMemorySlice> + Clone,
484480
{
485481
log::info!("Allocate bootinfo");
486482

0 commit comments

Comments
 (0)