Skip to content

Commit

Permalink
kernel/libxernel: Implement unlock function for OnDrop only when T is…
Browse files Browse the repository at this point in the history
… SpinlockGuard
  • Loading branch information
Lockna committed Sep 19, 2024
1 parent 156fdd6 commit 60ca2be
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 78 deletions.
9 changes: 9 additions & 0 deletions crates/libxernel/src/on_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use core::mem::ManuallyDrop;

use core::ops::{Deref, DerefMut};

use crate::sync::SpinlockGuard;

pub struct OnDrop<T, F>
where
F: FnOnce(),
Expand Down Expand Up @@ -34,6 +36,13 @@ where
}
}

impl<'a, T, F> OnDrop<SpinlockGuard<'a, T>, F>
where
F: FnOnce(),
{
pub fn unlock(self) {}
}

impl<T, F> Deref for OnDrop<T, F>
where
F: FnOnce(),
Expand Down
156 changes: 78 additions & 78 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,122 +89,122 @@ fn panic(info: &PanicInfo) -> ! {
// define the kernel's entry point function
#[no_mangle]
extern "C" fn kernel_main() -> ! {
{
framebuffer::init();
info!("framebuffer initialized");
framebuffer::init();
info!("framebuffer initialized");

gdt::init();
info!("GDT loaded");
amd64::interrupts::init();
info!("IDT loaded");
amd64::interrupts::disable_pic();
gdt::init();
info!("GDT loaded");
amd64::interrupts::init();
info!("IDT loaded");
amd64::interrupts::disable_pic();

mem::init();
mem::init();

acpi::init();
info!("acpi initialized");
acpi::init();
info!("acpi initialized");

backtrace::init();
info!("backtrace initialized");
backtrace::init();
info!("backtrace initialized");

hpet::init();
hpet::init();

apic::init();
apic::init();

syscall::init();
syscall::init();

vfs::init();
vfs::init();

vfs::test();
vfs::test();

let bootloader_info = BOOTLOADER_INFO
.get_response()
.get()
.expect("barebones: recieved no bootloader info");
let bootloader_info = BOOTLOADER_INFO
.get_response()
.get()
.expect("barebones: recieved no bootloader info");

info!(
"bootloader: (name={:?}, version={:?})",
bootloader_info.name.to_str().unwrap(),
bootloader_info.version.to_str().unwrap()
);
info!(
"bootloader: (name={:?}, version={:?})",
bootloader_info.name.to_str().unwrap(),
bootloader_info.version.to_str().unwrap()
);

Rtc::read();
Rtc::read();

KERNEL_PROCESS.set_once(Arc::new(Spinlock::new(Process::new(None))));
KERNEL_PROCESS.set_once(Arc::new(Spinlock::new(Process::new(None))));

let smp_response = SMP_REQUEST.get_response().get_mut().unwrap();
let smp_response = SMP_REQUEST.get_response().get_mut().unwrap();

let bsp_lapic_id = smp_response.bsp_lapic_id;
let bsp_lapic_id = smp_response.bsp_lapic_id;

CPU_COUNT.set_once(smp_response.cpu_count as usize);
CPU_COUNT.set_once(smp_response.cpu_count as usize);

register_cpu();
register_cpu();

for cpu in smp_response.cpus().iter_mut() {
if cpu.lapic_id != bsp_lapic_id {
cpu.goto_address = arch::amd64::x86_64_ap_main;
}
for cpu in smp_response.cpus().iter_mut() {
if cpu.lapic_id != bsp_lapic_id {
cpu.goto_address = arch::amd64::x86_64_ap_main;
}
}

wait_until_cpus_registered();
wait_until_cpus_registered();

timer::init();
info!("scheduler initialized");
timer::init();
info!("scheduler initialized");

let process = Arc::new(Spinlock::new(Process::new(Some(KERNEL_PROCESS.clone()))));
let process = Arc::new(Spinlock::new(Process::new(Some(KERNEL_PROCESS.clone()))));

let _user_task = Thread::new_user_thread(process.clone(), VirtAddr::new(0x200000));
let _user_task = Thread::new_user_thread(process.clone(), VirtAddr::new(0x200000));

let page = FRAME_ALLOCATOR.aquire().allocate_frame::<Size2MiB>().unwrap();
let page = FRAME_ALLOCATOR.aquire().allocate_frame::<Size2MiB>().unwrap();

KERNEL_PAGE_MAPPER.aquire().map(
page,
Page::from_start_address(VirtAddr::new(0x200000)).unwrap(),
PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE | PageTableFlags::PRESENT,
true,
);
KERNEL_PAGE_MAPPER.aquire().map(
page,
Page::from_start_address(VirtAddr::new(0x200000)).unwrap(),
PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE | PageTableFlags::PRESENT,
true,
);

let mut process = process.aquire();
let pm = process.get_page_table().as_mut().unwrap();
pm.map(
page,
Page::from_start_address(VirtAddr::new(0x200000)).unwrap(),
PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE | PageTableFlags::PRESENT,
true,
);
let mut process = process.aquire();
let pm = process.get_page_table().as_mut().unwrap();
pm.map(
page,
Page::from_start_address(VirtAddr::new(0x200000)).unwrap(),
PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE | PageTableFlags::PRESENT,
true,
);

// unsafe {
// let start_address_fn = test_userspace_fn as usize;
process.unlock();

// // the `test_userspace_fn` is very small and should fit in 512 bytes
// for i in 0..512 {
// let ptr = (0x200000 + i) as *mut u8;
// let val = (start_address_fn + i) as *mut u8;
// unsafe {
// let start_address_fn = test_userspace_fn as usize;

// ptr.write_volatile(val.read_volatile());
// }
// }
// // the `test_userspace_fn` is very small and should fit in 512 bytes
// for i in 0..512 {
// let ptr = (0x200000 + i) as *mut u8;
// let val = (start_address_fn + i) as *mut u8;

let main_task = Thread::kernel_thread_from_fn(kmain_thread);
// ptr.write_volatile(val.read_volatile());
// }
// }

let kernel_task = Thread::kernel_thread_from_fn(task1);
let main_task = Thread::kernel_thread_from_fn(kmain_thread);

let kernel_task2 = Thread::kernel_thread_from_fn(task2);
let kernel_task = Thread::kernel_thread_from_fn(task1);

current_cpu().enqueue_thread(Arc::new(main_task));
current_cpu().enqueue_thread(Arc::new(kernel_task));
current_cpu().enqueue_thread(Arc::new(kernel_task2));
let kernel_task2 = Thread::kernel_thread_from_fn(task2);

let timekeeper = TimerEvent::new(hardclock, (), Duration::from_secs(1), false);
current_cpu().enqueue_thread(Arc::new(main_task));
current_cpu().enqueue_thread(Arc::new(kernel_task));
current_cpu().enqueue_thread(Arc::new(kernel_task2));

current_cpu().enqueue_timer(timekeeper);
let timekeeper = TimerEvent::new(hardclock, (), Duration::from_secs(1), false);

let resched = TimerEvent::new(reschedule, (), Duration::from_millis(5), false);
current_cpu().enqueue_timer(timekeeper);

current_cpu().enqueue_timer(resched);
let resched = TimerEvent::new(reschedule, (), Duration::from_millis(5), false);

amd64::interrupts::enable();
}
current_cpu().enqueue_timer(resched);

amd64::interrupts::enable();
hcf();
}

Expand Down

0 comments on commit 60ca2be

Please sign in to comment.