Skip to content

Commit 3dd634b

Browse files
vm: refactor handle_pf_file
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent fc74645 commit 3dd634b

File tree

12 files changed

+207
-198
lines changed

12 files changed

+207
-198
lines changed

src/Cargo.lock

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aero_kernel/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ bit_field = "0.10.1"
2929
log = "0.4.14"
3030
raw-cpuid = "10.0.0"
3131
xmas-elf = "0.8.0"
32-
paste = "1.0.5"
3332
hashbrown = "0.11.2"
3433
rustc-demangle = "0.1.20"
35-
lru = "0.6.5"
3634
stivale-boot = "0.2.6"
3735
intrusive-collections = "0.9.2"
3836
aml = "0.15.0"

src/aero_kernel/src/arch/x86_64/interrupts/exceptions.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ pub(super) fn page_fault(stack: &mut InterruptErrorStack) {
9595
log::error!("stack: {:#x?}", stack);
9696
};
9797

98-
if accessed_address < userland_last_address && scheduler::is_initialized() {
98+
if accessed_address < userland_last_address && scheduler::is_initialized()
99+
|| stack.stack.iret.is_user()
100+
{
99101
let signal = scheduler::get_scheduler()
100102
.current_task()
101103
.vm
@@ -105,21 +107,19 @@ pub(super) fn page_fault(stack: &mut InterruptErrorStack) {
105107
log::error!("Segmentation fault");
106108
print_info();
107109

108-
if stack.stack.iret.is_user() {
109-
let task = scheduler::get_scheduler().current_task();
110-
111-
log::error!(
112-
"process: (pid={}, pid={})",
113-
task.tid().as_usize(),
114-
task.pid().as_usize()
115-
);
110+
let task = scheduler::get_scheduler().current_task();
116111

117-
log::error!(
118-
"process: (path=`{}`)",
119-
task.path()
120-
.expect("userland application does not have a path set")
121-
);
122-
}
112+
log::error!(
113+
"process: (pid={}, pid={})",
114+
task.tid().as_usize(),
115+
task.pid().as_usize()
116+
);
117+
118+
log::error!(
119+
"process: (path=`{}`)",
120+
task.path()
121+
.expect("userland application does not have a path set")
122+
);
123123

124124
scheduler::get_scheduler().current_task().vm.log();
125125
scheduler::get_scheduler().current_task().file_table.log();

src/aero_kernel/src/arch/x86_64/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ static PAGING_TAG: Stivale5LevelPagingHeaderTag = Stivale5LevelPagingHeaderTag::
6969
.next((&UNMAP_NULL as *const StivaleUnmapNullHeaderTag).cast());
7070

7171
/// The stivale2 specification says we need to define a "header structure".
72-
/// This structure needs to reside in the .stivale2hdr ELF section in order
73-
/// for the bootloader to find it. We use the #[linker_section] and #[used] macros to
72+
/// This structure needs to reside in the `.stivale2hdr` ELF section in order
73+
/// for the bootloader to find it. We use the `#[linker_section]` and `#[used]` macros to
7474
/// tell the compiler to put the following structure in said section.
7575
#[link_section = ".stivale2hdr"]
7676
#[no_mangle]

src/aero_kernel/src/arch/x86_64/task.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl ArchTask {
246246
envv: Option<ExecArgs>,
247247
) -> Result<(), MapToError<Size4KiB>> {
248248
let address_space = AddressSpace::new()?;
249+
let loaded_binary = vm.load_bin(executable).expect("exec: failed to load ELF");
249250

250251
// mmap the userland stack...
251252
vm.mmap(
@@ -261,8 +262,6 @@ impl ArchTask {
261262

262263
address_space.switch(); // Perform the address space switch
263264

264-
let loaded_binary = vm.load_bin(executable).expect("exec: failed to load ELF");
265-
266265
self.context = Unique::dangling();
267266
self.address_space = address_space; // Update the address space reference
268267

src/aero_kernel/src/fs/devfs.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ impl INodeInterface for DevINode {
121121
self.0.inode().read_at(offset, buffer)
122122
}
123123

124-
fn mmap(&self, offset: usize, flags: MMapFlags) -> Result<PhysAddr> {
125-
self.0.inode().mmap(offset, flags)
124+
fn mmap(&self, offset: usize, size: usize, flags: MMapFlags) -> Result<PhysFrame> {
125+
self.0.inode().mmap(offset, size, flags)
126126
}
127127

128128
fn ioctl(&self, command: usize, arg: usize) -> Result<usize> {
@@ -278,12 +278,12 @@ impl INodeInterface for DevFb {
278278
.expect("/dev/fb: terminal not initialized")
279279
}
280280

281-
fn mmap(&self, offset: usize, flags: MMapFlags) -> Result<PhysAddr> {
281+
fn mmap(&self, offset: usize, size: usize, flags: MMapFlags) -> Result<PhysFrame> {
282282
let rinfo = crate::rendy::get_rendy_info();
283283

284284
// Make sure we are in bounds.
285285
if offset > rinfo.byte_len || offset + Size4KiB::SIZE as usize > rinfo.byte_len {
286-
return Ok(PhysAddr::zero());
286+
return Ok(PhysFrame::containing_address(PhysAddr::zero()));
287287
}
288288

289289
crate::rendy::DEBUG_RENDY
@@ -300,23 +300,17 @@ impl INodeInterface for DevFb {
300300

301301
let fb_phys_ptr = fb_ptr.sub(crate::PHYSICAL_MEMORY_OFFSET.as_u64() as usize);
302302

303-
Ok(PhysAddr::new_unchecked(fb_phys_ptr as u64))
303+
Ok(PhysFrame::containing_address(PhysAddr::new_unchecked(
304+
fb_phys_ptr as u64,
305+
)))
304306
} else {
305307
let fb = lock.get_framebuffer();
306308

307309
// This is a private file mapping.
308-
let private_cp: PhysFrame = FRAME_ALLOCATOR
309-
.allocate_frame()
310-
.expect("/dev/fb: failed to allocate frame for private file mapping");
310+
let private_cp: PhysFrame = FRAME_ALLOCATOR.allocate_frame().unwrap();
311+
private_cp.as_slice_mut()[..size].copy_from_slice(&fb[offset..offset + size]);
311312

312-
let private_phys = private_cp.start_address();
313-
let private_virt = crate::PHYSICAL_MEMORY_OFFSET + private_phys.as_u64();
314-
let private_ptr = private_virt.as_mut_ptr();
315-
316-
core::slice::from_raw_parts_mut(private_ptr, Size4KiB::SIZE as usize)
317-
.copy_from_slice(&fb[offset..offset + Size4KiB::SIZE as usize]);
318-
319-
Ok(private_phys)
313+
Ok(private_cp)
320314
}
321315
})
322316
.expect("/dev/fb: terminal not initialized")

src/aero_kernel/src/fs/inode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use alloc::sync::Weak;
2929
use alloc::vec::Vec;
3030
use spin::Once;
3131

32-
use crate::mem::paging::PhysAddr;
32+
use crate::mem::paging::PhysFrame;
3333
use crate::utils::sync::Mutex;
3434
use crate::utils::Downcastable;
3535

@@ -137,7 +137,7 @@ pub trait INodeInterface: Send + Sync + Downcastable {
137137
Err(FileSystemError::NotSupported)
138138
}
139139

140-
fn mmap(&self, _offset: usize, _flags: MMapFlags) -> Result<PhysAddr> {
140+
fn mmap(&self, _offset: usize, _size: usize, _flags: MMapFlags) -> Result<PhysFrame> {
141141
Err(FileSystemError::NotSupported)
142142
}
143143

src/aero_kernel/src/fs/ramfs.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use alloc::sync::{Arc, Weak};
3131
use alloc::vec::Vec;
3232
use spin::RwLock;
3333

34-
use crate::mem::paging::PhysAddr;
34+
use crate::mem::paging::*;
3535
use crate::utils::downcast;
3636
use crate::utils::sync::Mutex;
3737

@@ -311,18 +311,28 @@ impl INodeInterface for LockedRamINode {
311311
}
312312
}
313313

314-
fn mmap(&self, offset: usize, flags: MMapFlags) -> Result<PhysAddr> {
314+
fn mmap(&self, offset: usize, size: usize, flags: MMapFlags) -> Result<PhysFrame> {
315315
let this = self.0.read();
316316

317317
match &this.contents {
318318
FileContents::Device(dev) => {
319319
let device = dev.clone();
320320
drop(this);
321321

322-
device.mmap(offset, flags)
322+
device.mmap(offset, size, flags)
323323
}
324324

325-
// TODO: Support memory mapping ramfs files:
325+
FileContents::StaticContent(contents) => {
326+
// TODO: Support shared static content ramfs file mappings.
327+
assert!(!flags.contains(MMapFlags::MAP_SHARED));
328+
329+
let private_cp: PhysFrame = unsafe { FRAME_ALLOCATOR.allocate_frame().unwrap() };
330+
private_cp.as_slice_mut()[..size].copy_from_slice(&contents[offset..offset + size]);
331+
332+
Ok(private_cp)
333+
}
334+
335+
// TODO: Support other memory mapping ramfs files:
326336
_ => Err(FileSystemError::NotSupported),
327337
}
328338
}

src/aero_kernel/src/mem/paging/frame.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,8 @@ impl VmFrame {
552552
pub fn dec_ref_count(&self) {
553553
let mut this = self.lock.lock();
554554

555-
if this.use_count > 0 {
556-
this.use_count -= 1;
557-
}
555+
assert!(this.use_count > 0);
556+
this.use_count -= 1;
558557
}
559558

560559
pub fn inc_ref_count(&self) {

src/aero_kernel/src/mem/paging/page.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ impl<S: PageSize> PhysFrame<S> {
296296
pub fn start_address(self) -> PhysAddr {
297297
self.start_address
298298
}
299+
300+
pub fn as_slice_mut<T>(&self) -> &mut [T] {
301+
assert!(core::mem::size_of::<T>() < S::SIZE as usize);
302+
303+
// TODO: Move the physical to virtual address translation to the `PhysAddr` structure.
304+
let virt = unsafe { crate::PHYSICAL_MEMORY_OFFSET + self.start_address.as_u64() };
305+
let ptr = virt.as_mut_ptr::<T>();
306+
307+
// SAFETY: The size of the slice is equal to the size of the frame and the
308+
// pointer is valid.
309+
unsafe {
310+
core::slice::from_raw_parts_mut(ptr, S::SIZE as usize / core::mem::size_of::<T>())
311+
}
312+
}
299313
}
300314

301315
impl<S: PageSize> fmt::Debug for PhysFrame<S> {

0 commit comments

Comments
 (0)