Skip to content

Commit d8acfbc

Browse files
committed
Removed more platform-dependent parts.
1 parent 803700f commit d8acfbc

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

src/paging/frame_alloc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Traits for abstracting away frame allocation and deallocation.
22
3-
use addr::{FrameWith, PhysicalAddress};
3+
use addr::*;
44
/// A trait for types that can allocate a frame of memory.
55
pub trait FrameAllocatorFor<P: PhysicalAddress> {
66
/// Allocate a frame of the appropriate size and return it if possible.
@@ -14,20 +14,24 @@ pub trait FrameDeallocatorFor<P: PhysicalAddress> {
1414
}
1515

1616
/// Polyfill for default use cases.
17-
use crate::addr::*;
17+
18+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
1819
pub trait FrameAllocator{
1920
fn alloc(&mut self) -> Option<Frame>;
2021
}
22+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
2123
pub trait FrameDeallocator{
2224
fn dealloc(&mut self, frame: Frame);
2325
}
2426

27+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
2528
impl<T: FrameAllocator> FrameAllocatorFor<PhysAddr> for T{
2629
#[inline]
2730
fn alloc(&mut self) -> Option<Frame>{
2831
FrameAllocator::alloc(self)
2932
}
3033
}
34+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
3135
impl<T: FrameDeallocator> FrameDeallocatorFor<PhysAddr> for T{
3236
#[inline]
3337
fn dealloc(&mut self, frame: Frame){

src/paging/multi_level.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ impl<'a, V: VirtualAddress + AddressL2, FL: MapperFlushable> Rv32PageTableWith<'
2424
&mut self,
2525
p2_index: usize,
2626
allocator: &mut impl FrameAllocatorFor<<Self as Mapper>::P>,
27-
) -> Result<&mut PageTable, MapToError> {
27+
) -> Result<&mut PageTableX32, MapToError> {
2828
if self.root_table[p2_index].is_unused() {
2929
let frame = allocator
3030
.alloc()
3131
.ok_or(MapToError::FrameAllocationFailed)?;
3232
self.root_table[p2_index].set(frame.clone(), F::VALID);
33-
let p1_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
33+
let p1_table: &mut PageTableX32 = unsafe { frame.as_kernel_mut(self.linear_offset) };
3434
p1_table.zero();
3535
Ok(p1_table)
3636
} else {
3737
let frame = self.root_table[p2_index].frame::<PhysAddrSv32>();
38-
let p1_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
38+
let p1_table: &mut PageTableX32 = unsafe { frame.as_kernel_mut(self.linear_offset) };
3939
Ok(p1_table)
4040
}
4141
}
@@ -66,12 +66,12 @@ impl<'a, V: VirtualAddress + AddressL2, FL: MapperFlushable> Mapper
6666
fn unmap(
6767
&mut self,
6868
page: <Self as MapperExt>::Page,
69-
) -> Result<(<Self as MapperExt>::Frame, Self::MapperFlush), UnmapError> {
69+
) -> Result<(<Self as MapperExt>::Frame, Self::MapperFlush), UnmapError<<Self as Mapper>::P>> {
7070
if self.root_table[page.p2_index()].is_unused() {
7171
return Err(UnmapError::PageNotMapped);
7272
}
7373
let p1_frame = self.root_table[page.p2_index()].frame::<PhysAddrSv32>();
74-
let p1_table: &mut PageTable = unsafe { p1_frame.as_kernel_mut(self.linear_offset) };
74+
let p1_table: &mut PageTableX32 = unsafe { p1_frame.as_kernel_mut(self.linear_offset) };
7575
let p1_entry = &mut p1_table[page.p1_index()];
7676
if !p1_entry.flags().contains(F::VALID) {
7777
return Err(UnmapError::PageNotMapped);
@@ -116,13 +116,13 @@ impl<'a, V: VirtualAddress + AddressL3, FL: MapperFlushable> Rv39PageTableWith<'
116116
p3_index: usize,
117117
p2_index: usize,
118118
allocator: &mut impl FrameAllocatorFor<<Self as Mapper>::P>,
119-
) -> Result<&mut PageTable, MapToError> {
119+
) -> Result<&mut PageTableX64, MapToError> {
120120
let p2_table = if self.root_table[p3_index].is_unused() {
121121
let frame = allocator
122122
.alloc()
123123
.ok_or(MapToError::FrameAllocationFailed)?;
124124
self.root_table[p3_index].set(frame.clone(), F::VALID);
125-
let p2_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
125+
let p2_table: &mut PageTableX64 = unsafe { frame.as_kernel_mut(self.linear_offset) };
126126
p2_table.zero();
127127
p2_table
128128
} else {
@@ -134,12 +134,12 @@ impl<'a, V: VirtualAddress + AddressL3, FL: MapperFlushable> Rv39PageTableWith<'
134134
.alloc()
135135
.ok_or(MapToError::FrameAllocationFailed)?;
136136
p2_table[p2_index].set(frame.clone(), F::VALID);
137-
let p1_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
137+
let p1_table: &mut PageTableX64 = unsafe { frame.as_kernel_mut(self.linear_offset) };
138138
p1_table.zero();
139139
Ok(p1_table)
140140
} else {
141141
let frame = p2_table[p2_index].frame::<PhysAddrSv39>();
142-
let p1_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
142+
let p1_table: &mut PageTableX64 = unsafe { frame.as_kernel_mut(self.linear_offset) };
143143
Ok(p1_table)
144144
}
145145
}
@@ -170,18 +170,18 @@ impl<'a, V: VirtualAddress + AddressL3, FL: MapperFlushable> Mapper
170170
fn unmap(
171171
&mut self,
172172
page: <Self as MapperExt>::Page,
173-
) -> Result<(<Self as MapperExt>::Frame, Self::MapperFlush), UnmapError> {
173+
) -> Result<(<Self as MapperExt>::Frame, Self::MapperFlush), UnmapError<<Self as Mapper>::P>> {
174174
if self.root_table[page.p3_index()].is_unused() {
175175
return Err(UnmapError::PageNotMapped);
176176
}
177177
let p2_frame = self.root_table[page.p3_index()].frame::<PhysAddrSv39>();
178-
let p2_table: &mut PageTable = unsafe { p2_frame.as_kernel_mut(self.linear_offset) };
178+
let p2_table: &mut PageTableX64 = unsafe { p2_frame.as_kernel_mut(self.linear_offset) };
179179

180180
if p2_table[page.p2_index()].is_unused() {
181181
return Err(UnmapError::PageNotMapped);
182182
}
183183
let p1_frame = p2_table[page.p2_index()].frame::<PhysAddrSv39>();
184-
let p1_table: &mut PageTable = unsafe { p1_frame.as_kernel_mut(self.linear_offset) };
184+
let p1_table: &mut PageTableX64 = unsafe { p1_frame.as_kernel_mut(self.linear_offset) };
185185
let p1_entry = &mut p1_table[page.p1_index()];
186186
if !p1_entry.flags().contains(F::VALID) {
187187
return Err(UnmapError::PageNotMapped);
@@ -233,13 +233,13 @@ impl<'a, V: VirtualAddress + AddressL4, FL: MapperFlushable> Rv48PageTableWith<'
233233
p3_index: usize,
234234
p2_index: usize,
235235
allocator: &mut impl FrameAllocatorFor<<Self as Mapper>::P>,
236-
) -> Result<&mut PageTable, MapToError> {
236+
) -> Result<&mut PageTableX64, MapToError> {
237237
let p3_table = if self.root_table[p4_index].is_unused() {
238238
let frame = allocator
239239
.alloc()
240240
.ok_or(MapToError::FrameAllocationFailed)?;
241241
self.root_table[p4_index].set(frame.clone(), F::VALID);
242-
let p3_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
242+
let p3_table: &mut PageTableX64 = unsafe { frame.as_kernel_mut(self.linear_offset) };
243243
p3_table.zero();
244244
p3_table
245245
} else {
@@ -252,7 +252,7 @@ impl<'a, V: VirtualAddress + AddressL4, FL: MapperFlushable> Rv48PageTableWith<'
252252
.alloc()
253253
.ok_or(MapToError::FrameAllocationFailed)?;
254254
p3_table[p3_index].set(frame.clone(), F::VALID);
255-
let p2_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
255+
let p2_table: &mut PageTableX64 = unsafe { frame.as_kernel_mut(self.linear_offset) };
256256
p2_table.zero();
257257
p2_table
258258
} else {
@@ -265,12 +265,12 @@ impl<'a, V: VirtualAddress + AddressL4, FL: MapperFlushable> Rv48PageTableWith<'
265265
.alloc()
266266
.ok_or(MapToError::FrameAllocationFailed)?;
267267
p2_table[p2_index].set(frame.clone(), F::VALID);
268-
let p1_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
268+
let p1_table: &mut PageTableX64 = unsafe { frame.as_kernel_mut(self.linear_offset) };
269269
p1_table.zero();
270270
Ok(p1_table)
271271
} else {
272272
let frame = p2_table[p2_index].frame::<PhysAddrSv48>();
273-
let p1_table: &mut PageTable = unsafe { frame.as_kernel_mut(self.linear_offset) };
273+
let p1_table: &mut PageTableX64 = unsafe { frame.as_kernel_mut(self.linear_offset) };
274274
Ok(p1_table)
275275
}
276276
}
@@ -306,24 +306,24 @@ impl<'a, V: VirtualAddress + AddressL4, FL: MapperFlushable> Mapper
306306
fn unmap(
307307
&mut self,
308308
page: <Self as MapperExt>::Page,
309-
) -> Result<(<Self as MapperExt>::Frame, Self::MapperFlush), UnmapError> {
309+
) -> Result<(<Self as MapperExt>::Frame, Self::MapperFlush), UnmapError<<Self as Mapper>::P>> {
310310
if self.root_table[page.p4_index()].is_unused() {
311311
return Err(UnmapError::PageNotMapped);
312312
}
313313
let p3_frame = self.root_table[page.p4_index()].frame::<PhysAddrSv48>();
314-
let p3_table: &mut PageTable = unsafe { p3_frame.as_kernel_mut(self.linear_offset) };
314+
let p3_table: &mut PageTableX64 = unsafe { p3_frame.as_kernel_mut(self.linear_offset) };
315315

316316
if p3_table[page.p3_index()].is_unused() {
317317
return Err(UnmapError::PageNotMapped);
318318
}
319319
let p2_frame = p3_table[page.p3_index()].frame::<PhysAddrSv48>();
320-
let p2_table: &mut PageTable = unsafe { p2_frame.as_kernel_mut(self.linear_offset) };
320+
let p2_table: &mut PageTableX64 = unsafe { p2_frame.as_kernel_mut(self.linear_offset) };
321321

322322
if p2_table[page.p2_index()].is_unused() {
323323
return Err(UnmapError::PageNotMapped);
324324
}
325325
let p1_frame = p2_table[page.p2_index()].frame::<PhysAddrSv48>();
326-
let p1_table: &mut PageTable = unsafe { p1_frame.as_kernel_mut(self.linear_offset) };
326+
let p1_table: &mut PageTableX64 = unsafe { p1_frame.as_kernel_mut(self.linear_offset) };
327327
let p1_entry = &mut p1_table[page.p1_index()];
328328
if !p1_entry.flags().contains(F::VALID) {
329329
return Err(UnmapError::PageNotMapped);

src/paging/recursive.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use super::frame_alloc::*;
2-
use super::page_table::{PageTableFlags as F, *};
2+
use super::page_table::*;
33
use addr::*;
44

5+
#[cfg(any(riscv32, riscv64))]
6+
use super::page_table::PageTableFlags as F;
7+
58
pub trait Mapper {
69
type P: PhysicalAddress;
710
type V: VirtualAddress;
@@ -25,7 +28,7 @@ pub trait Mapper {
2528
fn unmap(
2629
&mut self,
2730
page: PageWith<Self::V>,
28-
) -> Result<(FrameWith<Self::P>, Self::MapperFlush), UnmapError>;
31+
) -> Result<(FrameWith<Self::P>, Self::MapperFlush), UnmapError<<Self as Mapper>::P>>;
2932

3033
/// Get the reference of the specified `page` entry
3134
fn ref_entry(&mut self, page: PageWith<Self::V>) -> Result<&mut Self::Entry, FlagUpdateError>;
@@ -43,7 +46,7 @@ pub trait Mapper {
4346
}
4447

4548
/// Return the frame that the specified page is mapped to.
46-
fn translate_page(&mut self, page: PageWith<Self::V>) -> Option<Frame> {
49+
fn translate_page(&mut self, page: PageWith<Self::V>) -> Option<FrameWith<Self::P>> {
4750
match self.ref_entry(page) {
4851
Ok(e) => {
4952
if e.is_unused() {
@@ -108,14 +111,14 @@ pub enum MapToError {
108111

109112
/// An error indicating that an `unmap` call failed.
110113
#[derive(Debug)]
111-
pub enum UnmapError {
114+
pub enum UnmapError<P: PhysicalAddress> {
112115
/// An upper level page table entry has the `HUGE_PAGE` flag set, which means that the
113116
/// given page is part of a huge page and can't be freed individually.
114117
ParentEntryHugePage,
115118
/// The given page is not mapped to a physical frame.
116119
PageNotMapped,
117120
/// The page table entry for the given page points to an invalid physical address.
118-
InvalidFrameAddress(PhysAddr),
121+
InvalidFrameAddress(P),
119122
}
120123

121124
/// An error indicating that an `update_flags` call failed.
@@ -125,11 +128,13 @@ pub enum FlagUpdateError {
125128
PageNotMapped,
126129
}
127130

131+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
128132
struct TempMap<'a> {
129133
entry: &'a mut PageTableEntry,
130134
pt_addr: VirtAddr,
131135
}
132136

137+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
133138
impl<'a> TempMap<'a> {
134139
#[cfg(riscv32)]
135140
unsafe fn new(rec_idx: usize) -> Self {
@@ -183,6 +188,7 @@ pub enum PageTableType {
183188
/// A recursive page table is a last level page table with an entry mapped to the table itself.
184189
///
185190
/// This struct implements the `Mapper` trait.
191+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
186192
pub struct RecursivePageTable<'a> {
187193
root_table: &'a mut PageTable,
188194
/// Recursive index as `R`
@@ -447,7 +453,7 @@ impl<'a> Mapper for RecursivePageTable<'a> {
447453
Ok(MapperFlush::new(page))
448454
}
449455

450-
fn unmap(&mut self, page: Page) -> Result<(Frame, MapperFlush), UnmapError> {
456+
fn unmap(&mut self, page: Page) -> Result<(Frame, MapperFlush), UnmapError<<Self as Mapper>::P>> {
451457
if self.root_table[page.p2_index()].is_unused() {
452458
return Err(UnmapError::PageNotMapped);
453459
}
@@ -503,7 +509,7 @@ impl<'a> Mapper for RecursivePageTable<'a> {
503509
Ok(MapperFlush::new(page))
504510
}
505511

506-
fn unmap(&mut self, page: Page) -> Result<(Frame, MapperFlush), UnmapError> {
512+
fn unmap(&mut self, page: Page) -> Result<(Frame, MapperFlush), UnmapError<<Self as Mapper>::P>> {
507513
let p1_table = self.ref_p1(page).ok_or(UnmapError::PageNotMapped)?;
508514
let p1_entry = &mut p1_table[page.p1_index()];
509515
if !p1_entry.flags().contains(F::VALID) {

0 commit comments

Comments
 (0)