From 1f4fce0f66c5588dceb64b4d52f1f077cc09be52 Mon Sep 17 00:00:00 2001 From: koniifer Date: Mon, 16 Sep 2024 20:15:51 +0100 Subject: [PATCH] please bugs begone --- Cargo.lock | 29 +++++--- kernel/src/arch/x86_64/gdt.rs | 4 +- kernel/src/arch/x86_64/interrupts.rs | 66 +++++++++---------- kernel/src/holeybytes/ecah.rs | 52 +++++++-------- .../holeybytes/kernel_services/mem_serve.rs | 10 +-- kernel/src/holeybytes/mod.rs | 4 +- kernel/src/lib.rs | 1 + sysdata/programs/kvstore/README.md | 2 - sysdata/programs/kvstore/src/main.hb | 37 ----------- sysdata/programs/ps2_driver/src/main.hb | 49 +++++++------- sysdata/programs/render_example/src/main.hb | 2 +- 11 files changed, 112 insertions(+), 144 deletions(-) delete mode 100644 sysdata/programs/kvstore/README.md delete mode 100644 sysdata/programs/kvstore/src/main.hb diff --git a/Cargo.lock b/Cargo.lock index 1d9ee7d6..929e8d20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -390,25 +390,26 @@ dependencies = [ [[package]] name = "hbbytecode" version = "0.1.0" -source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#6968e7d769d905c41fbe4c195d9d34d3339c32cf" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#faf068885a42aedbb6a2bc50e21cb6d34f211cdb" [[package]] name = "hbbytecode" version = "0.1.0" -source = "git+https://git.ablecorp.us/ableos/holey-bytes.git#6968e7d769d905c41fbe4c195d9d34d3339c32cf" +source = "git+https://git.ablecorp.us/ableos/holey-bytes.git#faf068885a42aedbb6a2bc50e21cb6d34f211cdb" [[package]] name = "hblang" version = "0.1.0" -source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#6968e7d769d905c41fbe4c195d9d34d3339c32cf" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#faf068885a42aedbb6a2bc50e21cb6d34f211cdb" dependencies = [ "hbvm 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)", + "regalloc2", ] [[package]] name = "hbvm" version = "0.1.0" -source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#6968e7d769d905c41fbe4c195d9d34d3339c32cf" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#faf068885a42aedbb6a2bc50e21cb6d34f211cdb" dependencies = [ "hbbytecode 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)", ] @@ -416,7 +417,7 @@ dependencies = [ [[package]] name = "hbvm" version = "0.1.0" -source = "git+https://git.ablecorp.us/ableos/holey-bytes.git#6968e7d769d905c41fbe4c195d9d34d3339c32cf" +source = "git+https://git.ablecorp.us/ableos/holey-bytes.git#faf068885a42aedbb6a2bc50e21cb6d34f211cdb" dependencies = [ "hbbytecode 0.1.0 (git+https://git.ablecorp.us/ableos/holey-bytes.git)", ] @@ -526,9 +527,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.60" +version = "0.1.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -737,9 +738,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.20.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "paste" @@ -899,6 +900,16 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "regalloc2" +version = "0.10.2" +source = "git+https://github.com/jakubDoka/regalloc2.git#34d8424a0d74746d231a96239fdb32e3d5ec0245" +dependencies = [ + "hashbrown", + "rustc-hash", + "smallvec", +] + [[package]] name = "regex-syntax" version = "0.8.4" diff --git a/kernel/src/arch/x86_64/gdt.rs b/kernel/src/arch/x86_64/gdt.rs index a1e77591..8edf7add 100644 --- a/kernel/src/arch/x86_64/gdt.rs +++ b/kernel/src/arch/x86_64/gdt.rs @@ -12,7 +12,7 @@ use { pub const DOUBLE_FAULT_IX: u16 = 0; const STACK_SIZE: usize = 5 * 1024; -const STACK_ALIGNMENT: usize = 4096; +const STACK_ALIGNMENT: usize = 1; pub unsafe fn init() { use x86_64::instructions::{ @@ -39,7 +39,7 @@ static TSS: Lazy = Lazy::new(|| { let stack_ptr = unsafe { let layout = alloc::alloc::Layout::from_size_align(STACK_SIZE, STACK_ALIGNMENT) .expect("Failed to create stack layout"); - let stack = alloc::alloc::alloc_zeroed(layout); + let stack = alloc::alloc::alloc(layout); VirtAddr::from_ptr(stack) + STACK_SIZE as u64 }; diff --git a/kernel/src/arch/x86_64/interrupts.rs b/kernel/src/arch/x86_64/interrupts.rs index 08e7aa0d..02b8e2ce 100644 --- a/kernel/src/arch/x86_64/interrupts.rs +++ b/kernel/src/arch/x86_64/interrupts.rs @@ -1,56 +1,52 @@ -// TODO: Turn apic keyboard interrupt into a standard ipc message use { + core::mem::MaybeUninit, log::trace, - spin::{Lazy, Mutex}, x2apic::lapic::{LocalApic, LocalApicBuilder}, x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode}, }; -pub unsafe fn init() { - trace!("Initialising IDT"); - IDT.load(); - Lazy::force(&LAPIC); - x86_64::instructions::interrupts::enable(); -} +/// Safety: Using LAPIC or IDT before init() is UB +/// Using +static mut LAPIC: LocalApic = unsafe { MaybeUninit::zeroed().assume_init() }; +static mut IDT: InterruptDescriptorTable = unsafe { MaybeUninit::zeroed().assume_init() }; #[repr(u8)] enum Interrupt { Timer = 32, - ApicErr = u8::MAX - 1, Spurious = u8::MAX, } -pub(crate) static LAPIC: Lazy> = Lazy::new(|| { - let mut lapic = LocalApicBuilder::new() +pub unsafe fn init() { + trace!("Initializing IDT and LAPIC"); + + // Initialize and load the IDT + IDT = InterruptDescriptorTable::new(); + IDT.double_fault + .set_handler_fn(double_fault) + .set_stack_index(super::gdt::DOUBLE_FAULT_IX); + IDT.page_fault.set_handler_fn(page_fault); + + IDT[Interrupt::ApicErr as u8].set_handler_fn(apic_err); + IDT[Interrupt::Spurious as u8].set_handler_fn(spurious); + IDT[Interrupt::Timer as u8].set_handler_fn(timer); + + IDT.load(); + + LAPIC = LocalApicBuilder::new() .timer_vector(Interrupt::Timer as usize) .error_vector(Interrupt::ApicErr as usize) .spurious_vector(Interrupt::Spurious as usize) .set_xapic_base( - unsafe { x2apic::lapic::xapic_base() } + x2apic::lapic::xapic_base() + super::memory::HHDM_OFFSET.load(core::sync::atomic::Ordering::Relaxed), ) .build() - .expect("failed to setup Local APIC"); - unsafe { lapic.enable() }; - Mutex::new(lapic) -}); + .expect("Failed to setup Local APIC"); + LAPIC.enable(); -static IDT: Lazy = Lazy::new(|| { - let mut idt = InterruptDescriptorTable::new(); - unsafe { - idt.double_fault - .set_handler_fn(double_fault) - .set_stack_index(super::gdt::DOUBLE_FAULT_IX); - } - idt.page_fault.set_handler_fn(page_fault); - - idt[Interrupt::ApicErr as u8].set_handler_fn(apic_err); - idt[Interrupt::Spurious as u8].set_handler_fn(spurious); - idt[Interrupt::Timer as u8].set_handler_fn(timer); - - idt -}); + x86_64::instructions::interrupts::enable(); +} extern "x86-interrupt" fn double_fault(stack_frame: InterruptStackFrame, error_code: u64) -> ! { panic!("Double fault: error code {error_code} \n{stack_frame:#?}") @@ -64,7 +60,9 @@ extern "x86-interrupt" fn page_fault( } extern "x86-interrupt" fn timer(_isf: InterruptStackFrame) { - unsafe { LAPIC.lock().end_of_interrupt() }; + unsafe { + LAPIC.end_of_interrupt(); + } } extern "x86-interrupt" fn apic_err(_: InterruptStackFrame) { @@ -72,5 +70,7 @@ extern "x86-interrupt" fn apic_err(_: InterruptStackFrame) { } extern "x86-interrupt" fn spurious(_: InterruptStackFrame) { - unsafe { LAPIC.lock().end_of_interrupt() }; + unsafe { + LAPIC.end_of_interrupt(); + } } diff --git a/kernel/src/holeybytes/ecah.rs b/kernel/src/holeybytes/ecah.rs index d966778f..7075ccd2 100644 --- a/kernel/src/holeybytes/ecah.rs +++ b/kernel/src/holeybytes/ecah.rs @@ -13,7 +13,7 @@ use { pub fn handler(vm: &mut Vm) { let ecall_number = vm.registers[2].cast::(); - + log::info!("eca called :pensive:"); // debug!("Ecall number {:?}", ecall_number); //info!("Register dump: {:?}", vm.registers); @@ -97,43 +97,35 @@ pub fn handler(vm: &mut Vm) { let msg_vec = block_read(mem_addr, length); let msg_type = msg_vec[0]; match msg_type { - 0 => { + 0 => unsafe { let size = msg_vec[1]; let addr = u16::from_le_bytes(msg_vec[2..4].try_into().unwrap()); - let value = unsafe { - match size { - 0 => x86_in::(addr) as u64, - 1 => x86_in::(addr) as u64, - 2 => x86_in::(addr) as u64, - _ => panic!("how?"), - } + let value = match size { + 0 => x86_in::(addr) as u64, + 1 => x86_in::(addr) as u64, + 2 => x86_in::(addr) as u64, + _ => panic!("Trying to read size other than: 8, 16, 32 from port."), }; - info!("Read the value {} from address {}", value, addr); + // info!("Read the value {} from address {}", value, addr); vm.registers[1] = hbvm::value::Value(value); - } - 1 => { + }, + 1 => unsafe { let size = msg_vec[1]; let addr = u16::from_le_bytes(msg_vec[2..4].try_into().unwrap()); trace!("Setting address {}", addr); - unsafe { - match size { - 0 => x86_out(addr, msg_vec[4]), - 1 => x86_out( - addr, - u16::from_le_bytes( - msg_vec[4..6].try_into().unwrap_unchecked(), - ), - ), - 2 => x86_out( - addr, - u32::from_le_bytes( - msg_vec[4..8].try_into().unwrap_unchecked(), - ), - ), - _ => panic!("How?"), - } + match size { + 0 => x86_out(addr, msg_vec[4]), + 1 => x86_out( + addr, + u16::from_le_bytes(msg_vec[4..6].try_into().unwrap_unchecked()), + ), + 2 => x86_out( + addr, + u32::from_le_bytes(msg_vec[4..8].try_into().unwrap_unchecked()), + ), + _ => panic!("How?"), } - } + }, _ => {} } } diff --git a/kernel/src/holeybytes/kernel_services/mem_serve.rs b/kernel/src/holeybytes/kernel_services/mem_serve.rs index feeba2e7..935ca9ba 100644 --- a/kernel/src/holeybytes/kernel_services/mem_serve.rs +++ b/kernel/src/holeybytes/kernel_services/mem_serve.rs @@ -1,6 +1,6 @@ use { crate::holeybytes::{kernel_services::block_read, Vm}, - alloc::alloc::{alloc_zeroed, dealloc}, + alloc::alloc::{alloc, dealloc}, core::alloc::Layout, log::{debug, info}, }; @@ -17,7 +17,7 @@ pub enum MemoryQuotaType { } fn alloc_page(vm: &mut Vm, _mem_addr: u64, _length: usize) -> Result<(), MemoryServiceError> { - let ptr = unsafe { alloc_zeroed(Layout::new::<[u8; 4096]>()) }; + let ptr = unsafe { alloc(Layout::from_size_align_unchecked(4096, 4096)) }; info!("Block address: {:?}", ptr); vm.registers[1] = hbvm::value::Value(ptr as u64); vm.registers[2] = hbvm::value::Value(4096); @@ -40,9 +40,9 @@ pub fn memory_msg_handler( log::debug!("Allocating {} pages @ {:x}", page_count, mptr); let ptr = unsafe { - alloc_zeroed(Layout::from_size_align_unchecked( + alloc(Layout::from_size_align_unchecked( page_count as usize * 4096, - 1, + 4096, )) }; @@ -59,7 +59,7 @@ pub fn memory_msg_handler( unsafe { dealloc( mptr as *mut u8, - Layout::from_size_align_unchecked(page_count as usize * 4096, 1), + Layout::from_size_align_unchecked(page_count as usize * 4096, 4096), ) } } diff --git a/kernel/src/holeybytes/mod.rs b/kernel/src/holeybytes/mod.rs index a7f6478d..9b9a1d27 100644 --- a/kernel/src/holeybytes/mod.rs +++ b/kernel/src/holeybytes/mod.rs @@ -3,7 +3,7 @@ mod kernel_services; mod mem; use { - alloc::alloc::{alloc_zeroed, dealloc}, + alloc::alloc::{alloc, dealloc}, core::{ alloc::Layout, future::Future, @@ -102,5 +102,5 @@ const fn stack_layout() -> Layout { #[inline(always)] fn allocate_stack() -> *mut u8 { - unsafe { alloc_zeroed(stack_layout()) } + unsafe { alloc(stack_layout()) } } diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 2e6d2563..1a12eeff 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -3,6 +3,7 @@ //! Akern is woefully undersupported at the moment but we are looking to add support improve hardware discovery and make our lives as kernel and operating system developers easier and better #![no_std] #![feature( + exclusive_wrapper, new_uninit, abi_x86_interrupt, alloc_error_handler, diff --git a/sysdata/programs/kvstore/README.md b/sysdata/programs/kvstore/README.md deleted file mode 100644 index b4fa24ff..00000000 --- a/sysdata/programs/kvstore/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# KVStore -This is a small single process in memory key value store. \ No newline at end of file diff --git a/sysdata/programs/kvstore/src/main.hb b/sysdata/programs/kvstore/src/main.hb deleted file mode 100644 index 1abfa97c..00000000 --- a/sysdata/programs/kvstore/src/main.hb +++ /dev/null @@ -1,37 +0,0 @@ -Message := struct { - msg_type: u8, - key: String, - value: String, -} - -/* -# Message Type -0 => Set Key type -1 => Get Key -*/ - -recv_msg:= fn(): Message { - return Message.{ - msg_type: 0, - key: "", - value: "", - } -} - -main := fn(): int { - loop { - msg := recv_msg(); - if msg.msg_type == 0 { - continue; - } - if msg.msg_type == 1 { - continue; - } - if 2 <= msg.msg_type { - error("Unexpected message type in the bagging area"); - continue; - } - } - - return 0; -} \ No newline at end of file diff --git a/sysdata/programs/ps2_driver/src/main.hb b/sysdata/programs/ps2_driver/src/main.hb index f7279782..040a41f1 100644 --- a/sysdata/programs/ps2_driver/src/main.hb +++ b/sysdata/programs/ps2_driver/src/main.hb @@ -6,28 +6,31 @@ send_byte := fn(byte: u8): u8 { } main := fn(): int { - send_byte(238) - log.info("PS/2 Driver Loaded\0") - if send_byte(238) == 238 { - log.info("PS/2 Keyboard Echoed\0") - } - a := 0 - a += 1 - if send_byte(244) == 250 { - log.info("Enabled scanning\0") - } - buf := buffer.create("XKeyboard\0") - ptr := memory.request_page(1) - prev_input := 250 - loop { - input := memory.inb(96) - if input == prev_input { - continue - } - prev_input = input - keycode_str := string.display_int(input, ptr) - log.info(string.display_int(input, ptr)) - buffer.send_message(keycode_str, buf) - } + memory.outb(96, 238) + // memory.inb(96) + + // send_byte(238) + // log.info("PS/2 Driver Loaded\0") + // if send_byte(238) == 238 { + // log.info("PS/2 Keyboard Echoed\0") + // } + // a := 0 + // a += 1 + // if send_byte(244) == 250 { + // log.info("Enabled scanning\0") + // } + // buf := buffer.create("XKeyboard\0") + // ptr := memory.request_page(1) + // prev_input := 250 + // loop { + // input := memory.inb(96) + // if input == prev_input { + // continue + // } + // prev_input = input + // keycode_str := string.display_int(input, ptr) + // log.info(string.display_int(input, ptr)) + // buffer.send_message(keycode_str, buf) + // } return 0 } \ No newline at end of file diff --git a/sysdata/programs/render_example/src/main.hb b/sysdata/programs/render_example/src/main.hb index 4f44fd85..35b2706e 100644 --- a/sysdata/programs/render_example/src/main.hb +++ b/sysdata/programs/render_example/src/main.hb @@ -1,4 +1,4 @@ -.{example} := @use("./examples/colors.hb") +.{example} := @use("./examples/amogus.hb") main := fn(): void { @inline(example)