Skip to content

Commit 6d0156f

Browse files
committed
Revert "Add new UnsafePhysFrame type and use it in Mapper::map_to"
This reverts commit 0c8756f.
1 parent 26f332a commit 6d0156f

File tree

5 files changed

+41
-77
lines changed

5 files changed

+41
-77
lines changed

src/structures/paging/frame_alloc.rs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,18 @@
11
//! Traits for abstracting away frame allocation and deallocation.
22
3-
use crate::structures::paging::{PageSize, PhysFrame, Size4KiB};
4-
use core::ops::{Deref, DerefMut};
3+
use crate::structures::paging::{PageSize, PhysFrame};
54

65
/// A trait for types that can allocate a frame of memory.
76
///
87
/// This trait is unsafe to implement because the implementer must guarantee that
98
/// the `allocate_frame` method returns only unique unused frames.
109
pub unsafe trait FrameAllocator<S: PageSize> {
1110
/// Allocate a frame of the appropriate size and return it if possible.
12-
fn allocate_frame(&mut self) -> Option<UnusedPhysFrame<S>>;
11+
fn allocate_frame(&mut self) -> Option<PhysFrame<S>>;
1312
}
1413

1514
/// A trait for types that can deallocate a frame of memory.
1615
pub trait FrameDeallocator<S: PageSize> {
1716
/// Deallocate the given frame of memory.
18-
fn deallocate_frame(&mut self, frame: UnusedPhysFrame<S>);
19-
}
20-
21-
/// Represents a physical frame that is not used for any mapping.
22-
#[derive(Debug)]
23-
pub struct UnusedPhysFrame<S: PageSize = Size4KiB>(PhysFrame<S>);
24-
25-
impl<S: PageSize> UnusedPhysFrame<S> {
26-
/// Creates a new UnusedPhysFrame from the given frame.
27-
///
28-
/// ## Safety
29-
///
30-
/// This method is unsafe because the caller must guarantee
31-
/// that the given frame is unused.
32-
pub unsafe fn new(frame: PhysFrame<S>) -> Self {
33-
Self(frame)
34-
}
35-
36-
/// Returns the physical frame as `PhysFrame` type.
37-
pub fn frame(self) -> PhysFrame<S> {
38-
self.0
39-
}
40-
}
41-
42-
impl<S: PageSize> Deref for UnusedPhysFrame<S> {
43-
type Target = PhysFrame<S>;
44-
45-
fn deref(&self) -> &Self::Target {
46-
&self.0
47-
}
48-
}
49-
50-
impl<S: PageSize> DerefMut for UnusedPhysFrame<S> {
51-
fn deref_mut(&mut self) -> &mut Self::Target {
52-
&mut self.0
53-
}
17+
fn deallocate_frame(&mut self, frame: PhysFrame<S>);
5418
}

src/structures/paging/mapper/mapped_page_table.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
4141
fn map_to_1gib<A>(
4242
&mut self,
4343
page: Page<Size1GiB>,
44-
frame: UnusedPhysFrame<Size1GiB>,
44+
frame: PhysFrame<Size1GiB>,
4545
flags: PageTableFlags,
4646
allocator: &mut A,
4747
) -> Result<MapperFlush<Size1GiB>, MapToError<Size1GiB>>
@@ -66,7 +66,7 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
6666
fn map_to_2mib<A>(
6767
&mut self,
6868
page: Page<Size2MiB>,
69-
frame: UnusedPhysFrame<Size2MiB>,
69+
frame: PhysFrame<Size2MiB>,
7070
flags: PageTableFlags,
7171
allocator: &mut A,
7272
) -> Result<MapperFlush<Size2MiB>, MapToError<Size2MiB>>
@@ -94,7 +94,7 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
9494
fn map_to_4kib<A>(
9595
&mut self,
9696
page: Page<Size4KiB>,
97-
frame: UnusedPhysFrame<Size4KiB>,
97+
frame: PhysFrame<Size4KiB>,
9898
flags: PageTableFlags,
9999
allocator: &mut A,
100100
) -> Result<MapperFlush<Size4KiB>, MapToError<Size4KiB>>
@@ -115,17 +115,17 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
115115
if !p1[page.p1_index()].is_unused() {
116116
return Err(MapToError::PageAlreadyMapped(frame));
117117
}
118-
p1[page.p1_index()].set_frame(frame.frame(), flags);
118+
p1[page.p1_index()].set_frame(frame, flags);
119119

120120
Ok(MapperFlush::new(page))
121121
}
122122
}
123123

124124
impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {
125-
fn map_to<A>(
125+
unsafe fn map_to<A>(
126126
&mut self,
127127
page: Page<Size1GiB>,
128-
frame: UnusedPhysFrame<Size1GiB>,
128+
frame: PhysFrame<Size1GiB>,
129129
flags: PageTableFlags,
130130
allocator: &mut A,
131131
) -> Result<MapperFlush<Size1GiB>, MapToError<Size1GiB>>
@@ -195,10 +195,10 @@ impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {
195195
}
196196

197197
impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {
198-
fn map_to<A>(
198+
unsafe fn map_to<A>(
199199
&mut self,
200200
page: Page<Size2MiB>,
201-
frame: UnusedPhysFrame<Size2MiB>,
201+
frame: PhysFrame<Size2MiB>,
202202
flags: PageTableFlags,
203203
allocator: &mut A,
204204
) -> Result<MapperFlush<Size2MiB>, MapToError<Size2MiB>>
@@ -276,10 +276,10 @@ impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {
276276
}
277277

278278
impl<'a, P: PhysToVirt> Mapper<Size4KiB> for MappedPageTable<'a, P> {
279-
fn map_to<A>(
279+
unsafe fn map_to<A>(
280280
&mut self,
281281
page: Page<Size4KiB>,
282-
frame: UnusedPhysFrame<Size4KiB>,
282+
frame: PhysFrame<Size4KiB>,
283283
flags: PageTableFlags,
284284
allocator: &mut A,
285285
) -> Result<MapperFlush<Size4KiB>, MapToError<Size4KiB>>
@@ -465,10 +465,7 @@ impl<P: PhysToVirt> PageTableWalker<P> {
465465

466466
if entry.is_unused() {
467467
if let Some(frame) = allocator.allocate_frame() {
468-
entry.set_frame(
469-
frame.frame(),
470-
PageTableFlags::PRESENT | PageTableFlags::WRITABLE,
471-
);
468+
entry.set_frame(frame, PageTableFlags::PRESENT | PageTableFlags::WRITABLE);
472469
created = true;
473470
} else {
474471
return Err(PageTableCreateError::FrameAllocationFailed);

src/structures/paging/mapper/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ pub use self::mapped_page_table::{MappedPageTable, PhysToVirt};
55
pub use self::{offset_page_table::OffsetPageTable, recursive_page_table::RecursivePageTable};
66

77
use crate::structures::paging::{
8-
frame_alloc::{FrameAllocator, UnusedPhysFrame},
9-
page_table::PageTableFlags,
10-
Page, PageSize, PhysFrame, Size1GiB, Size2MiB, Size4KiB,
8+
frame_alloc::FrameAllocator, page_table::PageTableFlags, Page, PageSize, PhysFrame, Size1GiB,
9+
Size2MiB, Size4KiB,
1110
};
1211
use crate::{PhysAddr, VirtAddr};
1312

@@ -83,10 +82,13 @@ pub trait Mapper<S: PageSize> {
8382
///
8483
/// This function might need additional physical frames to create new page tables. These
8584
/// frames are allocated from the `allocator` argument. At most three frames are required.
86-
fn map_to<A>(
85+
///
86+
/// This function is unsafe because the caller must guarantee that passed `frame` is
87+
/// unused, i.e. not used for any other mappings.
88+
unsafe fn map_to<A>(
8789
&mut self,
8890
page: Page<S>,
89-
frame: UnusedPhysFrame<S>,
91+
frame: PhysFrame<S>,
9092
flags: PageTableFlags,
9193
frame_allocator: &mut A,
9294
) -> Result<MapperFlush<S>, MapToError<S>>
@@ -116,11 +118,12 @@ pub trait Mapper<S: PageSize> {
116118
///
117119
/// ## Safety
118120
///
119-
/// TODO: Should this function be safe?
121+
/// This function is unsafe because the caller must guarantee that the passed `frame` is
122+
/// unused, i.e. not used for any other mappings.
120123
#[inline]
121124
unsafe fn identity_map<A>(
122125
&mut self,
123-
frame: UnusedPhysFrame<S>,
126+
frame: PhysFrame<S>,
124127
flags: PageTableFlags,
125128
frame_allocator: &mut A,
126129
) -> Result<MapperFlush<S>, MapToError<S>>

src/structures/paging/mapper/offset_page_table.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ impl PhysToVirt for PhysOffset {
5353
// delegate all trait implementations to inner
5454

5555
impl<'a> Mapper<Size1GiB> for OffsetPageTable<'a> {
56-
fn map_to<A>(
56+
unsafe fn map_to<A>(
5757
&mut self,
5858
page: Page<Size1GiB>,
59-
frame: UnusedPhysFrame<Size1GiB>,
59+
frame: PhysFrame<Size1GiB>,
6060
flags: PageTableFlags,
6161
allocator: &mut A,
6262
) -> Result<MapperFlush<Size1GiB>, MapToError<Size1GiB>>
@@ -91,10 +91,10 @@ impl<'a> Mapper<Size1GiB> for OffsetPageTable<'a> {
9191

9292
impl<'a> Mapper<Size2MiB> for OffsetPageTable<'a> {
9393
#[inline]
94-
fn map_to<A>(
94+
unsafe fn map_to<A>(
9595
&mut self,
9696
page: Page<Size2MiB>,
97-
frame: UnusedPhysFrame<Size2MiB>,
97+
frame: PhysFrame<Size2MiB>,
9898
flags: PageTableFlags,
9999
allocator: &mut A,
100100
) -> Result<MapperFlush<Size2MiB>, MapToError<Size2MiB>>
@@ -129,10 +129,10 @@ impl<'a> Mapper<Size2MiB> for OffsetPageTable<'a> {
129129

130130
impl<'a> Mapper<Size4KiB> for OffsetPageTable<'a> {
131131
#[inline]
132-
fn map_to<A>(
132+
unsafe fn map_to<A>(
133133
&mut self,
134134
page: Page<Size4KiB>,
135-
frame: UnusedPhysFrame<Size4KiB>,
135+
frame: PhysFrame<Size4KiB>,
136136
flags: PageTableFlags,
137137
allocator: &mut A,
138138
) -> Result<MapperFlush<Size4KiB>, MapToError<Size4KiB>>

src/structures/paging/mapper/recursive_page_table.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a> RecursivePageTable<'a> {
113113

114114
if entry.is_unused() {
115115
if let Some(frame) = allocator.allocate_frame() {
116-
entry.set_frame(frame.frame(), Flags::PRESENT | Flags::WRITABLE);
116+
entry.set_frame(frame, Flags::PRESENT | Flags::WRITABLE);
117117
created = true;
118118
} else {
119119
return Err(MapToError::FrameAllocationFailed);
@@ -141,7 +141,7 @@ impl<'a> RecursivePageTable<'a> {
141141
fn map_to_1gib<A>(
142142
&mut self,
143143
page: Page<Size1GiB>,
144-
frame: UnusedPhysFrame<Size1GiB>,
144+
frame: PhysFrame<Size1GiB>,
145145
flags: PageTableFlags,
146146
allocator: &mut A,
147147
) -> Result<MapperFlush<Size1GiB>, MapToError<Size1GiB>>
@@ -167,7 +167,7 @@ impl<'a> RecursivePageTable<'a> {
167167
fn map_to_2mib<A>(
168168
&mut self,
169169
page: Page<Size2MiB>,
170-
frame: UnusedPhysFrame<Size2MiB>,
170+
frame: PhysFrame<Size2MiB>,
171171
flags: PageTableFlags,
172172
allocator: &mut A,
173173
) -> Result<MapperFlush<Size2MiB>, MapToError<Size2MiB>>
@@ -196,7 +196,7 @@ impl<'a> RecursivePageTable<'a> {
196196
fn map_to_4kib<A>(
197197
&mut self,
198198
page: Page<Size4KiB>,
199-
frame: UnusedPhysFrame<Size4KiB>,
199+
frame: PhysFrame<Size4KiB>,
200200
flags: PageTableFlags,
201201
allocator: &mut A,
202202
) -> Result<MapperFlush<Size4KiB>, MapToError<Size4KiB>>
@@ -217,17 +217,17 @@ impl<'a> RecursivePageTable<'a> {
217217
if !p1[page.p1_index()].is_unused() {
218218
return Err(MapToError::PageAlreadyMapped(frame));
219219
}
220-
p1[page.p1_index()].set_frame(frame.frame(), flags);
220+
p1[page.p1_index()].set_frame(frame, flags);
221221

222222
Ok(MapperFlush::new(page))
223223
}
224224
}
225225

226226
impl<'a> Mapper<Size1GiB> for RecursivePageTable<'a> {
227-
fn map_to<A>(
227+
unsafe fn map_to<A>(
228228
&mut self,
229229
page: Page<Size1GiB>,
230-
frame: UnusedPhysFrame<Size1GiB>,
230+
frame: PhysFrame<Size1GiB>,
231231
flags: PageTableFlags,
232232
allocator: &mut A,
233233
) -> Result<MapperFlush<Size1GiB>, MapToError<Size1GiB>>
@@ -310,10 +310,10 @@ impl<'a> Mapper<Size1GiB> for RecursivePageTable<'a> {
310310

311311
impl<'a> Mapper<Size2MiB> for RecursivePageTable<'a> {
312312
#[inline]
313-
fn map_to<A>(
313+
unsafe fn map_to<A>(
314314
&mut self,
315315
page: Page<Size2MiB>,
316-
frame: UnusedPhysFrame<Size2MiB>,
316+
frame: PhysFrame<Size2MiB>,
317317
flags: PageTableFlags,
318318
allocator: &mut A,
319319
) -> Result<MapperFlush<Size2MiB>, MapToError<Size2MiB>>
@@ -415,10 +415,10 @@ impl<'a> Mapper<Size2MiB> for RecursivePageTable<'a> {
415415
}
416416

417417
impl<'a> Mapper<Size4KiB> for RecursivePageTable<'a> {
418-
fn map_to<A>(
418+
unsafe fn map_to<A>(
419419
&mut self,
420420
page: Page<Size4KiB>,
421-
frame: UnusedPhysFrame<Size4KiB>,
421+
frame: PhysFrame<Size4KiB>,
422422
flags: PageTableFlags,
423423
allocator: &mut A,
424424
) -> Result<MapperFlush<Size4KiB>, MapToError<Size4KiB>>

0 commit comments

Comments
 (0)