Skip to content

Commit

Permalink
Migrate to Rust 2024 edition
Browse files Browse the repository at this point in the history
  • Loading branch information
fruhland committed Jun 12, 2024
1 parent 19f9812 commit 21021ea
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 63 deletions.
4 changes: 3 additions & 1 deletion os/application/date/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cargo-features = ["edition2024"]

[package]
edition = "2021"
edition = "2024"
name = "date"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]
Expand Down
4 changes: 3 additions & 1 deletion os/application/hello/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cargo-features = ["edition2024"]

[package]
edition = "2021"
edition = "2024"
name = "hello"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]
Expand Down
4 changes: 3 additions & 1 deletion os/application/shell/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cargo-features = ["edition2024"]

[package]
edition = "2021"
edition = "2024"
name = "shell"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]
Expand Down
4 changes: 3 additions & 1 deletion os/application/uptime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cargo-features = ["edition2024"]

[package]
edition = "2021"
edition = "2024"
name = "uptime"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]
Expand Down
9 changes: 5 additions & 4 deletions os/kernel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cargo-features = ["edition2024"]

[package]
edition = "2021"
edition = "2024"
name = "kernel"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]
Expand All @@ -17,7 +19,6 @@ syscall = { path = "../library/syscall" }

# External depencies
spin = "0.9.8"
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
x86_64 = "0.15.1"
linked_list_allocator = { version = "0.10.5", features = ["alloc_ref"] }
multiboot2 = "0.20.0"
Expand All @@ -35,7 +36,7 @@ uefi = { version = "0.28.0", features = ["alloc"] }
log = "0.4.20"
goblin = { version = "0.8.2", default-features = false, features = ["elf32", "elf64", "endian_fd"]}
tar-no-std = "0.3.1"
pci_types = "0.7.0"
pci_types = "0.9.1"

[build-dependencies]
built = { version = "0.7.2", features = ["chrono", "git2"] }
built = { version = "0.7.3", features = ["chrono", "git2"] }
2 changes: 1 addition & 1 deletion os/kernel/src/device/lfb_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl LFBTerminal {
let active_thread_ids = scheduler().active_thread_ids();

// Draw info string
let info_string = format!("D3OS v{} ({}) | Uptime: {:0>2}:{:0>2}:{:0>2} | Processes: {} | Threads: {}",
let info_string = format!("D³OS v{} ({}) | Uptime: {:0>2}:{:0>2}:{:0>2} | Processes: {} | Threads: {}",
built_info::PKG_VERSION, built_info::PROFILE,
uptime.num_hours(), uptime.num_minutes() % 60, uptime.num_seconds() - (uptime.num_minutes() * 60),
active_process_ids.len(),
Expand Down
14 changes: 9 additions & 5 deletions os/kernel/src/device/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl ConfigurationSpace {
| (address.function() as u32) << 8
| (offset & 0xfc) as u32;

ports.address_port.write(address_raw);
unsafe { ports.address_port.write(address_raw); }
}
}

Expand All @@ -52,15 +52,19 @@ impl ConfigRegionAccess for ConfigurationSpace {
unsafe fn read(&self, address: PciAddress, offset: u16) -> u32 {
let mut ports = self.ports.lock();

Self::prepare_access(&mut ports, address, offset);
return ports.data_port.read();
unsafe {
Self::prepare_access(&mut ports, address, offset);
return ports.data_port.read();
}
}

unsafe fn write(&self, address: PciAddress, offset: u16, value: u32) {
let mut ports = self.ports.lock();

Self::prepare_access(&mut ports, address, offset);
ports.data_port.write(value);
unsafe {
Self::prepare_access(&mut ports, address, offset);
ports.data_port.write(value);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions os/kernel/src/device/pit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl Timer {

/// Used to calibrate the APIC timer.
#[naked]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn early_delay_50ms() {
asm!(
"mov al, 0x30", // Channel 0, mode 0, low-/high byte access mode
Expand Down
13 changes: 8 additions & 5 deletions os/kernel/src/memory/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl KernelAllocator {
}

pub unsafe fn init(&self, frames: &PhysFrameRange) {
self.heap.lock().init(frames.start.start_address().as_u64() as *mut u8, (frames.end - frames.start) as usize * PAGE_SIZE);
let mut heap = self.heap.lock();
unsafe { heap.init(frames.start.start_address().as_u64() as *mut u8, (frames.end - frames.start) as usize * PAGE_SIZE); }
}

pub fn is_initialized(&self) -> bool {
Expand All @@ -49,7 +50,8 @@ unsafe impl Allocator for KernelAllocator {

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
if layout.size() != 0 {
self.heap.lock().deallocate(ptr, layout);
let mut heap = self.heap.lock();
unsafe { heap.deallocate(ptr, layout); }
}
}
}
Expand All @@ -63,7 +65,8 @@ unsafe impl GlobalAlloc for KernelAllocator {
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.heap.lock().deallocate(NonNull::new_unchecked(ptr), layout);
let mut heap = self.heap.lock();
unsafe { heap.deallocate(NonNull::new_unchecked(ptr), layout); }
}
}

Expand Down Expand Up @@ -92,14 +95,14 @@ unsafe impl Allocator for StackAllocator {
assert_eq!(layout.size() % PAGE_SIZE, 0);

let start = PhysFrame::from_start_address(PhysAddr::new(ptr.as_ptr() as u64)).unwrap();
physical::free(PhysFrameRange { start, end: start + (layout.size() / PAGE_SIZE) as u64 });
unsafe { physical::free(PhysFrameRange { start, end: start + (layout.size() / PAGE_SIZE) as u64 }); }
}
}
}

impl acpi::AcpiHandler for AcpiHandler {
unsafe fn map_physical_region<T>(&self, physical_address: usize, size: usize) -> PhysicalMapping<Self, T> {
PhysicalMapping::new(physical_address, NonNull::new(physical_address as *mut T).unwrap(), size, size, AcpiHandler)
unsafe { PhysicalMapping::new(physical_address, NonNull::new(physical_address as *mut T).unwrap(), size, size, AcpiHandler) }
}

fn unmap_physical_region<T>(_region: &PhysicalMapping<Self, T>) {}
Expand Down
61 changes: 38 additions & 23 deletions os/kernel/src/memory/physical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub unsafe fn insert(mut region: PhysFrameRange) {
current_limit.swap(&Cell::new(region.end));
}

free(region);
unsafe { free(region); }
}

/// Allocate `frame_count` contiguous page frames.
Expand All @@ -43,12 +43,12 @@ pub fn alloc(frame_count: usize) -> PhysFrameRange {
/// Free `frame_count` contiguous page frames.
/// Unsafe because invalid parameters may break the list allocator.
pub unsafe fn free(frames: PhysFrameRange) {
PAGE_FRAME_ALLOCATOR.lock().free_block(frames);
unsafe { PAGE_FRAME_ALLOCATOR.lock().free_block(frames); }
}

/// Permanently reserve a block of free memory.
pub unsafe fn reserve(frames: PhysFrameRange) {
PAGE_FRAME_ALLOCATOR.lock().reserve_block(frames);
unsafe { PAGE_FRAME_ALLOCATOR.lock().reserve_block(frames); }
}

/// Get the highest physical address, managed by PAGE_FRAME_ALLOCATOR.
Expand Down Expand Up @@ -117,9 +117,11 @@ impl PageFrameListAllocator {

// Check if list is empty
if self.head.next.is_none() {
new_block.next = self.head.next.take();
new_block_ptr.write(new_block);
self.head.next = Some(&mut *new_block_ptr);
unsafe {
new_block.next = self.head.next.take();
new_block_ptr.write(new_block);
self.head.next = Some(&mut *new_block_ptr);
}

return;
}
Expand All @@ -128,9 +130,11 @@ impl PageFrameListAllocator {
let mut current = &mut self.head;
while let Some(ref mut block) = current.next {
if block.start().start_address() > frames.start.start_address() {
new_block.next = current.next.take();
new_block_ptr.write(new_block);
current.next = Some(&mut *new_block_ptr);
unsafe {
new_block.next = current.next.take();
new_block_ptr.write(new_block);
current.next = Some(&mut *new_block_ptr);
}

return;
}
Expand All @@ -139,9 +143,11 @@ impl PageFrameListAllocator {
}

// Insert new block at the list's end
new_block.next = None;
new_block_ptr.write(new_block);
current.next = Some(&mut *new_block_ptr);
unsafe {
new_block.next = None;
new_block_ptr.write(new_block);
current.next = Some(&mut *new_block_ptr);
}
}

/// Search a free memory block.
Expand Down Expand Up @@ -189,11 +195,15 @@ impl PageFrameListAllocator {
if frames.end == block.start() {
// The freed memory block extends 'block' from the bottom
let mut new_block = PageFrameNode::new(block.frame_count + (frames.end - frames.start) as usize);
new_block_ptr = frames.start.start_address().as_u64() as *mut PageFrameNode;
new_block.next = block.next.take();
new_block_ptr.write(new_block);

current.next = Some(&mut *new_block_ptr);
unsafe {
new_block_ptr = frames.start.start_address().as_u64() as *mut PageFrameNode;
new_block.next = block.next.take();
new_block_ptr.write(new_block);

current.next = Some(&mut *new_block_ptr);
}

return;
} else if block.end() == frames.start {
// The freed memory block extends 'block' from the top
Expand All @@ -217,7 +227,7 @@ impl PageFrameListAllocator {
current = current.next.as_mut().unwrap();
}

self.insert(frames);
unsafe { self.insert(frames); }
}

/// Permanently reserve a block of free memory.
Expand All @@ -240,9 +250,12 @@ impl PageFrameListAllocator {

let mut above_block = PageFrameNode::new(above_size as usize);
let above_block_ptr = reserved.end.start_address().as_u64() as *mut PageFrameNode;
above_block.next = block.next.take();
block.next = Some(&mut *above_block_ptr);
above_block_ptr.write(above_block);

unsafe {
above_block.next = block.next.take();
block.next = Some(&mut *above_block_ptr);
above_block_ptr.write(above_block);
}
}
} else if block.start() <= reserved.end && block.end() >= reserved.start { // Block starts within the reserved region
if block.end() <= reserved.end { // Block start within and ends within the reserved region
Expand All @@ -252,9 +265,11 @@ impl PageFrameListAllocator {
let mut new_block = PageFrameNode::new(block.frame_count - overlapping as usize);
let new_block_ptr = (block.start() + overlapping).start_address().as_u64() as *mut PageFrameNode;

new_block.next = block.next.take();
new_block_ptr.write(new_block);
current.next = Some(&mut *new_block_ptr);
unsafe {
new_block.next = block.next.take();
new_block_ptr.write(new_block);
current.next = Some(&mut *new_block_ptr);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion os/kernel/src/process/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ unsafe impl Sync for Scheduler {}
/// Called from assembly code, after the thread has been switched
#[no_mangle]
pub unsafe extern "C" fn unlock_scheduler() {
scheduler().ready_state.force_unlock();
unsafe { scheduler().ready_state.force_unlock(); }
}

impl Scheduler {
Expand Down
22 changes: 10 additions & 12 deletions os/kernel/src/process/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ impl Thread {
}

pub unsafe fn start_first(thread_ptr: *const Thread) {
let thread = thread_ptr.as_ref().unwrap();
let thread = unsafe { thread_ptr.as_ref().unwrap() };
let old_rsp0 = thread.stacks.lock().old_rsp0;

thread_kernel_start(old_rsp0.as_u64());
unsafe { thread_kernel_start(old_rsp0.as_u64()); }
}

pub unsafe fn switch(current_ptr: *const Thread, next_ptr: *const Thread) {
let current = current_ptr.as_ref().unwrap();
let next = next_ptr.as_ref().unwrap();
let current = unsafe { current_ptr.as_ref().unwrap() };
let next = unsafe { next_ptr.as_ref().unwrap() };
let current_rsp0 = ptr::from_ref(&current.stacks.lock().old_rsp0) as *mut u64;
let next_rsp0 = next.stacks.lock().old_rsp0.as_u64();
let next_rsp0_end = next.kernel_stack_addr().as_u64();
Expand Down Expand Up @@ -274,17 +274,12 @@ impl Thread {
old_rsp0 = stacks.old_rsp0.as_u64();
}

unsafe {
asm!(
"mov rsi, {}",
in(reg) self.entry
);
thread_user_start(old_rsp0);
}
unsafe { thread_user_start(old_rsp0, self.entry); }
}
}

#[naked]
#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn thread_kernel_start(old_rsp0: u64) {
asm!(
"mov rsp, rdi", // First parameter -> load 'old_rsp0'
Expand Down Expand Up @@ -312,7 +307,9 @@ unsafe extern "C" fn thread_kernel_start(old_rsp0: u64) {
}

#[naked]
unsafe extern "C" fn thread_user_start(old_rsp0: u64) {
#[allow(unsafe_op_in_unsafe_fn)]
#[allow(improper_ctypes_definitions)] // 'entry' takes no arguments and has no return value, so we just assume that the "C" and "Rust" ABIs act the same way in this case
unsafe extern "C" fn thread_user_start(old_rsp0: u64, entry: fn()) {
asm!(
"mov rsp, rdi", // Load 'old_rsp' (first parameter)
"mov rdi, rsi", // Second parameter becomes first parameter for 'kickoff_user_thread()'
Expand All @@ -322,6 +319,7 @@ unsafe extern "C" fn thread_user_start(old_rsp0: u64) {
}

#[naked]
#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn thread_switch(current_rsp0: *mut u64, next_rsp0: u64, next_rsp0_end: u64, next_cr3: u64) {
asm!(
// Save registers of current thread
Expand Down
Loading

0 comments on commit 21021ea

Please sign in to comment.