a touch of fiddling

This commit is contained in:
koniifer 2024-09-09 01:42:11 +01:00
parent 9b34e19005
commit fd155ea26a
24 changed files with 617 additions and 544 deletions

716
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,9 @@
[workspace]
resolver = "2"
members = [ "dev","kernel", "repbuild"]
members = ["dev", "kernel", "repbuild"]
[profile.release]
strip = "symbols"
codegen-units = 1
lto = true
panic = "abort"

View file

@ -5,17 +5,17 @@ version = "0.2.0"
[dependencies]
embedded-graphics = "0.7"
embedded-graphics = "0.8"
hbvm.git = "https://git.ablecorp.us/ableos/holey-bytes"
log = "0.4"
spin = "0.9"
uart_16550 = "0.2"
uart_16550 = "0.3"
slab = { version = "0.4", default-features = false }
xml.git = "https://git.ablecorp.us/ableos/ableos_userland"
versioning.git = "https://git.ablecorp.us/ableos/ableos_userland"
able_graphics_library.git = "https://git.ablecorp.us/ableos/ableos_userland"
hashbrown = "*"
kiam = "0.1.1"
# able_graphics_library.git = "https://git.ablecorp.us/ableos/ableos_userland"
hashbrown = "0.14"
kiam = "0.1"
[dependencies.limine]
version = "0.1"
@ -26,12 +26,12 @@ version = "0.3"
default-features = false
features = ["alloc"]
[dependencies.clparse]
git = "https://git.ablecorp.us/ableos/ableos_userland"
default-features = false
# [dependencies.clparse]
# git = "https://git.ablecorp.us/ableos/ableos_userland"
# default-features = false
[dependencies.derive_more]
version = "0.99"
version = "1"
default-features = false
features = [
"add",
@ -48,9 +48,9 @@ features = [
[target.'cfg(target_arch = "x86_64")'.dependencies]
x86_64 = "0.14"
x86_64 = "0.15"
x2apic = "0.4"
virtio-drivers = "0.4.0"
virtio-drivers = "0.7"
# rdrand = "*"
rdrand = { version = "0.8", default-features = false }

View file

@ -39,7 +39,7 @@ static TSS: Lazy<TaskStateSegment> = Lazy::new(|| {
alloc::alloc::Layout::from_size_align(SIZE, 1).expect("stack pointer"),
)
};
VirtAddr::from_ptr(stack) + SIZE
VirtAddr::from_ptr(stack) + SIZE as u64
};
tss
});
@ -47,9 +47,9 @@ static TSS: Lazy<TaskStateSegment> = Lazy::new(|| {
static GDT: Lazy<(GlobalDescriptorTable, Selectors)> = Lazy::new(|| {
let mut gdt = GlobalDescriptorTable::new();
let sels = Selectors {
kcode: gdt.add_entry(Descriptor::kernel_code_segment()),
kdata: gdt.add_entry(Descriptor::kernel_data_segment()),
tss: gdt.add_entry(Descriptor::tss_segment(&TSS)),
kcode: gdt.append(Descriptor::kernel_code_segment()),
kdata: gdt.append(Descriptor::kernel_data_segment()),
tss: gdt.append(Descriptor::tss_segment(&TSS)),
};
(gdt, sels)
});

View file

@ -45,9 +45,9 @@ static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
}
idt.page_fault.set_handler_fn(page_fault);
idt[Interrupt::ApicErr as usize].set_handler_fn(apic_err);
idt[Interrupt::Spurious as usize].set_handler_fn(spurious);
idt[Interrupt::Timer as usize].set_handler_fn(timer);
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
});

View file

@ -1,11 +1,6 @@
//! Logging (as in terms of console / serial output)
#![allow(deprecated)]
use {
core::fmt::Write,
limine::{TerminalRequest, TerminalResponse},
spin::{Lazy, Mutex},
uart_16550::SerialPort,
};
use {core::fmt::Write, spin::Mutex, uart_16550::SerialPort};
pub static SERIAL_CONSOLE: Mutex<SerialPort> = Mutex::new(unsafe { SerialPort::new(0x3F8) });

View file

@ -1,7 +1,4 @@
use {
crate::bootmodules::BootModule, core::arch::asm, embedded_graphics::pixelcolor::Rgb888,
log::warn, rdrand::RdSeed,
};
use {crate::bootmodules::BootModule, core::arch::asm, log::warn, rdrand::RdSeed};
pub mod memory;
mod cpuid;
@ -208,7 +205,7 @@ pub fn hardware_random_u64() -> u64 {
trace!("Random {}", ret);
return ret;
}
Err(err) => {
Err(_) => {
warn!("RDRand not supported.");
// Try rdseed
let gen = RdSeed::new();
@ -218,7 +215,7 @@ pub fn hardware_random_u64() -> u64 {
trace!("Random {}", ret);
return ret;
}
Err(err) => {
Err(_) => {
panic!("Neither RDRand or RDSeed are supported")
}
}
@ -235,7 +232,7 @@ pub fn hardware_random_u32() -> u32 {
trace!("Random {}", ret);
return ret;
}
Err(err) => {
Err(_) => {
warn!("RDRand not supported.");
// Try rdseed
let gen = RdSeed::new();
@ -245,7 +242,7 @@ pub fn hardware_random_u32() -> u32 {
trace!("Random {}", ret);
return ret;
}
Err(err) => {
Err(_) => {
panic!("Neither RDRand or RDSeed are supported")
}
}
@ -255,6 +252,7 @@ pub fn hardware_random_u32() -> u32 {
pub fn get_edid() {}
#[allow(unused)]
pub fn register_dump() {
let rax: u64;
let rbx: u64 = 0;

View file

@ -1,10 +1,10 @@
use {
crate::alloc::string::ToString,
// crate::alloc::string::ToString,
alloc::{string::String, vec::Vec},
clparse::Arguments,
core::fmt::{Debug, Display},
log::trace,
xml::XMLElement,
// clparse::Arguments,
// core::fmt::{Debug, Display},
// log::trace,
// xml::XMLElement,
};
pub type BootModules = Vec<BootModule>;
@ -19,18 +19,18 @@ impl BootModule {
}
}
pub fn build_cmd<T: Display + Debug>(name: T, cmdline: T) -> XMLElement {
let mut cmdline = cmdline.to_string();
cmdline.pop();
cmdline.remove(0);
// pub fn build_cmd<T: Display + Debug>(name: T, cmdline: T) -> XMLElement {
// let mut cmdline = cmdline.to_string();
// cmdline.pop();
// cmdline.remove(0);
let cmd = Arguments::parse(cmdline.to_string()).unwrap();
trace!("Cmdline: {cmd:?}");
// let cmd = Arguments::parse(cmdline.to_string()).unwrap();
// trace!("Cmdline: {cmd:?}");
let mut clo = XMLElement::new(name);
for (key, value) in cmd.arguments {
clo.set_attribute(key, value);
}
trace!("command line object: {:?}", clo);
clo
}
// let mut clo = XMLElement::new(name);
// for (key, value) in cmd.arguments {
// clo.set_attribute(key, value);
// }
// trace!("command line object: {:?}", clo);
// clo
// }

View file

@ -1,18 +1,10 @@
//! Environment call handling routines
use core::borrow::Borrow;
use crate::{
allocator,
holeybytes::kernel_services::{
block_read,
service_definition_service::{sds_msg_handler, SERVICES},
},
};
use crate::holeybytes::kernel_services::{block_read, service_definition_service::sds_msg_handler};
use {
super::{mem::Memory, Vm},
crate::{arch, holeybytes::mem, ipc::buffer::IpcBuffer, kmain::IPC_BUFFERS},
super::Vm,
crate::{arch, ipc::buffer::IpcBuffer, kmain::IPC_BUFFERS},
alloc::string::String,
log::{debug, error, info, trace, warn},
};
@ -78,7 +70,7 @@ pub fn handler(vm: &mut Vm) {
},
1 => match log_msg_handler(vm, mem_addr, length) {
Ok(()) => {}
Err(err) => log::error!("Improper log format"),
Err(_) => log::error!("Improper log format"),
},
2 => {
use crate::holeybytes::kernel_services::mem_serve::memory_msg_handler;
@ -107,7 +99,7 @@ pub fn handler(vm: &mut Vm) {
x86_64::instructions::port::Port::new(address).write(value);
}
let mut msg_vec = block_read(mem_addr, length);
let msg_vec = block_read(mem_addr, length);
let msg_type = msg_vec[0];
match msg_type {
0 => 'wow: {
@ -203,7 +195,7 @@ pub fn handler(vm: &mut Vm) {
let max_length = vm.registers[5].cast::<u64>();
let mut buffs = IPC_BUFFERS.lock();
let mut buff: &mut IpcBuffer;
let buff: &mut IpcBuffer;
if buffs.get_mut(&buffer_id).is_some() {
buff = buffs.get_mut(&buffer_id).unwrap();
@ -252,7 +244,7 @@ pub fn handler(vm: &mut Vm) {
}
}
fn log_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), LogError> {
fn log_msg_handler(_vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), LogError> {
// let message_length = 8 + 8 + 8;
// log::info!("Mem Addr 0x{:x?} length {}", mem_addr, length);
let mut msg_vec = block_read(mem_addr, length);
@ -261,7 +253,7 @@ fn log_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), LogE
match String::from_utf8(msg_vec) {
Ok(strr) => {
// use LogLevel::*;
let ll = match log_level {
let _ll = match log_level {
0 | 48 => error!("{}", strr),
1 | 49 => warn!("{}", strr),
2 | 50 => info!("{}", strr),
@ -285,8 +277,8 @@ pub enum LogError {
NoMessages,
InvalidLogFormat,
}
use {alloc::vec, log::Record};
// use {alloc::vec, log::Record};
// fn memory_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), LogError> {
// let mut val = alloc::vec::Vec::new();
// for _ in 0..4096 {

View file

@ -1,19 +1,10 @@
use {
crate::holeybytes::{
ecah::LogError,
kernel_services::{block_read, mem_serve},
Vm,
},
alloc::{
alloc::{alloc_zeroed, dealloc},
vec::Vec,
},
crate::holeybytes::{kernel_services::block_read, Vm},
alloc::alloc::{alloc_zeroed, dealloc},
core::alloc::Layout,
log::{debug, info},
};
use alloc::vec;
pub enum MemoryServiceError {
InvalidMemoryFormat,
}
@ -25,7 +16,7 @@ pub enum MemoryQuotaType {
KillQuota = 3,
}
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]>()) };
info!("Block address: {:?}", ptr);
vm.registers[1] = hbvm::value::Value(ptr as u64);
@ -38,7 +29,7 @@ pub fn memory_msg_handler(
mem_addr: u64,
length: usize,
) -> Result<(), MemoryServiceError> {
let mut msg_vec = block_read(mem_addr, length);
let msg_vec = block_read(mem_addr, length);
let msg_type = msg_vec[0];
match msg_type {
0 => {
@ -84,7 +75,7 @@ pub fn memory_msg_handler(
let hid_raw: [u8; 8] = msg_vec[2..10].try_into().unwrap();
let hid: u64 = u64::from_le_bytes(hid_raw);
let pid_raw: [u8; 8] = msg_vec[10..18].try_into().unwrap();
let pid: u64 = u64::from_le_bytes(hid_raw);
let pid: u64 = u64::from_le_bytes(pid_raw);
debug!(
"Setting HID-{:x}:PID-{:x}'s quota type to {:?}",
hid, pid, quota_type

View file

@ -1,4 +1,4 @@
use alloc::{vec, vec::Vec};
use alloc::vec::Vec;
pub mod mem_serve;
pub mod service_definition_service;

View file

@ -1,12 +1,8 @@
use {
crate::{
alloc::string::ToString,
arch::hardware_random_u64,
holeybytes::{ecah::LogError, kernel_services::block_read, Vm},
ipc::{
buffer::IpcBuffer,
protocol::{self, Protocol},
},
ipc::{buffer::IpcBuffer, protocol::Protocol},
kmain::IPC_BUFFERS,
},
alloc::string::String,
@ -100,7 +96,7 @@ fn sds_create_service(protocol: String) -> u64 {
}
fn sds_search_service(protocol: String) -> u64 {
let mut services = SERVICES.lock();
let services = SERVICES.lock();
let compare = Protocol::from(protocol.clone());
for (bid, protocol_canidate) in &services.0 {
trace!("BID-{bid} protocol_canidate {:?}", protocol_canidate);

View file

@ -7,7 +7,7 @@
use hbvm::mem::Address;
fn calc_start_of_page(ptr: u64) -> u64 {
let mut page_aligned = false;
let _page_aligned = false;
if ptr % 4096 == 0 {
// page_aligned = true;
return ptr / 4096;
@ -21,11 +21,11 @@ pub struct Memory {
impl Memory {
#[cfg(target_arch = "x86_64")]
fn read_device(addr: Address) {
unsafe {
//
// x86_64::instructions::port::Port::new(addr.get()).read()
}
fn read_device(_addr: Address) {
//unsafe {
//
// x86_64::instructions::port::Port::new(addr.get()).read()
//}
}
}
@ -37,7 +37,6 @@ impl hbvm::mem::Memory for Memory {
target: *mut u8,
count: usize,
) -> Result<(), hbvm::mem::LoadError> {
use log::{error, info};
if addr.get() % 4096 == 0 {}
core::ptr::copy(addr.get() as *const u8, target, count);
Ok(())

View file

@ -3,30 +3,33 @@ mod kernel_services;
mod mem;
use {
crate::{arch, ipc::buffer::IpcBuffer, kmain::IPC_BUFFERS},
alloc::boxed::Box,
core::{default, future::Future, marker::PhantomData, ptr::NonNull, task::Poll},
core::{
alloc::Layout,
future::Future,
marker::PhantomData,
pin::Pin,
ptr::NonNull,
task::{Context, Poll},
},
hbvm::{
mem::{
softpaging::{icache::ICache, HandlePageFault, SoftPagedMem},
Address, Memory,
},
mem::{softpaging::HandlePageFault, Address},
VmRunError, VmRunOk,
},
log::{debug, error, info, trace, warn},
log::error,
};
const STACK_SIZE: usize = 1024 * 1024;
const TIMER_QUOTIENT: usize = 100;
const TIMER_QUOTIENT: usize = 1000;
type Vm = hbvm::Vm<mem::Memory, TIMER_QUOTIENT>;
pub struct ExecThread<'p> {
vm: Vm,
stack_bottom: *mut u8,
stack_bottom: NonNull<u8>,
_phantom: PhantomData<&'p [u8]>,
}
unsafe impl<'p> Send for ExecThread<'p> {}
impl<'p> ExecThread<'p> {
pub fn set_arguments(&mut self, ptr: u64, length: u64) {
self.vm.registers[1] = hbvm::value::Value(ptr);
@ -34,55 +37,60 @@ impl<'p> ExecThread<'p> {
}
pub unsafe fn new(program: &'p [u8], entrypoint: Address) -> Self {
let mut vm = unsafe {
Vm::new(
mem::Memory {},
Address::new(program.as_ptr() as u64 + entrypoint.get()),
)
};
let mut vm = Vm::new(
mem::Memory {},
Address::new(program.as_ptr() as u64 + entrypoint.get()),
);
let stack_bottom = unsafe { allocate_stack().as_ptr() };
vm.write_reg(254, (stack_bottom as usize + STACK_SIZE - 1) as u64);
let stack_bottom = allocate_stack();
vm.write_reg(
254,
(stack_bottom.as_ptr() as usize + STACK_SIZE - 1) as u64,
);
ExecThread {
vm,
stack_bottom,
_phantom: Default::default(),
_phantom: PhantomData,
}
}
}
impl<'p> Drop for ExecThread<'p> {
fn drop(&mut self) {
unsafe { alloc::alloc::dealloc(self.stack_bottom, stack_layout()) };
unsafe { alloc::alloc::dealloc(self.stack_bottom.as_ptr(), stack_layout()) };
}
}
impl<'p> Future for ExecThread<'p> {
type Output = Result<(), VmRunError>;
fn poll(
mut self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> Poll<Self::Output> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.vm.run() {
Err(err) => {
log::error!("HBVM Error\r\nRegister dump: {:?}", self.vm.registers,);
return Poll::Ready(Err(err));
error!("HBVM Error\r\nRegister dump: {:?}", self.vm.registers);
Poll::Ready(Err(err))
}
Ok(VmRunOk::End) => Poll::Ready(Ok(())),
Ok(VmRunOk::Ecall) => {
ecah::handler(&mut self.vm);
cx.waker().wake_by_ref();
Poll::Pending
}
Ok(VmRunOk::Timer) => {
cx.waker().wake_by_ref();
Poll::Pending
}
Ok(VmRunOk::End) => return Poll::Ready(Ok(())),
Ok(VmRunOk::Ecall) => ecah::handler(&mut self.vm),
Ok(VmRunOk::Timer) => (),
Ok(VmRunOk::Breakpoint) => {
log::error!(
error!(
"HBVM Debug breakpoint\r\nRegister dump: {:?}",
self.vm.registers,
self.vm.registers
);
cx.waker().wake_by_ref();
Poll::Pending
}
}
cx.waker().wake_by_ref();
Poll::Pending
}
}
@ -91,33 +99,23 @@ impl HandlePageFault for PageFaultHandler {
fn page_fault(
&mut self,
reason: hbvm::mem::MemoryAccessReason,
pagetable: &mut hbvm::mem::softpaging::paging::PageTable,
_pagetable: &mut hbvm::mem::softpaging::paging::PageTable,
vaddr: hbvm::mem::Address,
size: hbvm::mem::softpaging::PageSize,
dataptr: *mut u8,
) -> bool
where
Self: Sized,
{
log::error!(
"REASON: {reason} \
vaddr: {vaddr} \
size: {size:?} \
Dataptr {dataptr:p}",
);
) -> bool {
error!("REASON: {reason} vaddr: {vaddr} size: {size:?} Dataptr {dataptr:p}");
false
}
}
const fn stack_layout() -> core::alloc::Layout {
unsafe { alloc::alloc::Layout::from_size_align_unchecked(STACK_SIZE, 4096) }
const fn stack_layout() -> Layout {
unsafe { Layout::from_size_align_unchecked(STACK_SIZE, 4096) }
}
fn allocate_stack() -> NonNull<u8> {
let layout = stack_layout();
match NonNull::new(unsafe { alloc::alloc::alloc_zeroed(layout) }) {
Some(ptr) => ptr,
None => alloc::alloc::handle_alloc_error(layout),
}
NonNull::new(unsafe { alloc::alloc::alloc_zeroed(layout) })
.unwrap_or_else(|| alloc::alloc::handle_alloc_error(layout))
}

View file

@ -24,7 +24,7 @@ impl IpcBuffer {
length
);
match (bounded, length) {
(false, a) => {
(false, ..) => {
let buftype = BufferTypes::Unbound(SegQueue::new());
Self {

View file

@ -1,7 +1,6 @@
use {
alloc::{string::String, vec::Vec},
hashbrown::HashMap,
log::info,
};
#[derive(Debug, PartialEq, Clone)]
pub struct Type {}

View file

@ -2,27 +2,25 @@
use {
crate::{
arch::{hardware_random_u64, logging::SERIAL_CONSOLE},
bootmodules::{build_cmd, BootModules},
capabilities,
arch::hardware_random_u64,
bootmodules::BootModules,
//bootmodules::build_cmd,
device_tree::DeviceTree,
holeybytes::ExecThread,
ipc::buffer::{self, IpcBuffer},
ipc::buffer::IpcBuffer,
},
alloc::format,
hashbrown::HashMap,
hbvm::mem::Address,
limine::{Framebuffer, FramebufferRequest, NonNullPtr},
log::{debug, info, trace},
log::{debug, info},
spin::{Lazy, Mutex},
xml::XMLElement,
};
pub fn kmain(cmdline: &str, boot_modules: BootModules) -> ! {
pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! {
debug!("Entered kmain");
let kcmd = build_cmd("Kernel Command Line", cmdline);
trace!("Cmdline: {kcmd:?}");
// let kcmd = build_cmd("Kernel Command Line", cmdline);
// trace!("Cmdline: {kcmd:?}");
// for (i, bm) in boot_modules.iter().enumerate() {
// let name = format!("module-{}", i);
@ -108,7 +106,6 @@ pub static DEVICE_TREE: Lazy<Mutex<DeviceTree>> = Lazy::new(|| {
});
pub static FB_REQ: FramebufferRequest = FramebufferRequest::new(0);
use alloc::vec::Vec;
pub type IpcBuffers = HashMap<u64, IpcBuffer>;
pub static IPC_BUFFERS: Lazy<Mutex<IpcBuffers>> = Lazy::new(|| {
let mut bufs = HashMap::new();

View file

@ -5,7 +5,6 @@
#![feature(
abi_x86_interrupt,
alloc_error_handler,
panic_info_message,
ptr_sub_ptr,
custom_test_frameworks,
naked_functions,
@ -40,6 +39,7 @@ pub const VERSION: Version = Version {
};
#[panic_handler]
#[cfg(target_os = "none")]
fn panic(info: &core::panic::PanicInfo) -> ! {
arch::register_dump();
@ -52,10 +52,8 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
));
}
if let Some(msg) = info.message() {
let _ = crate::arch::log(format_args!("{msg}\r\n"));
}
let msg = info.message();
let _ = crate::arch::log(format_args!("{msg}\r\n"));
loop {}
}

View file

@ -1,3 +1,4 @@
#![allow(deprecated)]
// TODO: Add a logger api with logger levels and various outputs
pub static TERMINAL_LOGGER: Lazy<Mutex<TermLogger>> = Lazy::new(|| Mutex::new(TermLogger::new()));

View file

@ -1,7 +1,6 @@
//! The Memory Manager
use alloc::collections::VecDeque;
use derive_more::*;
use {alloc::collections::VecDeque, derive_more::*};
pub use crate::arch::PAGE_SIZE;
pub const MAX_ORDER: usize = 10;
@ -44,7 +43,7 @@ pub const MAX_ORDER: usize = 10;
Sum,
UpperHex,
)]
#[display(fmt = "0x{:x}", _0)]
#[display("0x{:x}", _0)]
#[from(forward)]
pub struct VirtualAddress(usize);
@ -55,11 +54,11 @@ impl VirtualAddress {
pub fn vpns(&self) -> [usize; 3] {
[
// [20:12]
(self.0 >> 12) & 0x1ff,
(self.0 >> 12) & 0x1FF,
// [29:21]
(self.0 >> 21) & 0x1ff,
(self.0 >> 21) & 0x1FF,
// [38:30]
(self.0 >> 30) & 0x1ff,
(self.0 >> 30) & 0x1FF,
]
}
@ -114,7 +113,7 @@ impl VirtualAddress {
Sum,
UpperHex,
)]
#[display(fmt = "0x{:x}", _0)]
#[display("0x{:x}", _0)]
#[from(forward)]
pub struct PhysicalAddress(usize);
@ -125,11 +124,11 @@ impl PhysicalAddress {
pub fn ppns(&self) -> [usize; 3] {
[
// [20:12]
(self.0 >> 12) & 0x1ff,
(self.0 >> 12) & 0x1FF,
// [29:21]
(self.0 >> 21) & 0x1ff,
(self.0 >> 21) & 0x1FF,
// [55:30]
(self.0 >> 30) & 0x3ffffff,
(self.0 >> 30) & 0x3FFFFFF,
]
}

View file

@ -1,26 +1,18 @@
#![allow(unused)]
use {
alloc::{boxed::Box, collections::BTreeMap, sync::Arc, task::Wake},
alloc::{boxed::Box, sync::Arc, task::Wake},
core::{
future::Future,
pin::Pin,
task::{Context, Poll, Waker},
},
crossbeam_queue::SegQueue,
kiam::when,
hashbrown::HashMap,
slab::Slab,
spin::RwLock,
spin::Mutex,
};
static SPAWN_QUEUE: RwLock<Option<SpawnQueue>> = RwLock::new(None);
pub fn spawn(future: impl Future<Output = ()> + Send + 'static) {
match &*SPAWN_QUEUE.read() {
Some(s) => s.push(Task::new(future)),
None => panic!("no task executor is running"),
}
}
pub fn yield_now() -> impl Future<Output = ()> {
struct YieldNow(bool);
impl Future for YieldNow {
@ -42,61 +34,51 @@ pub fn yield_now() -> impl Future<Output = ()> {
#[derive(Default)]
pub struct Executor {
tasks: Slab<Task>,
queue: TaskQueue,
to_spawn: SpawnQueue,
wakers: BTreeMap<TaskId, Waker>,
tasks: Slab<Task>,
queue: Arc<SegQueue<TaskId>>,
wakers: HashMap<TaskId, Waker>, // Keeping HashMap as it is more performant for lookups.
}
impl Executor {
pub fn new() -> Self {
Self {
tasks: Slab::new(),
queue: Arc::new(SegQueue::new()),
wakers: HashMap::new(),
}
}
pub fn spawn(&mut self, future: impl Future<Output = ()> + Send + 'static) {
self.queue
.push(TaskId(self.tasks.insert(Task::new(future))));
let task_id = TaskId(self.tasks.insert(Task::new(future)));
self.queue.push(task_id);
}
pub fn run(&mut self) {
{
let mut global_spawner = SPAWN_QUEUE.write();
if global_spawner.is_some() {
panic!("Task executor is already running");
}
*global_spawner = Some(Arc::clone(&self.to_spawn));
}
loop {
when! {
let Some(id) = self
.to_spawn
.pop()
.map(|t| TaskId(self.tasks.insert(t)))
.or_else(|| self.queue.pop())
=> {
let Some(task) = self.tasks.get_mut(id.0) else {
panic!("Attempted to get task from empty slot: {}", id.0);
};
let mut cx = Context::from_waker(self.wakers.entry(id).or_insert_with(|| {
while let Some(id) = self.queue.pop() {
if let Some(task) = self.tasks.get_mut(id.0) {
let waker = self.wakers.entry(id).or_insert_with(|| {
Waker::from(Arc::new(TaskWaker {
id,
queue: Arc::clone(&self.queue),
}))
}));
});
match task.poll(&mut cx) {
Poll::Ready(()) => {
self.tasks.remove(id.0);
self.wakers.remove(&id);
}
Poll::Pending => (),
let mut cx = Context::from_waker(waker);
if let Poll::Ready(()) = task.poll(&mut cx) {
self.tasks.remove(id.0);
self.wakers.remove(&id);
}
},
self.tasks.is_empty() => break,
_ => (),
} else {
panic!("Attempted to get task from empty slot: {}", id.0);
}
}
if self.tasks.is_empty() {
break;
}
}
*SPAWN_QUEUE.write() = None;
}
}
@ -117,21 +99,18 @@ impl Task {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Hash, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct TaskId(usize);
type TaskQueue = Arc<SegQueue<TaskId>>;
type SpawnQueue = Arc<SegQueue<Task>>;
struct TaskWaker {
id: TaskId,
queue: TaskQueue,
queue: Arc<SegQueue<TaskId>>,
}
impl Wake for TaskWaker {
fn wake(self: Arc<Self>) {
log::trace!("Woke Task-{:?}", self.id);
self.wake_by_ref();
self.queue.push(self.id);
}
fn wake_by_ref(self: &Arc<Self>) {

View file

@ -4,15 +4,15 @@ version = "0.2.0"
edition = "2021"
[dependencies]
str-reader = "0.1.2"
derive_more = "0.99"
error-stack = "0.4"
str-reader = "0.1"
derive_more = { version = "1", features = ["full"] }
error-stack = "0.5"
fatfs = "0.3"
toml = "0.5.2"
toml = "0.8"
# hbasm.git = "https://git.ablecorp.us/AbleOS/holey-bytes.git"
hblang.git = "https://git.ablecorp.us/AbleOS/holey-bytes.git"
[dependencies.reqwest]
version = "0.11"
version = "0.12"
default-features = false
features = ["rustls-tls", "blocking"]

View file

@ -1,4 +1,5 @@
#![allow(unused)]
mod dev;
use {
@ -7,7 +8,7 @@ use {
error_stack::{bail, report, Context, Report, Result, ResultExt},
fatfs::{FileSystem, FormatVolumeOptions, FsOptions, ReadWriteSeek},
std::{
fmt::Display,
// fmt::Display,
fs::{self, File},
io::{self, Write},
path::Path,
@ -428,11 +429,11 @@ fn fetch_ovmf(target: Target) -> Result<String, OvmfFetchError> {
#[derive(Debug, Display)]
enum OvmfFetchError {
#[display(fmt = "Failed to fetch OVMF package")]
#[display("Failed to fetch OVMF package")]
Fetch,
#[display(fmt = "No OVMF package available")]
#[display("No OVMF package available")]
Empty,
#[display(fmt = "IO Error")]
#[display("IO Error")]
Io,
}
@ -447,19 +448,19 @@ enum Target {
#[derive(Debug, Display)]
enum Error {
#[display(fmt = "Failed to build the kernel")]
#[display("Failed to build the kernel")]
Build,
#[display(fmt = "Missing or invalid subcommand (available: build, run)")]
#[display("Missing or invalid subcommand (available: build, run)")]
InvalidSubCom,
#[display(fmt = "IO Error")]
#[display("IO Error")]
Io,
#[display(fmt = "Failed to spawn a process")]
#[display("Failed to spawn a process")]
ProcessSpawn,
#[display(fmt = "Failed to fetch UEFI firmware")]
#[display("Failed to fetch UEFI firmware")]
OvmfFetch,
#[display(fmt = "Failed to assemble Holey Bytes code")]
#[display("Failed to assemble Holey Bytes code")]
Assembler,
#[display(fmt = "QEMU Error: {}", "fmt_qemu_err(*_0)")]
#[display("QEMU Error: {}", "fmt_qemu_err(*_0)")]
Qemu(Option<i32>),
}

View file

@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-05-17"
channel = "nightly-2024-07-17"
components = ["rust-src", "llvm-tools"]