From 26a553083c6165af508b47c2713904df6d69f963 Mon Sep 17 00:00:00 2001
From: Able <abl3theabove@gmail.com>
Date: Tue, 23 Nov 2021 05:53:06 -0600
Subject: [PATCH] breaking changes

---
 .github/workflows/rust.yml                  |  8 ++---
 ableos/Cargo.lock                           | 34 +++++++++++++++++++++
 ableos/Cargo.toml                           |  4 +++
 ableos/keymaps/qwerty.keymap                |  2 +-
 ableos/src/allocator/dummy.rs               | 14 +++++++++
 ableos/src/allocator/mod.rs                 | 24 +++++++++++++++
 ableos/src/arch/aarch64/mod.rs              | 26 ++++++++--------
 ableos/src/arch/riscv/mod.rs                |  3 +-
 ableos/src/arch/x86_64/drivers/allocator.rs | 15 +++++++++
 ableos/src/arch/x86_64/drivers/mod.rs       |  2 +-
 ableos/src/experiments/systeminfo.rs        |  2 +-
 ableos/src/keyboard/small_types.rs          | 27 ++++++++++++++++
 ableos/src/kmain.rs                         |  8 ++++-
 ableos/src/lib.rs                           |  6 +++-
 ableos/src/print.rs                         |  3 +-
 repbuild/src/main.rs                        |  1 -
 16 files changed, 153 insertions(+), 26 deletions(-)
 create mode 100644 ableos/src/allocator/dummy.rs
 create mode 100644 ableos/src/allocator/mod.rs
 create mode 100644 ableos/src/arch/x86_64/drivers/allocator.rs

diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 8941734..e7afbac 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -6,11 +6,11 @@ on:
 
 env:
   CARGO_TERM_COLOR: always
-  
+
 jobs:
   build:
     runs-on: ubuntu-latest
-    
+
     steps:
     - uses: actions/checkout@v2
     - uses: actions-rs/toolchain@v1
@@ -18,5 +18,5 @@ jobs:
         components: rust-src
     - uses: actions-rs/cargo@v1
       with:
-        command: build
-        args: --release
+        command: repbuild
+        args: run
diff --git a/ableos/Cargo.lock b/ableos/Cargo.lock
index 571238b..9d9bb0f 100644
--- a/ableos/Cargo.lock
+++ b/ableos/Cargo.lock
@@ -9,6 +9,7 @@ dependencies = [
  "bootloader",
  "cpuio",
  "lazy_static",
+ "linked_list_allocator",
  "pic8259",
  "psp",
  "spin",
@@ -60,6 +61,24 @@ dependencies = [
  "spin",
 ]
 
+[[package]]
+name = "linked_list_allocator"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "549ce1740e46b291953c4340adcd74c59bcf4308f4cac050fd33ba91b7168f4a"
+dependencies = [
+ "spinning_top",
+]
+
+[[package]]
+name = "lock_api"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109"
+dependencies = [
+ "scopeguard",
+]
+
 [[package]]
 name = "num_enum"
 version = "0.5.4"
@@ -145,12 +164,27 @@ dependencies = [
  "proc-macro2",
 ]
 
+[[package]]
+name = "scopeguard"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
+
 [[package]]
 name = "spin"
 version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
 
+[[package]]
+name = "spinning_top"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75adad84ee84b521fb2cca2d4fd0f1dab1d8d026bda3c5bea4ca63b5f9f9293c"
+dependencies = [
+ "lock_api",
+]
+
 [[package]]
 name = "syn"
 version = "1.0.81"
diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml
index 08c1b40..d6b12fc 100644
--- a/ableos/Cargo.toml
+++ b/ableos/Cargo.toml
@@ -15,11 +15,15 @@ run-args=["-serial", "stdio"]
 
 [dependencies]
 spin = "0.5.2"
+linked_list_allocator = "0.9.0"
 
 [dependencies.lazy_static]
 features = ["spin_no_std"]
 version = "1.0"
 
+
+
+
 # [dependencies.rhai]
 # version = "*"
 # features = ["no_std"]
diff --git a/ableos/keymaps/qwerty.keymap b/ableos/keymaps/qwerty.keymap
index 541b6a7..95db614 100644
--- a/ableos/keymaps/qwerty.keymap
+++ b/ableos/keymaps/qwerty.keymap
@@ -88,7 +88,7 @@
 86-v
 87-w
 88-x
-89-
+89-y
 90-z
 91-
 92-
diff --git a/ableos/src/allocator/dummy.rs b/ableos/src/allocator/dummy.rs
new file mode 100644
index 0000000..7b5f9d5
--- /dev/null
+++ b/ableos/src/allocator/dummy.rs
@@ -0,0 +1,14 @@
+use alloc::alloc::{GlobalAlloc, Layout};
+use core::ptr::null_mut;
+
+pub struct Dummy;
+
+unsafe impl GlobalAlloc for Dummy {
+    unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
+        null_mut()
+    }
+
+    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
+        panic!("dealloc should be never called")
+    }
+}
diff --git a/ableos/src/allocator/mod.rs b/ableos/src/allocator/mod.rs
new file mode 100644
index 0000000..8b2d79a
--- /dev/null
+++ b/ableos/src/allocator/mod.rs
@@ -0,0 +1,24 @@
+use alloc::alloc::{GlobalAlloc, Layout};
+// use core::alloc::{GlobalAlloc, Layout};
+use core::ptr::null_mut;
+
+// mod dummy;
+// use dummy::Dummy;
+
+pub const HEAP_START: usize = 0x_4444_4444_0000;
+/// 131072 bytes
+pub const HEAP_MULTIPLIER: usize = 1024;
+pub const HEAP_BASE: usize = 100;
+
+pub const HEAP_SIZE: usize = HEAP_BASE * HEAP_MULTIPLIER;
+// X86 alloc should be in arch/drivers/x86/alloc.rs
+
+use crate::arch::drivers::allocator::Dummy;
+
+#[global_allocator]
+static ALLOCATOR: Dummy = Dummy;
+
+#[alloc_error_handler]
+fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
+    panic!("allocation error: {:?}", layout)
+}
diff --git a/ableos/src/arch/aarch64/mod.rs b/ableos/src/arch/aarch64/mod.rs
index 46d9ae5..bbcf6f1 100644
--- a/ableos/src/arch/aarch64/mod.rs
+++ b/ableos/src/arch/aarch64/mod.rs
@@ -32,19 +32,19 @@ pub extern "C" fn not_main() {
             ptr::write_volatile(UART0, *byte);
         }
     }
-
-    let gpios = Pins::take();
-    let mut led = gpios.p0_31;
-
-    loop {
-        led.set_high();
-        delay(2_000_000);
-
-        led.set_low();
-        delay(6_000_000);
-    }
-
-    led.set_push_pull_output(Level::Low);
+    //
+    // let gpios = Pins::take();
+    // let mut led = gpios.p0_31;
+    //
+    // loop {
+    //     led.set_high();
+    //     delay(2_000_000);
+    //
+    //     led.set_low();
+    //     delay(6_000_000);
+    // }
+    //
+    // led.set_push_pull_output(Level::Low);
     crate::kmain::kernel_main();
 
     sloop();
diff --git a/ableos/src/arch/riscv/mod.rs b/ableos/src/arch/riscv/mod.rs
index 5ae0349..7d850fd 100644
--- a/ableos/src/arch/riscv/mod.rs
+++ b/ableos/src/arch/riscv/mod.rs
@@ -29,10 +29,11 @@ unsafe extern "C" fn _boot() -> ! {
     ",
     sym _start, options(noreturn));
 }
+
 use crate::serial::SERIAL;
+
 extern "C" fn _start() -> ! {
     SERIAL.lock().out(format_args!("Hi"));
-    // Serial123::out();
     sloop()
 }
 
diff --git a/ableos/src/arch/x86_64/drivers/allocator.rs b/ableos/src/arch/x86_64/drivers/allocator.rs
new file mode 100644
index 0000000..d12915c
--- /dev/null
+++ b/ableos/src/arch/x86_64/drivers/allocator.rs
@@ -0,0 +1,15 @@
+use alloc::alloc::{GlobalAlloc, Layout};
+// use core::alloc::{GlobalAlloc, Layout};
+use core::ptr::null_mut;
+
+pub struct Dummy;
+
+unsafe impl GlobalAlloc for Dummy {
+    unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
+        null_mut()
+    }
+
+    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
+        panic!("dealloc should be never called")
+    }
+}
diff --git a/ableos/src/arch/x86_64/drivers/mod.rs b/ableos/src/arch/x86_64/drivers/mod.rs
index d5087e9..9267fd2 100644
--- a/ableos/src/arch/x86_64/drivers/mod.rs
+++ b/ableos/src/arch/x86_64/drivers/mod.rs
@@ -1,5 +1,5 @@
+pub mod allocator;
 pub mod graphics;
 pub mod serial;
-
 #[deprecated(note = "The use of hardware specific drivers for VGA is discouraged")]
 pub mod vga;
diff --git a/ableos/src/experiments/systeminfo.rs b/ableos/src/experiments/systeminfo.rs
index 611f5be..cb7de27 100644
--- a/ableos/src/experiments/systeminfo.rs
+++ b/ableos/src/experiments/systeminfo.rs
@@ -30,7 +30,7 @@ Memory: {}
     );
     return x;
 }
-*/
+// */
 pub const KERNEL_VERSION: &str = env!("CARGO_PKG_VERSION");
 #[cfg(debug_assertions)]
 /// A constant to check if the kernel is in debug mode
diff --git a/ableos/src/keyboard/small_types.rs b/ableos/src/keyboard/small_types.rs
index 75ea706..4d9d5e9 100644
--- a/ableos/src/keyboard/small_types.rs
+++ b/ableos/src/keyboard/small_types.rs
@@ -130,24 +130,32 @@ macro_rules! keycode_enum {
 keycode_enum! {
     AltLeft = 0x00,
     AltRight = 0x01,
+
     ArrowDown = 0x02,
     ArrowLeft = 0x03,
     ArrowRight = 0x04,
     ArrowUp = 0x05,
+
     BackSlash = 0x06,
     Backspace = 0x07,
     BackTick = 0x08,
+
     BracketSquareLeft = 0x09,
     BracketSquareRight = 0x0A,
+
     CapsLock = 0x0B,
+
     Comma = 0x0C,
+
     ControlLeft = 0x0D,
     ControlRight = 0x0E,
+
     Delete = 0x0F,
     End = 0x10,
     Enter = 0x11,
     Escape = 0x12,
     Equals = 0x13,
+
     F1 = 0x14,
     F2 = 0x15,
     F3 = 0x16,
@@ -160,9 +168,13 @@ keycode_enum! {
     F10 = 0x1D,
     F11 = 0x1E,
     F12 = 0x1F,
+
     Fullstop = 0x20,
+
     Home = 0x21,
+
     Insert = 0x22,
+
     Key1 = 0x23,
     Key2 = 0x24,
     Key3 = 0x25,
@@ -173,8 +185,11 @@ keycode_enum! {
     Key8 = 0x2A,
     Key9 = 0x2B,
     Key0 = 0x2C,
+
     Menus = 0x2D,
+
     Minus = 0x2E,
+
     Numpad0 = 0x2F,
     Numpad1 = 0x30,
     Numpad2 = 0x31,
@@ -185,6 +200,7 @@ keycode_enum! {
     Numpad7 = 0x36,
     Numpad8 = 0x37,
     Numpad9 = 0x38,
+
     NumpadEnter = 0x39,
     NumpadLock = 0x3A,
     NumpadSlash = 0x3B,
@@ -192,20 +208,25 @@ keycode_enum! {
     NumpadMinus = 0x3D,
     NumpadPeriod = 0x3E,
     NumpadPlus = 0x3F,
+
     PageDown = 0x40,
     PageUp = 0x41,
+
     PauseBreak = 0x42,
     PrintScreen = 0x43,
     ScrollLock = 0x44,
     SemiColon = 0x45,
+
     ShiftLeft = 0x46,
     ShiftRight = 0x47,
+
     Slash = 0x48,
     Spacebar = 0x49,
     Tab = 0x4A,
     Quote = 0x4B,
     WindowsLeft = 0x4C,
     WindowsRight = 0x4D,
+
     A = 0x4E,
     B = 0x4F,
     C = 0x50,
@@ -232,15 +253,21 @@ keycode_enum! {
     X = 0x65,
     Y = 0x66,
     Z = 0x67,
+
     HashTilde = 0x68,
+
     PrevTrack = 0x69,
     NextTrack = 0x6A,
+
     Mute = 0x6B,
+
     Calculator = 0x6C,
+
     Play = 0x6D,
     Stop = 0x6E,
     VolumeDown = 0x6F,
     VolumeUp = 0x70,
+
     WWWHome = 0x71,
     PowerOnTestOk = 0x72,
 }
diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs
index 9967d9d..fb3fb2a 100644
--- a/ableos/src/kmain.rs
+++ b/ableos/src/kmain.rs
@@ -29,11 +29,17 @@ pub extern "C" fn kernel_main() {
     GraphicsBuffer::show_cursor();
     seed_rng();
 
+    {
+        use alloc::{vec, vec::Vec};
+        let x: Vec<u8> = vec![1];
+        println!("{:?}", x);
+    }
+
     /* If AES is present then AES init rng as well
     // Maybe via a cfg
         AES::init_rng();
 
-        */
+    */
 
     println!("{} v{}", RELEASE_TYPE, KERNEL_VERSION);
 
diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs
index 3d95faf..fcd7c4f 100644
--- a/ableos/src/lib.rs
+++ b/ableos/src/lib.rs
@@ -4,6 +4,7 @@
 #![feature(
     abi_x86_interrupt,
     asm,
+    alloc_error_handler,
     core_intrinsics,
     global_asm,
     lang_items,
@@ -27,7 +28,6 @@ pub mod arch;
 pub mod print;
 
 use arch::drivers::serial;
-
 pub mod driver_traits;
 pub mod experiments;
 pub mod keyboard;
@@ -36,3 +36,7 @@ pub mod panic;
 pub mod relib;
 
 use experiments::server;
+
+extern crate alloc;
+
+pub mod allocator;
diff --git a/ableos/src/print.rs b/ableos/src/print.rs
index 9d3a4e1..26545b2 100644
--- a/ableos/src/print.rs
+++ b/ableos/src/print.rs
@@ -1,6 +1,5 @@
 pub struct Stdout;
-use core::fmt::Arguments;
-use core::fmt::Error;
+use core::fmt::{Arguments, Error};
 impl Stdout {
     pub fn write_fmt(&mut self, arg: Arguments<'_>) /*-> Result<(), Error> */
     {
diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs
index a16ed57..98af974 100644
--- a/repbuild/src/main.rs
+++ b/repbuild/src/main.rs
@@ -21,7 +21,6 @@ enum Command {
 #[derive(clap::ArgEnum, Debug, Clone)]
 enum MachineType {
     X86,
-    /// hi
     RISCV,
     ARM,
 }