Format code

This commit is contained in:
pjht 2024-07-07 14:37:21 -05:00
parent e2d248b609
commit 40a3a2c3b4
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
6 changed files with 62 additions and 30 deletions

View File

@ -31,7 +31,7 @@ static GDT: Lazy<GDTAndSelectors> = Lazy::new(|| {
code_sel: gdt.append(Descriptor::kernel_code_segment()), code_sel: gdt.append(Descriptor::kernel_code_segment()),
data_sel: gdt.append(Descriptor::kernel_data_segment()), data_sel: gdt.append(Descriptor::kernel_data_segment()),
// SAFETY: The TSS is a static and thus a pointer to it will always be valid // SAFETY: The TSS is a static and thus a pointer to it will always be valid
tss_sel: gdt.append(unsafe { Descriptor::tss_segment_unchecked(addr_of!(TSS))}), tss_sel: gdt.append(unsafe { Descriptor::tss_segment_unchecked(addr_of!(TSS)) }),
user_data_sel: gdt.append(Descriptor::user_data_segment()), user_data_sel: gdt.append(Descriptor::user_data_segment()),
user_code_sel: gdt.append(Descriptor::user_code_segment()), user_code_sel: gdt.append(Descriptor::user_code_segment()),
}; };

View File

@ -1,5 +1,10 @@
use crate::{ use crate::{
bootinfo::BOOTINFO, print, println, serial::SECOND_PORT, tasking::SleepReason, virtual_memory::{ASpaceMutex, AddressSpace, ACTIVE_SPACE, KERNEL_SPACE}, TASKING bootinfo::BOOTINFO,
print, println,
serial::SECOND_PORT,
tasking::SleepReason,
virtual_memory::{ASpaceMutex, AddressSpace, ACTIVE_SPACE, KERNEL_SPACE},
TASKING,
}; };
use alloc::{boxed::Box, vec::Vec}; use alloc::{boxed::Box, vec::Vec};
use core::{arch::asm, ptr::addr_of, slice, str}; use core::{arch::asm, ptr::addr_of, slice, str};
@ -242,7 +247,8 @@ extern "C" fn syscall_handler() {
} }
3 => { 3 => {
let initrd = unsafe { let initrd = unsafe {
let ramdisk_start = BOOTINFO.ramdisk_addr.into_option().expect("ramdisk to be present"); let ramdisk_start =
BOOTINFO.ramdisk_addr.into_option().expect("ramdisk to be present");
let ramdisk_len = BOOTINFO.ramdisk_len; let ramdisk_len = BOOTINFO.ramdisk_len;
slice::from_raw_parts(ramdisk_start as *const u8, ramdisk_len as usize) slice::from_raw_parts(ramdisk_start as *const u8, ramdisk_len as usize)
}; };
@ -397,9 +403,11 @@ extern "C" fn syscall_handler() {
17 => { 17 => {
let mut tasking = TASKING.lock(); let mut tasking = TASKING.lock();
let space = tasking.address_spaces_mut().get_mut((regs.rcx - 1) as usize).unwrap(); let space = tasking.address_spaces_mut().get_mut((regs.rcx - 1) as usize).unwrap();
space.run(|| unsafe { slice::from_raw_parts_mut(regs.rdx as *mut u8, regs.rsi as usize).fill(0) }); space.run(|| unsafe {
slice::from_raw_parts_mut(regs.rdx as *mut u8, regs.rsi as usize).fill(0)
});
retval = 0; retval = 0;
}, }
18 => { 18 => {
let mut tasking = TASKING.lock(); let mut tasking = TASKING.lock();
if tasking.current_message_queue_mut().is_empty() { if tasking.current_message_queue_mut().is_empty() {

View File

@ -26,11 +26,22 @@ mod virtual_memory;
use core::{slice, usize}; use core::{slice, usize};
use bootinfo::BOOTINFO; use bootinfo::BOOTINFO;
use elf::{abi::{PT_DYNAMIC, PT_GNU_EH_FRAME, PT_GNU_RELRO, PT_GNU_STACK, PT_LOAD, PT_NULL, PT_PHDR, R_X86_64_RELATIVE, SHT_REL, SHT_RELA}, endian::AnyEndian, ElfBytes}; use elf::{
abi::{
PT_DYNAMIC, PT_GNU_EH_FRAME, PT_GNU_RELRO, PT_GNU_STACK, PT_LOAD, PT_NULL, PT_PHDR,
R_X86_64_RELATIVE, SHT_REL, SHT_RELA,
},
endian::AnyEndian,
ElfBytes,
};
use serial::SECOND_PORT; use serial::SECOND_PORT;
use tar_no_std::TarArchiveRef; use tar_no_std::TarArchiveRef;
use tasking::TASKING; use tasking::TASKING;
use x86_64::{registers::rflags::{self, RFlags}, structures::paging::{Page, PageTableFlags}, VirtAddr}; use x86_64::{
registers::rflags::{self, RFlags},
structures::paging::{Page, PageTableFlags},
VirtAddr,
};
use crate::virtual_memory::AddressSpace; use crate::virtual_memory::AddressSpace;
@ -61,14 +72,14 @@ pub fn main() {
for mut pheader in init.segments().unwrap().iter() { for mut pheader in init.segments().unwrap().iter() {
match pheader.p_type { match pheader.p_type {
PT_NULL => (), PT_NULL => (),
PT_LOAD => { PT_LOAD => {
if pheader.p_vaddr < 0x1000 { if pheader.p_vaddr < 0x1000 {
if pheader.p_memsz < 0x1000 { if pheader.p_memsz < 0x1000 {
continue; continue;
} }
pheader.p_offset += 0x1000-pheader.p_vaddr; pheader.p_offset += 0x1000 - pheader.p_vaddr;
pheader.p_memsz -= 0x1000-pheader.p_vaddr; pheader.p_memsz -= 0x1000 - pheader.p_vaddr;
pheader.p_filesz -= 0x1000-pheader.p_vaddr; pheader.p_filesz -= 0x1000 - pheader.p_vaddr;
pheader.p_vaddr = 0x1000; pheader.p_vaddr = 0x1000;
} }
let start_page = Page::containing_address(VirtAddr::new(pheader.p_vaddr)); let start_page = Page::containing_address(VirtAddr::new(pheader.p_vaddr));
@ -84,8 +95,14 @@ pub fn main() {
.map_only_unused(start_page, num_pages, PageTableFlags::USER_ACCESSIBLE) .map_only_unused(start_page, num_pages, PageTableFlags::USER_ACCESSIBLE)
.expect("Unable to map region"); .expect("Unable to map region");
init_addr_space.run(|| unsafe { init_addr_space.run(|| unsafe {
let dst = slice::from_raw_parts_mut(pheader.p_vaddr as *mut u8, pheader.p_memsz as usize); let dst = slice::from_raw_parts_mut(
dst[0..pheader.p_filesz as usize].copy_from_slice(&init_data[(pheader.p_offset as usize)..((pheader.p_offset+pheader.p_filesz) as usize)]); pheader.p_vaddr as *mut u8,
pheader.p_memsz as usize,
);
dst[0..pheader.p_filesz as usize].copy_from_slice(
&init_data[(pheader.p_offset as usize)
..((pheader.p_offset + pheader.p_filesz) as usize)],
);
dst[(pheader.p_filesz as usize)..(pheader.p_memsz as usize)].fill(0) dst[(pheader.p_filesz as usize)..(pheader.p_memsz as usize)].fill(0)
}); });
} }
@ -138,9 +155,6 @@ pub fn main() {
TASKING TASKING
.lock() .lock()
.new_process( .new_process(init.ehdr.e_entry as _, init_addr_space)
init.ehdr.e_entry as _,
init_addr_space,
)
.expect("Failed to create init process"); .expect("Failed to create init process");
} }

View File

@ -2,10 +2,7 @@
fn print_backtrace() { fn print_backtrace() {
use core::ffi::c_void; use core::ffi::c_void;
use unwinding::abi::{UnwindContext, UnwindReasonCode, _Unwind_Backtrace, _Unwind_GetIP}; use unwinding::abi::{UnwindContext, UnwindReasonCode, _Unwind_Backtrace, _Unwind_GetIP};
extern "C" fn callback( extern "C" fn callback(unwind_ctx: &UnwindContext<'_>, _arg: *mut c_void) -> UnwindReasonCode {
unwind_ctx: &UnwindContext<'_>,
_arg: *mut c_void,
) -> UnwindReasonCode {
println!("{:#x}", _Unwind_GetIP(unwind_ctx)); println!("{:#x}", _Unwind_GetIP(unwind_ctx));
UnwindReasonCode::NO_REASON UnwindReasonCode::NO_REASON
} }

View File

@ -1,4 +1,8 @@
use crate::{bootinfo::BOOTINFO, println, virtual_memory::{AsVirt, PHYS_OFFSET}}; use crate::{
bootinfo::BOOTINFO,
println,
virtual_memory::{AsVirt, PHYS_OFFSET},
};
use bootloader_api::info::MemoryRegionKind; use bootloader_api::info::MemoryRegionKind;
use core::{alloc::Layout, ptr::NonNull}; use core::{alloc::Layout, ptr::NonNull};
use derive_try_from_primitive::TryFromPrimitive; use derive_try_from_primitive::TryFromPrimitive;

View File

@ -1,12 +1,17 @@
use crate::{ use crate::{
dbg, gdt, println, qemu_exit, virtual_memory::{ASpaceMutex, AddressSpace, PagingError, KERNEL_SPACE} gdt, println, qemu_exit,
virtual_memory::{ASpaceMutex, AddressSpace, PagingError, KERNEL_SPACE},
}; };
use alloc::{boxed::Box, collections::VecDeque, vec::Vec}; use alloc::{boxed::Box, collections::VecDeque, vec::Vec};
use core::{arch::asm, ptr::addr_of}; use core::{arch::asm, ptr::addr_of};
use crossbeam_queue::SegQueue; use crossbeam_queue::SegQueue;
use slab::Slab; use slab::Slab;
use spin::{Lazy, Mutex}; use spin::{Lazy, Mutex};
use x86_64::{instructions::interrupts, structures::paging::{Page, PageTableFlags}, VirtAddr}; use x86_64::{
instructions::interrupts,
structures::paging::{Page, PageTableFlags},
VirtAddr,
};
#[naked] #[naked]
extern "C" fn switch_to_asm(current_stack: *mut *mut usize, next_stack: *mut usize) { extern "C" fn switch_to_asm(current_stack: *mut *mut usize, next_stack: *mut usize) {
@ -90,7 +95,7 @@ struct Process {
address_spaces: Slab<AddressSpace>, address_spaces: Slab<AddressSpace>,
data_buffers: Slab<*mut [u8]>, data_buffers: Slab<*mut [u8]>,
message_queue: SegQueue<(usize, usize)>, message_queue: SegQueue<(usize, usize)>,
sleeping: Option<SleepReason> sleeping: Option<SleepReason>,
} }
unsafe impl Send for Process {} unsafe impl Send for Process {}
@ -122,8 +127,12 @@ impl Tasking {
kernel_stack.resize(0x1_0000, 0); kernel_stack.resize(0x1_0000, 0);
let mut kernel_stack = kernel_stack.into_boxed_slice(); let mut kernel_stack = kernel_stack.into_boxed_slice();
kernel_stack[0xFFFF] = entry_point as usize; kernel_stack[0xFFFF] = entry_point as usize;
address_space.map_assert_unused(Page::from_start_address(VirtAddr::new(0xFFF_FF80_0000)).unwrap(), 16, PageTableFlags::USER_ACCESSIBLE)?; address_space.map_assert_unused(
kernel_stack[0xFFFE] = 0xFFF_FF80_0000 + (16*4096); Page::from_start_address(VirtAddr::new(0xFFF_FF80_0000)).unwrap(),
16,
PageTableFlags::USER_ACCESSIBLE,
)?;
kernel_stack[0xFFFE] = 0xFFF_FF80_0000 + (16 * 4096);
kernel_stack[0xFFFD] = task_init as usize; kernel_stack[0xFFFD] = task_init as usize;
kernel_stack[0xFFFC] = task_force_unlock as usize; kernel_stack[0xFFFC] = task_force_unlock as usize;
let pid = self.processes.insert(Process { let pid = self.processes.insert(Process {
@ -217,7 +226,7 @@ impl Tasking {
self.processes[pid].sleeping self.processes[pid].sleeping
} }
pub fn sleep(&mut self, reason: SleepReason) { pub fn sleep(&mut self, reason: SleepReason) {
self.processes[self.current_process.unwrap()].sleeping = Some(reason); self.processes[self.current_process.unwrap()].sleeping = Some(reason);
self.task_yield(); self.task_yield();
} }