From e8712d7c39adf56a3d14cd3eb1905618fe40ce0a Mon Sep 17 00:00:00 2001
From: Able <abl3theabove@gmail.com>
Date: Thu, 24 Nov 2022 07:20:16 -0600
Subject: [PATCH] framebuffer + smp work

---
 ableos/src/boot_conf.rs | 22 ++++++++--
 ableos/src/kmain.rs     | 91 ++++++++++++++++++++++++-----------------
 base/boot/limine.cfg    |  4 +-
 3 files changed, 75 insertions(+), 42 deletions(-)

diff --git a/ableos/src/boot_conf.rs b/ableos/src/boot_conf.rs
index d2ec535..19a5217 100644
--- a/ableos/src/boot_conf.rs
+++ b/ableos/src/boot_conf.rs
@@ -24,9 +24,17 @@ pub struct KernelConfig {
 
 impl KernelConfig {
     pub fn new() -> Self {
-        toml::from_str(include_str!("../assets/kernel.toml")).unwrap()
+        KernelConfig::default()
+    }
+    pub fn load_from_string(toml_string: &str) -> Self {
+        match toml::from_str(toml_string) {
+            Ok(kernel_conf) => kernel_conf,
+            Err(err) => {
+                error!("Error {}", err);
+                KernelConfig::new()
+            }
+        }
     }
-
     pub fn log_level(&self) -> LevelFilter {
         use LevelFilter::*;
         match self.logging.level {
@@ -42,7 +50,15 @@ impl KernelConfig {
 
 impl Default for KernelConfig {
     fn default() -> Self {
-        Self::new()
+        Self {
+            logging: LoggingConfig {
+                enabled: true,
+                log_to_serial: true,
+                log_to_vterm: false,
+                level: LogLevel::Trace,
+                filter: Vec::new(),
+            },
+        }
     }
 }
 
diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs
index d88f59f..eb5b686 100644
--- a/ableos/src/kmain.rs
+++ b/ableos/src/kmain.rs
@@ -16,6 +16,9 @@ use crate::{boot_conf::KernelConfig, scratchpad, systeminfo::RELEASE_TYPE};
 // use crate::{boot_conf::KernelConfig, scratchpad, systeminfo::RELEASE_TYPE, TERM};
 use crate::{filesystem, hardware};
 use kernel::KERNEL_VERSION;
+use limine::LimineSmpInfo;
+use limine::{LimineFramebufferRequest, LimineSmpRequest};
+
 use spin::Lazy;
 
 // FIXME: platform agnostic paging stuff
@@ -23,6 +26,8 @@ use x86_64::structures::paging::{Mapper, Size4KiB};
 
 // TODO: Change this structure to allow for multiple cores loaded
 pub static KERNEL_CONF: Lazy<KernelConfig> = Lazy::new(KernelConfig::new);
+pub static FRAMEBUFFER: LimineFramebufferRequest = LimineFramebufferRequest::new(0);
+pub static SMP: LimineSmpRequest = LimineSmpRequest::new(0);
 
 /// The main entry point of the kernel
 pub fn kernel_main(
@@ -31,14 +36,20 @@ pub fn kernel_main(
 ) -> ! {
     init::init();
 
-    // /*
+    {
+        // TODO: Setup config loaded from disk
+        let mut kptr = KERNEL_CONF.as_mut_ptr();
+        unsafe {
+            *kptr = KernelConfig::new();
+        }
+    }
     if KERNEL_CONF.logging.enabled {
         log::set_max_level(KERNEL_CONF.log_level());
         // println!("{:#?}", *KERNEL_CONF);
     } else {
         log::set_max_level(log::LevelFilter::Off);
     }
-    // */
+
     // let mut term = TERM.lock();
     // term.initialize();
     // term.set_dirty(true);
@@ -83,46 +94,50 @@ pub fn kernel_main(
     /*
     log_version_data();
     // */
+
+    {
+        // TODO: setup a proper framebuffer handler
+        let fb_response = FRAMEBUFFER.get_response().get().unwrap();
+        for fb in fb_response.framebuffers().unwrap() {
+            trace!("Framebuffer {}x{}", fb.width, fb.height);
+            trace!("{}", fb.memory_model);
+            trace!("{}", fb.bpp);
+            let mut count = 0;
+            let total = fb.width * fb.height * 3;
+            while count != total {
+                unsafe {
+                    let fb_ptr = fb.address.as_mut_ptr().unwrap();
+                    *fb_ptr.offset((count).try_into().unwrap()) = 0xff;
+                    *fb_ptr.offset((count + 1).try_into().unwrap()) = 0xff;
+                    *fb_ptr.offset((count + 2).try_into().unwrap()) = 0xff;
+                    *fb_ptr.offset((count + 3).try_into().unwrap()) = 0x00;
+
+                    *fb_ptr.offset((count + 4).try_into().unwrap()) = 0x00;
+                    *fb_ptr.offset((count + 5).try_into().unwrap()) = 0x00;
+                    // *fb_ptr.offset((count + 6).try_into().unwrap()) = 0x00;
+                }
+                count += 6;
+            }
+        }
+    }
+
+    // SMP
+    {
+        let smp = SMP.get_response().get().unwrap();
+        for cpu in smp.cpus().unwrap() {
+            // unsafe {
+            //     cpu.goto_address = *(trace_hcf as *const u64);
+            // }
+        }
+    }
+
     scratchpad();
     sloop()
 }
 
-pub fn traceloop() {
-    // TODO: Having an empty function double faults
-
-    // let mut last_time = 0.0;
-    /*
-    loop {
-        // FIXME: the following double faults
-        /*
-        let time = fetch_time();
-        if time > last_time {
-            last_time = time;
-            trace!("Timer");
-        }
-        */
-    }
-
-    */
-
-    /* TODO: This also double faults
-    let fs = &*crate::filesystem::FILE_SYSTEM.lock();
-    let path = format!("/home/able/bins/aos_test.wasm");
-
-    let home_exec_file = fs.open(&path.as_bytes(), OpenOptions::new().read(true));
-
-    drop(fs);
-    let mut binary_prog: Vec<u8> = vec![];
-
-    match home_exec_file {
-        Ok(file) => {
-            let ret = file.read_to_end(&mut binary_prog).unwrap();
-        }
-        _ => {}
-    }
-
-    wasm_jumploader::run_program(&binary_prog);
-    */
+extern "C" fn trace_hcf(info: *const LimineSmpInfo) -> ! {
+    trace!("CPU BOOT");
+    loop {}
 }
 
 pub fn cpu_socket_startup() {
diff --git a/base/boot/limine.cfg b/base/boot/limine.cfg
index 2229358..5c521a4 100644
--- a/base/boot/limine.cfg
+++ b/base/boot/limine.cfg
@@ -5,7 +5,7 @@ ${ABLEOS_KERNEL}=boot:///boot/kernel
 DEFAULT_ENTRY=1
 TIMEOUT=3
 VERBOSE=yes
-
+INTERFACE_RESOLUTION=800x600
 # Terminal related settings
 # TERM_WALLPAPER=${WALLPAPER_PATH}
 TERM_BACKDROP=008080
@@ -15,3 +15,5 @@ TERM_BACKDROP=008080
     PROTOCOL=limine
     KERNEL_PATH=${ABLEOS_KERNEL}
     KERNEL_CMDLINE=
+    # Setting a default resolution for the framebuffer
+    RESOLUTION=800x600x24