From d9bbdff08c909ddbc4e31b582824a901d8abafc8 Mon Sep 17 00:00:00 2001 From: zxq5 Date: Mon, 24 Feb 2025 03:26:49 +0000 Subject: [PATCH] - added a new libary libm containing procedural macros for the kernel. these should be used to include external files and resources in the kernel binary at compile time. - libm currently supports loading psf-1 formatted fonts - added two fonts that are included in the binary at compile time - refactored libk to make the crate structure more organised and maintainable in future. new structure: - drivers (hardware interaction) - resources (consts and statics included either manually or via macros) - std (standard functions for higher level interaction with the os, for example creating windows) - added geometry.rs - provides the Vec2 struct for use with dimensions, coordinates etc. - added window.rs - provides the Window struct for rendering the state of an application to the screen - added application.rs - provides the Application trait for custom programs to implement in order to run --- Cargo.lock | 99 ++++++++++++++++++ Cargo.toml | 2 +- docs/.obsidian/workspace.json | 2 +- kernel/src/arch/x86_64/interrupts.rs | 2 +- kernel/src/lib.rs | 4 +- kernel/src/main.rs | 13 +-- libk/Cargo.toml | 1 + libk/resources/font/cp850-8x16.psf | Bin 0 -> 4100 bytes libk/resources/font/spleen-8x16.psf | Bin 0 -> 10632 bytes .../{io/ascii.rs => drivers/io/ascii/mod.rs} | 18 ++-- .../{ => drivers}/io/framebuffer/colour.rs | 0 .../io/framebuffer/display.rs} | 4 +- libk/src/drivers/io/framebuffer/mod.rs | 2 + libk/src/{ => drivers}/io/keyboard.rs | 0 libk/src/{io.rs => drivers/io/mod.rs} | 0 libk/src/{ => drivers}/io/port.rs | 0 libk/src/{ => drivers}/io/serial.rs | 10 +- libk/src/{ => drivers}/kalloc/allocator.rs | 0 libk/src/drivers/kalloc/mod.rs | 1 + libk/src/drivers/mod.rs | 3 + libk/src/{ => drivers}/scheduling/mod.rs | 0 libk/src/{ => drivers}/scheduling/task.rs | 8 +- libk/src/kalloc.rs | 3 - libk/src/lib.rs | 20 +++- .../font/ibm_vga_8x16.rs} | 0 libk/src/resources/font/mod.rs | 12 +++ libk/src/resources/mod.rs | 1 + libk/src/std/application/application.rs | 14 +++ libk/src/std/application/mod.rs | 2 + libk/src/std/application/window.rs | 74 +++++++++++++ libk/src/std/maths/geometry.rs | 73 +++++++++++++ libk/src/std/maths/mod.rs | 1 + libk/src/std/mod.rs | 2 + libm/Cargo.toml | 15 +++ libm/src/lib.rs | 69 ++++++++++++ scripts/run_debug.sh | 5 +- 36 files changed, 421 insertions(+), 39 deletions(-) create mode 100644 libk/resources/font/cp850-8x16.psf create mode 100644 libk/resources/font/spleen-8x16.psf rename libk/src/{io/ascii.rs => drivers/io/ascii/mod.rs} (92%) rename libk/src/{ => drivers}/io/framebuffer/colour.rs (100%) rename libk/src/{io/framebuffer.rs => drivers/io/framebuffer/display.rs} (98%) create mode 100644 libk/src/drivers/io/framebuffer/mod.rs rename libk/src/{ => drivers}/io/keyboard.rs (100%) rename libk/src/{io.rs => drivers/io/mod.rs} (100%) rename libk/src/{ => drivers}/io/port.rs (100%) rename libk/src/{ => drivers}/io/serial.rs (95%) rename libk/src/{ => drivers}/kalloc/allocator.rs (100%) create mode 100644 libk/src/drivers/kalloc/mod.rs create mode 100644 libk/src/drivers/mod.rs rename libk/src/{ => drivers}/scheduling/mod.rs (100%) rename libk/src/{ => drivers}/scheduling/task.rs (95%) delete mode 100644 libk/src/kalloc.rs rename libk/src/{io/ascii/font.rs => resources/font/ibm_vga_8x16.rs} (100%) create mode 100644 libk/src/resources/font/mod.rs create mode 100644 libk/src/resources/mod.rs create mode 100644 libk/src/std/application/application.rs create mode 100644 libk/src/std/application/mod.rs create mode 100644 libk/src/std/application/window.rs create mode 100644 libk/src/std/maths/geometry.rs create mode 100644 libk/src/std/maths/mod.rs create mode 100644 libk/src/std/mod.rs create mode 100644 libm/Cargo.toml create mode 100644 libm/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 5d87614..a28837f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,47 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "foundry_os" version = "0.1.0" @@ -101,12 +142,19 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "libk" version = "0.1.0" dependencies = [ "crossbeam", "futures-util", + "libm", "limine", "linked_list_allocator", "pc-keyboard", @@ -114,6 +162,16 @@ dependencies = [ "x86_64", ] +[[package]] +name = "libm" +version = "0.1.0" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "limine" version = "0.3.1" @@ -169,6 +227,24 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rustversion" version = "1.0.19" @@ -205,6 +281,29 @@ dependencies = [ "lock_api", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" + [[package]] name = "volatile" version = "0.4.6" diff --git a/Cargo.toml b/Cargo.toml index 3abf5a6..e681630 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["kernel", "libk"] +members = ["kernel", "libk", "libm"] resolver = "2" [workspace.package] diff --git a/docs/.obsidian/workspace.json b/docs/.obsidian/workspace.json index 730b8e1..81213d4 100644 --- a/docs/.obsidian/workspace.json +++ b/docs/.obsidian/workspace.json @@ -189,7 +189,7 @@ "command-palette:Open command palette": false } }, - "active": "e1fb15cab546d0b6", + "active": "add883d295e04659", "lastOpenFiles": [ "Usage", "Welcome.md", diff --git a/kernel/src/arch/x86_64/interrupts.rs b/kernel/src/arch/x86_64/interrupts.rs index 49f417c..2be3781 100644 --- a/kernel/src/arch/x86_64/interrupts.rs +++ b/kernel/src/arch/x86_64/interrupts.rs @@ -96,7 +96,7 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac let mut port = Port::new(0x60); let scancode: u8 = unsafe { port.read() }; - libk::io::keyboard::add_scancode(scancode); + libk::drivers::io::keyboard::add_scancode(scancode); unsafe { PICS.lock() diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index dd8bf8e..29e9f0a 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -6,7 +6,7 @@ extern crate alloc; use core::arch::asm; use limine::BaseRevision; -use libk::kalloc::init_heap; +use libk::drivers::kalloc::allocator::init_heap; use libk::prelude::*; use x86_64::VirtAddr; @@ -51,7 +51,7 @@ pub fn boot() -> Result<(), &'static str> { let memory_map = memmap::get_memory_map(); print_log!(" Initialising Serial... "); - libk::io::serial::init()?; + libk::drivers::io::serial::init()?; println_log!("[Success]"); print_log!(" Setting Up Global Descriptor Table... "); diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 2aecb69..4e1168d 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -4,10 +4,13 @@ extern crate alloc; use libk::{ - io::{self, keyboard}, - prelude::*, - scheduling::task::{Executor, Task}, // scheduling::task::{Executor, Task}, + drivers::{ + io::{self, ascii::WRITER, keyboard}, + scheduling::task::{Executor, Task}, + }, + prelude::*, + resources::font::FONT_SPLEEN_8X16, }; #[unsafe(no_mangle)] @@ -20,7 +23,7 @@ extern "C" fn kmain() -> ! { println_log!("[ Kernel Initialised Successfully ] "); let dimensions = io::ascii::screensize_chars(); - let dimensions2 = io::ascii::screensize_px(); + let dimensions2 = io::framebuffer::display::screensize_px(); println!("Dimensions: {}x{} (px)", dimensions2.0, dimensions2.1); println!("Dimensions: {}x{} (chars)", dimensions.0, dimensions.1); @@ -49,6 +52,4 @@ extern "C" fn kmain() -> ! { let mut executor = Executor::new(); executor.spawn(Task::new(keyboard::print_keypresses())); executor.run(); - - loop {} } diff --git a/libk/Cargo.toml b/libk/Cargo.toml index a6d272e..9b9f99c 100644 --- a/libk/Cargo.toml +++ b/libk/Cargo.toml @@ -19,3 +19,4 @@ futures-util = { version = "0.3.31", default-features = false, features = [ "alloc", ] } linked_list_allocator = "0.10.5" +libm = { path = "../libm" } diff --git a/libk/resources/font/cp850-8x16.psf b/libk/resources/font/cp850-8x16.psf new file mode 100644 index 0000000000000000000000000000000000000000..7539f61e8c4060b43fc40feec098534b95c4d428 GIT binary patch literal 4100 zcma)9y=xpt6n_}A)gqK`h=tOmaFJqg!g&dc6iohwG^rLTOk4~>)#V_=+H#@)!7dGH zT)H;nFT9VLow>Nk@BFcE-kUe?duJyPVH^H~_W1AP;}5To zk6UK7>&x}}?0UTx8o&<2Ft{r2-L70ps=B_uzE-Lh)v9jS>vp68P((3zO}IY80}!-Y zn`h4qZ>Pnx*B?H-J`?`#*4IyFec$&p)=(Q`Os!fa8u!27*IMhNnYK&FE20u zdW0a_`KGeo+p6@A1qc{2ev0}eo=hWz&lnE`P~}hxm!O-W(fr8hx=yqc9^1h_78U?( zSCw<&cmNm%=c+M%8&Gv!qcLKSG3MJ`!rH;oUtCto`P0r7#ff*pgHvBB-S`vUR#{t3 z7k%GPiPl;-nZ8nCy0lg$FKbm2)aaq9*R@2OpvG)RD3xr0G#Y|n9l(PE?UQ^1W@|pj zjIp4qW|9G>oo8_`I-M_yx+sc}-g2k5{ZN+WF3S&H*WD-ls_D9W?|qOTe1QtJ7XKWw z5*LA1Tpm1loA?zLV2XpXz_PjYT5-*{f<|f&H<-PflX*`LJlk>3n z2r@r*sT#(9Um0U(Gi%I>_un*xc%J_>40BW}`uys4xH=zciZw#~CI3)`*eCq?l`FUK z1mdHQa-Q?PsmdbmZ`)rpdr5bqUuYW|4(a)PK76A70r1|ss&Y2ii_xSQi2s8>!Q`EW z`m4EJv<~z2Ww$)4cH+FMxqZr#_?S(O(-(l#HuhbDS?OZG!m?>F+Qy9I%=xbCLi~2h zvbH#nxxW$44+($rM9I}zs}xje{)+;(3WA}MTQ&_BfXK73 zO5ZOZhxvRyAN^i%g+>oK#iKvWcd-P5eW9fnQ!~~F`PYo&eOv2$bZ@c~^^?8iBr=5xEUd42Sd(T!FaUD+z55mG;N9_mZ=gU{;+^<{hmS$(M5WHidB_2b!H zoG^HFWve*gkS>)Bs5n0W;NxV{UR?Zvj{tkN?+482IgxBHLh6U?9ci|Ar1h-N;)(pM zK0J;9L`x&zhXEw|y>J7KaOYRIE8$o6w}wXGis$n~cYco5zgoGUNLcDCjDf93c$-m5 zU2uKidm-(2%y$|;7sw`)%$nv z-@SX4h`@mT&kCC5fw4+Ai<46A0R-Bdzu9LIy1LLMCY%TpdtoP$0=Tdfd%^TqKj*(+ z-sQi4{>*=&*EVn$=%$P1MgBXV(`Av<0PfYBw{PFR5q~F$+v1k~{{qPBVS|5L$ZSC1 zjSQ!6yzlVsINpy5(O$tfyd0+q-@`|&gJ6&k4w#*L!}R2u8&;-5Jtw_-%F3 zd+&P`qUoIQX&NUq}djP$!=MAAPPJ8G6>W-->NY^e_>A5}7M{>S&9f)+xjj8JsLr*W7- fs&WJ4ZiU4qrHXL)F_O&6+LY=^ZhFhuP|5fgSK0&Q literal 0 HcmV?d00001 diff --git a/libk/resources/font/spleen-8x16.psf b/libk/resources/font/spleen-8x16.psf new file mode 100644 index 0000000000000000000000000000000000000000..d030f4ea4379f1aa57b3e0691a94eebd524bc021 GIT binary patch literal 10632 zcmd5?d3aP+vadrfEno{_wb?u2o&-&yYM?#!G2U&Sxnx^=2fRh_Cj zRW}wG8JQ#?;0pu-zJPqDk_TRV;DHx6KJY+^#!&%OTm&>u6ig%%A+KT;km(Jj;x3=> z;>C+DmrwDN%U1%FByke2d@e;)KT0L+=Yok zw~dEZJ+*oB=BHML>^JQPBnGs%vCyi$+g{(cP1BRB{tM{+N&82bo3=6-Oaz1SmwcUB za_rc#kTCzMiU>y_=ybU}1p&We^9F-KZ}I79beY7HhAOD<@_58?c|5NA;!Uy3>2x?8 z4yV&qtd|6QF1W1l6yS1|Sf`f+m&=nx>{v~L=8%%*o;pB6k;u`?D;FXW?HQMM$vEi) zEh7|hIGiqzKcdf1;68`LVXY5`qtR$I;dDCHU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1XD z_;Bvv*YJA$T0W8kChy4ecn98|=kgrR`BXlQPv^ic0^TCvZ35mSAWXpf1RNlsoPfgw zd?<9`jd>&9kdNVa^0B;#15Nn#ycuuG$MNxe0x#x3H{P4~;yt;S_uvJ*JDW;d^EqC18X?2ngfrDs>p$r9QcKR&k0zU(bxdO89+4we-N;n z_GJJG0=8zD20#pWIU^?vU<_!J4b)=C3?PFYH-HY=z|?Hu?*z=u24-Xf?sh;{JK$FW z24w@U5zssv*h4^zY+!{Oc%6Xf`3?>k45-Zj2LrBQKph6uWk5X!T+09_1FmC0eFijO zKtl#JVnAaCG-JT^3~(`^IRjcSpd|xZF`%{3mH}-U;ATJ;1F{*=jsYeEI0JGRkjsGf z4CugsJO*@RKqm%tW`KtQT^P`f0o@r;z+8X7O9Oa5qpbn7Gk_ce$TfiWB5n*I&j30azzqg)qXBd>fX)Wc#Q^dRpsN9N zGk^jE=wSd}1L$c0y$qnY0r(8yCIjee0R0SLfB_6NfLje (u32, u32) { } pub struct Writer { + font: &'static Font, /// Measured in chars not pixels. screen_width: u32, /// Measured in chars not pixels. @@ -48,6 +47,7 @@ impl Writer { panic!("Framebuffer writer not initialized."); }, |writer| Self { + font: &FONT_CP850_8X16, screen_width: writer.width() / 8, screen_height: writer.height() / 16, text_line: 0, @@ -60,6 +60,10 @@ impl Writer { ) } + pub fn set_font(&mut self, font: &'static Font) { + self.font = font; + } + pub fn write_glyph(&mut self, c: u16) { if c as u8 == b'\n' { self.newline(); @@ -67,7 +71,7 @@ impl Writer { } // get the character data from the font array. -- each byte is a row of pixels - let data: &[u8] = &FONT[c as usize * 16..(c as usize + 1) * 16]; + let data: &[u8] = &self.font.0[c as usize]; if let Some(writer) = FRAMEBUFFER_WRITER.lock().as_mut() { for (row, line) in data.iter().enumerate().take(16) { @@ -190,7 +194,7 @@ macro_rules! println_log { #[macro_export] macro_rules! print_log { - ($($arg:tt)*) => ($crate::io::ascii::_print_log(format_args!($($arg)*))); + ($($arg:tt)*) => ($crate::_print_log(format_args!($($arg)*))); } #[macro_export] @@ -201,7 +205,7 @@ macro_rules! println { #[macro_export] macro_rules! print { - ($($arg:tt)*) => ($crate::io::ascii::_print(format_args!($($arg)*))); + ($($arg:tt)*) => ($crate::_print(format_args!($($arg)*))); } #[macro_export] diff --git a/libk/src/io/framebuffer/colour.rs b/libk/src/drivers/io/framebuffer/colour.rs similarity index 100% rename from libk/src/io/framebuffer/colour.rs rename to libk/src/drivers/io/framebuffer/colour.rs diff --git a/libk/src/io/framebuffer.rs b/libk/src/drivers/io/framebuffer/display.rs similarity index 98% rename from libk/src/io/framebuffer.rs rename to libk/src/drivers/io/framebuffer/display.rs index a9dd243..ca9450d 100644 --- a/libk/src/io/framebuffer.rs +++ b/libk/src/drivers/io/framebuffer/display.rs @@ -4,9 +4,7 @@ use limine::request::FramebufferRequest; #[unsafe(link_section = ".requests")] static FRAMEBUFFER_REQUEST: FramebufferRequest = FramebufferRequest::new(); -mod colour; - -pub use colour::Colour; +use super::colour::Colour; use core::panic; use limine::framebuffer::Framebuffer; diff --git a/libk/src/drivers/io/framebuffer/mod.rs b/libk/src/drivers/io/framebuffer/mod.rs new file mode 100644 index 0000000..cc5957a --- /dev/null +++ b/libk/src/drivers/io/framebuffer/mod.rs @@ -0,0 +1,2 @@ +pub mod colour; +pub mod display; diff --git a/libk/src/io/keyboard.rs b/libk/src/drivers/io/keyboard.rs similarity index 100% rename from libk/src/io/keyboard.rs rename to libk/src/drivers/io/keyboard.rs diff --git a/libk/src/io.rs b/libk/src/drivers/io/mod.rs similarity index 100% rename from libk/src/io.rs rename to libk/src/drivers/io/mod.rs diff --git a/libk/src/io/port.rs b/libk/src/drivers/io/port.rs similarity index 100% rename from libk/src/io/port.rs rename to libk/src/drivers/io/port.rs diff --git a/libk/src/io/serial.rs b/libk/src/drivers/io/serial.rs similarity index 95% rename from libk/src/io/serial.rs rename to libk/src/drivers/io/serial.rs index 03db09a..bb95d43 100644 --- a/libk/src/io/serial.rs +++ b/libk/src/drivers/io/serial.rs @@ -5,14 +5,14 @@ use core::{ use spin::{Lazy, Mutex}; #[macro_export] -macro_rules! serial_println { - () => ($crate::serial_print!("\n")); - ($($arg:tt)*) => ($crate::io::serial_print!("{}\n", format_args!($($arg)*))); +macro_rules! serial_print { + ($($arg:tt)*) => ($crate::_serial_write(format_args!($($arg)*))); } #[macro_export] -macro_rules! serial_print { - ($($arg:tt)*) => ($crate::io::serial::_serial_write(format_args!($($arg)*))); +macro_rules! serial_println { + () => ($crate::serial_print!("\n")); + ($($arg:tt)*) => (serial_print!("{}\n", format_args!($($arg)*))); } use super::port::{inb, outb}; diff --git a/libk/src/kalloc/allocator.rs b/libk/src/drivers/kalloc/allocator.rs similarity index 100% rename from libk/src/kalloc/allocator.rs rename to libk/src/drivers/kalloc/allocator.rs diff --git a/libk/src/drivers/kalloc/mod.rs b/libk/src/drivers/kalloc/mod.rs new file mode 100644 index 0000000..98fe5c3 --- /dev/null +++ b/libk/src/drivers/kalloc/mod.rs @@ -0,0 +1 @@ +pub mod allocator; diff --git a/libk/src/drivers/mod.rs b/libk/src/drivers/mod.rs new file mode 100644 index 0000000..61ec870 --- /dev/null +++ b/libk/src/drivers/mod.rs @@ -0,0 +1,3 @@ +pub mod io; +pub mod kalloc; +pub mod scheduling; diff --git a/libk/src/scheduling/mod.rs b/libk/src/drivers/scheduling/mod.rs similarity index 100% rename from libk/src/scheduling/mod.rs rename to libk/src/drivers/scheduling/mod.rs diff --git a/libk/src/scheduling/task.rs b/libk/src/drivers/scheduling/task.rs similarity index 95% rename from libk/src/scheduling/task.rs rename to libk/src/drivers/scheduling/task.rs index 5267d70..ef68925 100644 --- a/libk/src/scheduling/task.rs +++ b/libk/src/drivers/scheduling/task.rs @@ -1,10 +1,8 @@ -use alloc::boxed::Box; -use alloc::collections::{BTreeMap, VecDeque}; -use alloc::sync::Arc; -use alloc::task::Wake; +use alloc::{boxed::Box, collections::BTreeMap, sync::Arc, task::Wake}; + use core::sync::atomic::AtomicU64; +use core::task::Waker; use core::task::{Context, Poll}; -use core::task::{RawWaker, Waker}; use core::{future::Future, pin::Pin}; use crossbeam::queue::ArrayQueue; use x86_64::instructions::interrupts::{self, enable_and_hlt}; diff --git a/libk/src/kalloc.rs b/libk/src/kalloc.rs deleted file mode 100644 index 56c2a60..0000000 --- a/libk/src/kalloc.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod allocator; - -pub use self::allocator::init_heap; diff --git a/libk/src/lib.rs b/libk/src/lib.rs index 9acd10f..1176f0f 100644 --- a/libk/src/lib.rs +++ b/libk/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![allow(async_fn_in_trait)] #![warn(tail_expr_drop_order)] #![warn(clippy::correctness, clippy::perf, clippy::nursery)] // alloc @@ -8,11 +9,22 @@ extern crate alloc; -pub mod io; -pub mod kalloc; -pub mod scheduling; +pub mod drivers; +pub mod resources; +pub mod std; -/// Re-exports most of the IO macros. +/// Re-exports most of the IO macros as well as standard allocation stuff pub mod prelude { + pub use crate::drivers::io::ascii::{_print, _print_log}; pub use crate::{print, print_log, println, println_log, serial_print, serial_println}; + pub use alloc::{ + boxed::Box, + string::{String, ToString}, + vec::Vec, + }; } + +pub use crate::drivers::io::{ + ascii::{_print, _print_log}, + serial::_serial_write, +}; diff --git a/libk/src/io/ascii/font.rs b/libk/src/resources/font/ibm_vga_8x16.rs similarity index 100% rename from libk/src/io/ascii/font.rs rename to libk/src/resources/font/ibm_vga_8x16.rs diff --git a/libk/src/resources/font/mod.rs b/libk/src/resources/font/mod.rs new file mode 100644 index 0000000..b51acbe --- /dev/null +++ b/libk/src/resources/font/mod.rs @@ -0,0 +1,12 @@ +use libm::include_font; + +pub mod ibm_vga_8x16; + +pub static FONT_SPLEEN_8X16: Font = Font(include_font!( + "/home/zxq5/Projects/OSDev/FoundryOS/libk/resources/font/spleen-8x16.psf" +)); +pub static FONT_CP850_8X16: Font = Font(include_font!( + "/home/zxq5/Projects/OSDev/FoundryOS/libk/resources/font/cp850-8x16.psf" +)); + +pub struct Font(pub [[u8; 16]; 512]); diff --git a/libk/src/resources/mod.rs b/libk/src/resources/mod.rs new file mode 100644 index 0000000..8123d3b --- /dev/null +++ b/libk/src/resources/mod.rs @@ -0,0 +1 @@ +pub mod font; diff --git a/libk/src/std/application/application.rs b/libk/src/std/application/application.rs new file mode 100644 index 0000000..1a691bd --- /dev/null +++ b/libk/src/std/application/application.rs @@ -0,0 +1,14 @@ +use crate::prelude::*; + +pub trait Application { + type Output; + + async fn run(&mut self, args: Vec) -> Result; +} + +#[derive(Debug)] +pub enum Error { + UnknownCommand(String), + ApplicationFailed(String), + KernelError(String), +} diff --git a/libk/src/std/application/mod.rs b/libk/src/std/application/mod.rs new file mode 100644 index 0000000..1e8b41c --- /dev/null +++ b/libk/src/std/application/mod.rs @@ -0,0 +1,2 @@ +pub mod application; +pub mod window; diff --git a/libk/src/std/application/window.rs b/libk/src/std/application/window.rs new file mode 100644 index 0000000..b4c957c --- /dev/null +++ b/libk/src/std/application/window.rs @@ -0,0 +1,74 @@ +use crate::{prelude::*, std::maths::geometry::Vec2}; + +pub struct Window { + dimensions: Vec2, + position: Vec2, + bordered: bool, + opened: bool, + title: String, +} + +impl Window { + pub const fn new() -> Window { + Window { + dimensions: Vec2::new(0, 0), + position: Vec2::new(0, 0), + bordered: true, + opened: false, + title: String::new(), + } + } + + pub fn is_bordered(&self) -> bool { + self.bordered + } + + pub fn is_open(&self) -> bool { + self.opened + } + + pub fn open(&mut self) { + self.opened = true; + } + + pub fn close(&mut self) { + self.opened = false; + } + + // some basic getters and setters for utility. + pub fn title(&self) -> &str { + &self.title + } + + pub fn dimensions(&self) -> Vec2 { + self.dimensions + } + + pub fn position(&self) -> Vec2 { + self.position + } + + pub fn set_title(&mut self, title: String) { + self.title = title; + } + + pub fn move_window(&mut self, offset: Vec2) { + self.position += offset; + } + + pub fn set_position(&mut self, position: Vec2) { + self.position = position; + } + + pub fn set_dimensions(&mut self, dimensions: Vec2) { + self.dimensions = dimensions; + } +} + +impl Drop for Window { + fn drop(&mut self) { + if self.opened { + self.close(); + } + } +} diff --git a/libk/src/std/maths/geometry.rs b/libk/src/std/maths/geometry.rs new file mode 100644 index 0000000..0ad9f76 --- /dev/null +++ b/libk/src/std/maths/geometry.rs @@ -0,0 +1,73 @@ +use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; + +pub trait Coordinate: + Copy + Clone + PartialEq + AddAssign + MulAssign + SubAssign + DivAssign +{ +} + +impl Coordinate for usize {} +impl Coordinate for isize {} +impl Coordinate for u8 {} +impl Coordinate for i8 {} +impl Coordinate for u16 {} +impl Coordinate for i16 {} +impl Coordinate for u32 {} +impl Coordinate for i32 {} +impl Coordinate for u64 {} +impl Coordinate for i64 {} +impl Coordinate for u128 {} +impl Coordinate for i128 {} +impl Coordinate for f32 {} +impl Coordinate for f64 {} + +#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)] +pub struct Vec2 { + x: T, + y: T, +} + +impl Vec2 { + pub const fn new(x: T, y: T) -> Self { + Self { x, y } + } + + pub fn into>(&self) -> Vec2 { + Vec2::new(self.x.clone().into(), self.y.clone().into()) + } + + pub fn x(&self) -> T { + self.x + } + + pub fn y(&self) -> T { + self.y + } +} + +impl AddAssign for Vec2 { + fn add_assign(&mut self, rhs: Self) { + self.x += rhs.x; + self.y += rhs.y; + } +} + +impl SubAssign for Vec2 { + fn sub_assign(&mut self, rhs: Self) { + self.x -= rhs.x; + self.y -= rhs.y; + } +} + +impl MulAssign for Vec2 { + fn mul_assign(&mut self, rhs: T) { + self.x *= rhs; + self.y *= rhs; + } +} + +impl DivAssign for Vec2 { + fn div_assign(&mut self, rhs: T) { + self.x /= rhs; + self.y /= rhs; + } +} diff --git a/libk/src/std/maths/mod.rs b/libk/src/std/maths/mod.rs new file mode 100644 index 0000000..af899a6 --- /dev/null +++ b/libk/src/std/maths/mod.rs @@ -0,0 +1 @@ +pub mod geometry; diff --git a/libk/src/std/mod.rs b/libk/src/std/mod.rs new file mode 100644 index 0000000..aeba020 --- /dev/null +++ b/libk/src/std/mod.rs @@ -0,0 +1,2 @@ +pub mod application; +pub mod maths; diff --git a/libm/Cargo.toml b/libm/Cargo.toml new file mode 100644 index 0000000..6a36e94 --- /dev/null +++ b/libm/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "libm" +version.workspace = true +edition.workspace = true +authors.workspace = true + +[dependencies] +darling = "0.20.10" +proc-macro2 = "1.0.93" +quote = "1.0.38" +syn = "2.0.98" + + +[lib] +proc-macro = true diff --git a/libm/src/lib.rs b/libm/src/lib.rs new file mode 100644 index 0000000..ab3cca4 --- /dev/null +++ b/libm/src/lib.rs @@ -0,0 +1,69 @@ +use std::fs::File; +use std::io::{Read, Seek, SeekFrom}; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{LitStr, parse_macro_input}; + +extern crate proc_macro; + +#[proc_macro] +pub fn include_font(item: TokenStream) -> TokenStream { + let filename = parse_macro_input!(item as LitStr); + let file_path = filename.value().to_string(); + + println!("Loading font: [{}]", file_path); + + let font_data = match Font::new(load_file(file_path)) { + Ok(font) => font.0, + Err(e) => panic!("{}", e), + }; + + quote!( + [ + #( + [#(#font_data),*] + ),* + ] + ) + .into() +} + +struct Font([[u8; 16]; 512]); + +impl Font { + const MAGIC: u16 = 0x3604; + + pub fn new(data: [u8; (32 + 2) * 512 + 4]) -> Result { + let magic: u16 = (data[0] as u16) << 8 | data[1] as u16; + let mode = data[2]; + let size = data[3]; + + if magic != Self::MAGIC { + return Err("Magic value is invalid!"); + } + + let has_512_glyphs = (mode & 0x01) != 0; + let mut glyphs = [[0; 16]; 512]; + let glyph_count = if has_512_glyphs { 512 } else { 256 }; + + for i in 0..(glyph_count as usize) { + let mut buff: [u8; 16] = [0; 16]; + for j in 0..(size as usize) { + buff[j] = data[4 + i * (size as usize) + j]; + } + glyphs[i] = buff; + } + + Ok(Font(glyphs)) + } +} + +fn load_file(filename: String) -> [u8; (32 + 2) * 512 + 4] { + let mut buf = [0; (32 + 2) * 512 + 4]; + let mut f = File::open(filename).unwrap(); + f.seek(SeekFrom::Start(0)).unwrap(); + f.read(&mut buf).unwrap(); + + return buf; +} diff --git a/scripts/run_debug.sh b/scripts/run_debug.sh index 138298c..b538ce7 100755 --- a/scripts/run_debug.sh +++ b/scripts/run_debug.sh @@ -176,10 +176,13 @@ kvm_flag="" trap 'check_test_res "tests completed"' ERR + # $build_dir/image.iso + + cd "$project_root" qemu-system-x86_64 -M q35 \ - ${kvm_flag} \ -cdrom "$build_dir/image.iso" \ + ${kvm_flag} \ -boot d \ -m 2G \ ${serial_flags} \