please bugs begone

This commit is contained in:
koniifer 2024-09-16 20:15:51 +01:00
parent b021fc5b86
commit 1f4fce0f66
11 changed files with 112 additions and 144 deletions

29
Cargo.lock generated
View file

@ -390,25 +390,26 @@ dependencies = [
[[package]] [[package]]
name = "hbbytecode" name = "hbbytecode"
version = "0.1.0" 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]] [[package]]
name = "hbbytecode" name = "hbbytecode"
version = "0.1.0" 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]] [[package]]
name = "hblang" name = "hblang"
version = "0.1.0" 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 = [ dependencies = [
"hbvm 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)", "hbvm 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)",
"regalloc2",
] ]
[[package]] [[package]]
name = "hbvm" name = "hbvm"
version = "0.1.0" 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 = [ dependencies = [
"hbbytecode 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)", "hbbytecode 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)",
] ]
@ -416,7 +417,7 @@ dependencies = [
[[package]] [[package]]
name = "hbvm" name = "hbvm"
version = "0.1.0" 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 = [ dependencies = [
"hbbytecode 0.1.0 (git+https://git.ablecorp.us/ableos/holey-bytes.git)", "hbbytecode 0.1.0 (git+https://git.ablecorp.us/ableos/holey-bytes.git)",
] ]
@ -526,9 +527,9 @@ dependencies = [
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.60" version = "0.1.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220"
dependencies = [ dependencies = [
"android_system_properties", "android_system_properties",
"core-foundation-sys", "core-foundation-sys",
@ -737,9 +738,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.20.0" version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]] [[package]]
name = "paste" name = "paste"
@ -899,6 +900,16 @@ dependencies = [
"bitflags 1.3.2", "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]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.8.4" version = "0.8.4"

View file

@ -12,7 +12,7 @@ use {
pub const DOUBLE_FAULT_IX: u16 = 0; pub const DOUBLE_FAULT_IX: u16 = 0;
const STACK_SIZE: usize = 5 * 1024; const STACK_SIZE: usize = 5 * 1024;
const STACK_ALIGNMENT: usize = 4096; const STACK_ALIGNMENT: usize = 1;
pub unsafe fn init() { pub unsafe fn init() {
use x86_64::instructions::{ use x86_64::instructions::{
@ -39,7 +39,7 @@ static TSS: Lazy<TaskStateSegment> = Lazy::new(|| {
let stack_ptr = unsafe { let stack_ptr = unsafe {
let layout = alloc::alloc::Layout::from_size_align(STACK_SIZE, STACK_ALIGNMENT) let layout = alloc::alloc::Layout::from_size_align(STACK_SIZE, STACK_ALIGNMENT)
.expect("Failed to create stack layout"); .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 VirtAddr::from_ptr(stack) + STACK_SIZE as u64
}; };

View file

@ -1,56 +1,52 @@
// TODO: Turn apic keyboard interrupt into a standard ipc message
use { use {
core::mem::MaybeUninit,
log::trace, log::trace,
spin::{Lazy, Mutex},
x2apic::lapic::{LocalApic, LocalApicBuilder}, x2apic::lapic::{LocalApic, LocalApicBuilder},
x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode}, x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode},
}; };
pub unsafe fn init() { /// Safety: Using LAPIC or IDT before init() is UB
trace!("Initialising IDT"); /// Using
IDT.load(); static mut LAPIC: LocalApic = unsafe { MaybeUninit::zeroed().assume_init() };
Lazy::force(&LAPIC); static mut IDT: InterruptDescriptorTable = unsafe { MaybeUninit::zeroed().assume_init() };
x86_64::instructions::interrupts::enable();
}
#[repr(u8)] #[repr(u8)]
enum Interrupt { enum Interrupt {
Timer = 32, Timer = 32,
ApicErr = u8::MAX - 1, ApicErr = u8::MAX - 1,
Spurious = u8::MAX, Spurious = u8::MAX,
} }
pub(crate) static LAPIC: Lazy<Mutex<LocalApic>> = Lazy::new(|| { pub unsafe fn init() {
let mut lapic = LocalApicBuilder::new() 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) .timer_vector(Interrupt::Timer as usize)
.error_vector(Interrupt::ApicErr as usize) .error_vector(Interrupt::ApicErr as usize)
.spurious_vector(Interrupt::Spurious as usize) .spurious_vector(Interrupt::Spurious as usize)
.set_xapic_base( .set_xapic_base(
unsafe { x2apic::lapic::xapic_base() } x2apic::lapic::xapic_base()
+ super::memory::HHDM_OFFSET.load(core::sync::atomic::Ordering::Relaxed), + super::memory::HHDM_OFFSET.load(core::sync::atomic::Ordering::Relaxed),
) )
.build() .build()
.expect("failed to setup Local APIC"); .expect("Failed to setup Local APIC");
unsafe { lapic.enable() }; LAPIC.enable();
Mutex::new(lapic)
});
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| { x86_64::instructions::interrupts::enable();
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
});
extern "x86-interrupt" fn double_fault(stack_frame: InterruptStackFrame, error_code: u64) -> ! { extern "x86-interrupt" fn double_fault(stack_frame: InterruptStackFrame, error_code: u64) -> ! {
panic!("Double fault: error code {error_code} \n{stack_frame:#?}") 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) { 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) { extern "x86-interrupt" fn apic_err(_: InterruptStackFrame) {
@ -72,5 +70,7 @@ extern "x86-interrupt" fn apic_err(_: InterruptStackFrame) {
} }
extern "x86-interrupt" fn spurious(_: InterruptStackFrame) { extern "x86-interrupt" fn spurious(_: InterruptStackFrame) {
unsafe { LAPIC.lock().end_of_interrupt() }; unsafe {
LAPIC.end_of_interrupt();
}
} }

View file

@ -13,7 +13,7 @@ use {
pub fn handler(vm: &mut Vm) { pub fn handler(vm: &mut Vm) {
let ecall_number = vm.registers[2].cast::<u64>(); let ecall_number = vm.registers[2].cast::<u64>();
log::info!("eca called :pensive:");
// debug!("Ecall number {:?}", ecall_number); // debug!("Ecall number {:?}", ecall_number);
//info!("Register dump: {:?}", vm.registers); //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_vec = block_read(mem_addr, length);
let msg_type = msg_vec[0]; let msg_type = msg_vec[0];
match msg_type { match msg_type {
0 => { 0 => unsafe {
let size = msg_vec[1]; let size = msg_vec[1];
let addr = u16::from_le_bytes(msg_vec[2..4].try_into().unwrap()); let addr = u16::from_le_bytes(msg_vec[2..4].try_into().unwrap());
let value = unsafe { let value = match size {
match size { 0 => x86_in::<u8>(addr) as u64,
0 => x86_in::<u8>(addr) as u64, 1 => x86_in::<u16>(addr) as u64,
1 => x86_in::<u16>(addr) as u64, 2 => x86_in::<u32>(addr) as u64,
2 => x86_in::<u32>(addr) as u64, _ => panic!("Trying to read size other than: 8, 16, 32 from port."),
_ => panic!("how?"),
}
}; };
info!("Read the value {} from address {}", value, addr); // info!("Read the value {} from address {}", value, addr);
vm.registers[1] = hbvm::value::Value(value); vm.registers[1] = hbvm::value::Value(value);
} },
1 => { 1 => unsafe {
let size = msg_vec[1]; let size = msg_vec[1];
let addr = u16::from_le_bytes(msg_vec[2..4].try_into().unwrap()); let addr = u16::from_le_bytes(msg_vec[2..4].try_into().unwrap());
trace!("Setting address {}", addr); trace!("Setting address {}", addr);
unsafe { match size {
match size { 0 => x86_out(addr, msg_vec[4]),
0 => x86_out(addr, msg_vec[4]), 1 => x86_out(
1 => x86_out( addr,
addr, u16::from_le_bytes(msg_vec[4..6].try_into().unwrap_unchecked()),
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()),
2 => x86_out( ),
addr, _ => panic!("How?"),
u32::from_le_bytes(
msg_vec[4..8].try_into().unwrap_unchecked(),
),
),
_ => panic!("How?"),
}
} }
} },
_ => {} _ => {}
} }
} }

View file

@ -1,6 +1,6 @@
use { use {
crate::holeybytes::{kernel_services::block_read, Vm}, crate::holeybytes::{kernel_services::block_read, Vm},
alloc::alloc::{alloc_zeroed, dealloc}, alloc::alloc::{alloc, dealloc},
core::alloc::Layout, core::alloc::Layout,
log::{debug, info}, log::{debug, info},
}; };
@ -17,7 +17,7 @@ pub enum MemoryQuotaType {
} }
fn alloc_page(vm: &mut Vm, _mem_addr: u64, _length: usize) -> Result<(), MemoryServiceError> { 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); info!("Block address: {:?}", ptr);
vm.registers[1] = hbvm::value::Value(ptr as u64); vm.registers[1] = hbvm::value::Value(ptr as u64);
vm.registers[2] = hbvm::value::Value(4096); vm.registers[2] = hbvm::value::Value(4096);
@ -40,9 +40,9 @@ pub fn memory_msg_handler(
log::debug!("Allocating {} pages @ {:x}", page_count, mptr); log::debug!("Allocating {} pages @ {:x}", page_count, mptr);
let ptr = unsafe { let ptr = unsafe {
alloc_zeroed(Layout::from_size_align_unchecked( alloc(Layout::from_size_align_unchecked(
page_count as usize * 4096, page_count as usize * 4096,
1, 4096,
)) ))
}; };
@ -59,7 +59,7 @@ pub fn memory_msg_handler(
unsafe { unsafe {
dealloc( dealloc(
mptr as *mut u8, 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),
) )
} }
} }

View file

@ -3,7 +3,7 @@ mod kernel_services;
mod mem; mod mem;
use { use {
alloc::alloc::{alloc_zeroed, dealloc}, alloc::alloc::{alloc, dealloc},
core::{ core::{
alloc::Layout, alloc::Layout,
future::Future, future::Future,
@ -102,5 +102,5 @@ const fn stack_layout() -> Layout {
#[inline(always)] #[inline(always)]
fn allocate_stack() -> *mut u8 { fn allocate_stack() -> *mut u8 {
unsafe { alloc_zeroed(stack_layout()) } unsafe { alloc(stack_layout()) }
} }

View file

@ -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 //! 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] #![no_std]
#![feature( #![feature(
exclusive_wrapper,
new_uninit, new_uninit,
abi_x86_interrupt, abi_x86_interrupt,
alloc_error_handler, alloc_error_handler,

View file

@ -1,2 +0,0 @@
# KVStore
This is a small single process in memory key value store.

View file

@ -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;
}

View file

@ -6,28 +6,31 @@ send_byte := fn(byte: u8): u8 {
} }
main := fn(): int { main := fn(): int {
send_byte(238) memory.outb(96, 238)
log.info("PS/2 Driver Loaded\0") // memory.inb(96)
if send_byte(238) == 238 {
log.info("PS/2 Keyboard Echoed\0") // send_byte(238)
} // log.info("PS/2 Driver Loaded\0")
a := 0 // if send_byte(238) == 238 {
a += 1 // log.info("PS/2 Keyboard Echoed\0")
if send_byte(244) == 250 { // }
log.info("Enabled scanning\0") // a := 0
} // a += 1
buf := buffer.create("XKeyboard\0") // if send_byte(244) == 250 {
ptr := memory.request_page(1) // log.info("Enabled scanning\0")
prev_input := 250 // }
loop { // buf := buffer.create("XKeyboard\0")
input := memory.inb(96) // ptr := memory.request_page(1)
if input == prev_input { // prev_input := 250
continue // loop {
} // input := memory.inb(96)
prev_input = input // if input == prev_input {
keycode_str := string.display_int(input, ptr) // continue
log.info(string.display_int(input, ptr)) // }
buffer.send_message(keycode_str, buf) // 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 return 0
} }

View file

@ -1,4 +1,4 @@
.{example} := @use("./examples/colors.hb") .{example} := @use("./examples/amogus.hb")
main := fn(): void { main := fn(): void {
@inline(example) @inline(example)