Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions kernel/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 29 additions & 27 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,42 @@ edition = "2021"
# Treat warnings as a build error.
strict = []


[dependencies]
arrayvec = { version = "0.7.6", default-features = false }
async-trait = "0.1.86"
bitflags = { version = "2.8", default-features = false }
bytes = { version = "1.10.0", default-features = false }
lazy_static = { version = "1.5.0", features = ["spin_no_std"] }
limine = "0.3.1"
raw-cpuid = "11.3.0"
spin = "0.9.8"
uart_16550 = "0.3.2"
x86_64 = "0.15.2"
talc = "4.4.2"
goblin = { version = "0.9.3", default-features = false, features = [
crossbeam-queue = { version = "0.3.12", default-features = false, features = [
"alloc",
"elf64",
"elf32",
"endian_fd",
] }
rand = { version = "0.8.3", features = ["small_rng"], default-features = false }
fontdue = { version = "0.9.3", default-features = false, features = [
"hashbrown",
] }
futures = { version = "0.3", default-features = false, features = [
"alloc",
"async-await",
] }
crossbeam-queue = { version = "0.3.12", default-features = false, features = [
futures-util = { version = "0.3.31", default-features = false, features = [
"alloc",
"async-await",
"async-await-macro",
"futures-macro",
] }
arrayvec = { version = "0.7.6", default-features = false }
goblin = { version = "0.9.3", default-features = false, features = [
"alloc",
"elf64",
"elf32",
"endian_fd",
] }
lazy_static = { version = "1.5.0", features = ["spin_no_std"] }
limine = "0.3.1"
log = { version = "0.4.25", default-features = false }
async-trait = "0.1.86"
num-derive = { version = "0.4", default-features = false }
num-traits = { version = "0.2", default-features = false }
pc-keyboard = "0.8.0"
ps2 = "0.2.0"
rand = { version = "0.8.3", features = ["small_rng"], default-features = false }
raw-cpuid = "11.3.0"
smoltcp = { version = "0.10.0", default-features = false, features = [
"alloc",
"proto-ipv4",
Expand All @@ -44,16 +52,10 @@ smoltcp = { version = "0.10.0", default-features = false, features = [
"socket-icmp",
"socket-dns",
"socket-udp",
"socket-tcp",
] }
fontdue = { version = "0.9.3", default-features = false, features = [
"hashbrown",
] }
spin = "0.9.8"
talc = "4.4.2"
uart_16550 = "0.3.2"
x86_64 = "0.15.2"
zerocopy = { version = "0.8", features = ["derive"] }
pc-keyboard = "0.8.0"
futures-util = { version = "0.3.31", default-features = false, features = [
"alloc",
"async-await",
"async-await-macro",
"futures-macro",
] }
ps2 = "0.2.0"
2 changes: 1 addition & 1 deletion kernel/limage_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gdb-terminal = { args = ["-nographic", "-s", "-S"] }
gdb-gui = { args = ["-s", "-S"] }

[test]
timeout_secs = 30
timeout_secs = 60
success_exit_code = 33
no_reboot = true
extra_args = [
Expand Down
3 changes: 3 additions & 0 deletions kernel/src/constants/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub const SYSCALL_MPROTECT: u32 = 10;
pub const SYSCALL_MUNMAP: u32 = 11;
pub const SYSCALL_FORK: u32 = 5;
pub const SYSCALL_WAIT: u32 = 6;
pub const SYSCALL_SOCKET: u32 = 41;
pub const SYSCALL_BIND: u32 = 49;
pub const SYSCALL_CONNECT: u32 = 42;

// Mmap
pub const START_MMAP_ADDRESS: u64 = 0x0900_0000_0000;
2 changes: 2 additions & 0 deletions kernel/src/filesys/ext2/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum FilesystemError {
InvalidPath,
CacheError,
InvalidFd,
/// The process has used all of its file descriptors
NoFd,
}

pub type FilesystemResult<T> = Result<T, FilesystemError>;
Expand Down
68 changes: 44 additions & 24 deletions kernel/src/filesys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
paging::map_kernel_frame,
KERNEL_MAPPER,
},
processes::process::with_current_pcb,
processes::process::{with_current_pcb, FakeFile},
};
use alloc::{
boxed::Box,
Expand All @@ -21,7 +21,10 @@ use alloc::{
sync::Arc,
vec::Vec,
};
use core::sync::atomic::{AtomicBool, Ordering};
use core::{
cmp,
sync::atomic::{AtomicBool, Ordering},
};
use ext2::{
filesystem::{Ext2, FilesystemError, FilesystemResult},
node::{DirEntry, NodeError},
Expand Down Expand Up @@ -221,7 +224,17 @@ pub fn get_file(fd: usize) -> FilesystemResult<Arc<Mutex<File>>> {
if file.is_none() {
return Err(FilesystemError::InvalidFd);
}
Ok(file.unwrap())
let file = file.unwrap().clone();

// let file_guard = file.lock();
if let FakeFile::File(f) = file {
return Ok(f);
}
// match file_guard {
// FakeFile::File(f) => {return Ok(Arc::new(Mutex::new(f)))}
// _ => Err(FilesystemError::InvalidFd)
// }
Err(FilesystemError::InvalidFd)
}

/// Gets the file descriptor from the file path
Expand All @@ -231,9 +244,11 @@ pub fn get_fd(filepath: &str) -> FilesystemResult<usize> {
with_current_pcb(|pcb| {
for (fd, file_opt) in pcb.fd_table.iter().enumerate() {
if let Some(file_arc) = file_opt {
let file = file_arc.lock();
if file.pathname == filepath {
return Ok(fd);
if let FakeFile::File(f) = file_arc.clone() {
let file_guard = f.lock();
if file_guard.pathname == filepath {
return Ok(fd);
}
}
}
}
Expand Down Expand Up @@ -320,22 +335,24 @@ impl FileSystem for Ext2Wrapper {
self.filesystem.lock().get_node(path).await?.number()
};

let fd = with_current_pcb(|pcb| {
let mut next_fd_guard = pcb.next_fd.lock();
let fd = *next_fd_guard;
*next_fd_guard += 1;

let fd: Option<usize> = with_current_pcb(|pcb| {
let fd = pcb.find_next_fd()?;
let file = File::new(path.to_string(), fd, 0, flags, inode_number);
pcb.fd_table[fd] = Some(Arc::new(Mutex::new(file)));
pcb.fd_table[fd] = Some(FakeFile::File(Arc::new(Mutex::new(file))));

fd
Option::Some(fd)
});
self.refcount
.lock()
.entry(inode_number)
.and_modify(|v| *v += 1)
.or_insert(1);
Ok(fd)
match fd {
Option::None => Err(FilesystemError::NoFd),
Option::Some(file) => {
self.refcount
.lock()
.entry(inode_number)
.and_modify(|v| *v += 1)
.or_insert(1);
Ok(file)
}
}
}

async fn remove(&mut self, fd: usize) -> FilesystemResult<()> {
Expand Down Expand Up @@ -498,7 +515,6 @@ impl FileSystem for Ext2Wrapper {
let mut remaining = buf.len();
let mut total_read = 0;
let mut file_pos = locked_file.position;

while remaining > 0 {
let page_offset = file_pos & !(PAGE_SIZE - 1);
let page_offset_in_buf = file_pos % PAGE_SIZE;
Expand Down Expand Up @@ -596,7 +612,11 @@ impl FileSystem for Ext2Wrapper {
// Do raw pointer write *after* .await to avoid Send violation
unsafe {
let buf_ptr = kernel_va.as_mut_ptr();
core::ptr::copy_nonoverlapping(file_buf.as_ptr(), buf_ptr, file_buf.len());
core::ptr::copy_nonoverlapping(
file_buf.as_ptr(),
buf_ptr,
cmp::min(PAGE_SIZE, file_buf.len()),
);
}

file_mappings.insert(offset, Page::containing_address(kernel_va));
Expand Down Expand Up @@ -704,7 +724,7 @@ mod tests {
.open_file("./temp/test.txt", OpenFlags::O_WRONLY | OpenFlags::O_CREAT)
.await
.unwrap();
assert!(active_fd_count() > 0);
assert!(active_fd_count() == 4);

user_fs.write_file(fd, b"Test 123").await.unwrap();
user_fs.seek_file(fd, 0).await.unwrap();
Expand All @@ -715,7 +735,7 @@ mod tests {
assert_eq!(&buf[..8], b"Test 123");

user_fs.close_file(fd).await.unwrap();
assert_eq!(active_fd_count(), 0);
assert_eq!(active_fd_count(), 3);
}

#[test_case]
Expand Down Expand Up @@ -780,7 +800,7 @@ mod tests {
{
let meta = user_fs.metadata(fd).await.unwrap();
assert_eq!(meta.pathname, "./temp/meta.txt");
assert_eq!(meta.fd, 0);
assert_eq!(meta.fd, 3);
assert_eq!(meta.flags, OpenFlags::O_WRONLY | OpenFlags::O_CREAT);
}

Expand Down
1 change: 1 addition & 0 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(naked_functions_rustic_abi)]
#![feature(abi_x86_interrupt)]
#![feature(btree_cursors)]
#![cfg_attr(feature = "strict", deny(warnings))]
#![no_std]
#![cfg_attr(test, no_main)]
Expand Down
81 changes: 44 additions & 37 deletions kernel/src/memory/page_fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
paging::{create_mapping, create_mapping_to_frame, update_mapping, update_permissions},
HHDM_OFFSET, KERNEL_MAPPER,
},
processes::process::with_current_pcb,
processes::process::{with_current_pcb, FakeFile},
};

use super::mm::{VmArea, VmAreaBackings, VmaChain};
Expand Down Expand Up @@ -282,26 +282,31 @@ pub async fn handle_shared_file_mapping(
.expect("could not get fd from fd table")
});

let file_guard = { file.lock() };
let absent_in_page_cache = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.is_err();
if absent_in_page_cache {
fs.add_entry_to_page_cache(file_guard.clone(), offset as usize)
if let FakeFile::File(file_arc) = file {
let file_guard = file_arc.lock();
let absent_in_page_cache = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.expect("failed to add entry to page cache");
}
.is_err();
if absent_in_page_cache {
fs.add_entry_to_page_cache(file_guard.clone(), offset as usize)
.await
.expect("failed to add entry to page cache");
}

let kernel_va = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.unwrap();
let kernel_mapper = { KERNEL_MAPPER.lock() };
let frame: PhysFrame<Size4KiB> = kernel_mapper
.translate_page(Page::containing_address(kernel_va))
.expect("Could not translate kernel VA.");
create_mapping_to_frame(page, mapper, Some(flags), frame);
let kernel_va = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.unwrap();
let kernel_mapper = { KERNEL_MAPPER.lock() };
let frame: PhysFrame<Size4KiB> = kernel_mapper
.translate_page(Page::containing_address(kernel_va))
.expect("Could not translate kernel VA.");
create_mapping_to_frame(page, mapper, Some(flags), frame);
} else {
// TODO: fix this to return a error to the user (probally just kill it)
panic!("Tried to mmap a socket!")
}
}

/// Handles a page fault for a file that should not be shared by adding an entry into the page
Expand Down Expand Up @@ -340,26 +345,28 @@ pub async fn handle_private_file_mapping(
.expect("could not get fd from fd table")
});

let file_guard = { file.lock() };
let absent_in_page_cache = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.is_err();
if absent_in_page_cache {
fs.add_entry_to_page_cache(file_guard.clone(), offset as usize)
if let FakeFile::File(file_arc) = file {
let file_guard = file_arc.lock();
let absent_in_page_cache = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.expect("failed to add entry to page cache");
}
.is_err();
if absent_in_page_cache {
fs.add_entry_to_page_cache(file_guard.clone(), offset as usize)
.await
.expect("failed to add entry to page cache");
}

let kernel_va = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.unwrap();
let kernel_mapper = { KERNEL_MAPPER.lock() };
let frame: PhysFrame<Size4KiB> = kernel_mapper
.translate_page(Page::containing_address(kernel_va))
.expect("Could not translate kernel VA.");
create_mapping_to_frame(page, mapper, Some(flags), frame);
let kernel_va = fs
.page_cache_get_mapping(file_guard.clone(), offset as usize)
.await
.unwrap();
let kernel_mapper = { KERNEL_MAPPER.lock() };
let frame: PhysFrame<Size4KiB> = kernel_mapper
.translate_page(Page::containing_address(kernel_va))
.expect("Could not translate kernel VA.");
create_mapping_to_frame(page, mapper, Some(flags), frame);
}
}

/// Handles a fault by creating a new mapping and inserting it into the backing.
Expand Down
Loading
Loading