1
0
Fork 0
forked from AbleOS/ableos
ableOS_v1Change/kernel/src/arch/x86_64/mod.rs

195 lines
5.5 KiB
Rust
Raw Normal View History

2023-05-06 06:50:24 -05:00
use embedded_graphics::pixelcolor::Rgb888;
2023-07-15 07:47:46 -05:00
use crate::{arch::x86_64::graphics::DISPLAY, bootmodules::BootModule};
2023-05-06 06:50:24 -05:00
2023-03-30 16:43:04 -05:00
pub mod memory;
2023-05-06 06:50:24 -05:00
mod cpuid;
2023-03-30 16:43:04 -05:00
mod gdt;
2023-05-15 02:19:34 -05:00
pub mod graphics;
2023-04-07 16:44:33 -05:00
pub(crate) mod interrupts;
2023-05-15 02:19:34 -05:00
pub mod logging;
2023-05-06 06:50:24 -05:00
pub mod pci;
pub mod virtio;
2023-07-15 07:47:46 -05:00
mod device_info_collector;
2023-03-30 16:43:04 -05:00
2023-05-06 06:50:24 -05:00
pub use {logging::log, memory::PAGE_SIZE};
2023-03-30 16:43:04 -05:00
2023-05-06 06:50:24 -05:00
use {
crate::allocator,
limine::{HhdmRequest, KernelFileRequest, MemmapRequest, ModuleRequest},
x86_64::VirtAddr,
};
2023-03-30 16:43:04 -05:00
extern "C" {
fn _initial_kernel_heap_start();
fn _initial_kernel_heap_size();
}
const INITIAL_KERNEL_HEAP_START: *mut u8 = _initial_kernel_heap_start as _;
const INITIAL_KERNEL_HEAP_SIZE: *const () = _initial_kernel_heap_size as _;
#[no_mangle]
unsafe extern "C" fn _kernel_start() -> ! {
logging::init();
crate::logger::init().expect("failed to set logger");
log::info!("Initialising AKern {}", crate::VERSION);
static HDHM_REQ: HhdmRequest = HhdmRequest::new(0);
memory::init_pt(VirtAddr::new(
HDHM_REQ
.get_response()
.get()
.expect("tried to get physical memory mapping offset from Limine")
.offset,
));
allocator::init(INITIAL_KERNEL_HEAP_START, INITIAL_KERNEL_HEAP_SIZE as _);
static MMAP_REQ: MemmapRequest = MemmapRequest::new(0);
memory::initialize(
MMAP_REQ
.get_response()
.get()
.expect("tried to get memory map from Limine")
.memmap(),
);
gdt::init();
interrupts::init();
static KFILE_REQ: KernelFileRequest = KernelFileRequest::new(0);
static MOD_REQ: ModuleRequest = ModuleRequest::new(0);
2023-04-05 12:29:20 -05:00
2023-07-15 07:47:46 -05:00
device_info_collector::collect_device_info();
2023-05-06 06:50:24 -05:00
// Graphics test
{
2023-05-06 07:05:45 -05:00
graphics::init();
2023-05-06 06:50:24 -05:00
let mut dis = DISPLAY.lock();
2023-05-23 05:16:14 -05:00
use embedded_graphics::prelude::RgbColor;
2023-05-15 02:19:34 -05:00
2023-05-23 05:16:14 -05:00
let _ = dis.set_color(Rgb888::YELLOW);
let thick = 6;
2023-05-15 02:19:34 -05:00
let p1 = (400, 30);
let p2 = (200, 150);
let p3 = (600, 150);
let p4 = (200, 350);
let p5 = (600, 350);
let p6 = (400, 470);
{
//HEXAGON
2023-05-23 05:16:14 -05:00
let _ = dis.line(p1.0, p1.1, p2.0, p2.1, thick);
let _ = dis.line(p1.0, p1.1, p3.0, p3.1, thick);
let _ = dis.line(p2.0, p2.1, p4.0, p4.1, thick);
let _ = dis.line(p3.0, p3.1, p5.0, p5.1, thick);
let _ = dis.line(p6.0, p6.1, p4.0, p4.1, thick);
let _ = dis.line(p6.0, p6.1, p5.0, p5.1, thick);
2023-05-15 02:19:34 -05:00
}
{
2023-05-23 05:16:14 -05:00
let _ = dis.line(600, 150, 200, 350, thick);
let _ = dis.line(600, 350, 400, 250, thick);
2023-05-15 02:19:34 -05:00
}
{
2023-05-23 05:16:14 -05:00
let _ = dis.set_color(Rgb888::WHITE);
2023-05-15 02:19:34 -05:00
let hp1 = (350, 150);
let hp2 = (350, 350);
let hp3 = (450, 250);
let hp4 = (350, 250);
let hp5 = (450, 150);
let hp6 = (450, 350);
2023-05-23 05:16:14 -05:00
let _ = dis.line(hp1.0, hp1.1, hp2.0, hp2.1, thick);
let _ = dis.line(hp3.0, hp3.1, hp4.0, hp4.1, thick);
let _ = dis.line(hp5.0, hp5.1, hp6.0, hp6.1, thick);
2023-05-15 02:19:34 -05:00
}
dis.swap_buffers();
2023-05-23 05:16:14 -05:00
};
2023-05-06 06:50:24 -05:00
2023-05-28 02:00:54 -05:00
// TODO: Add in rdseed and rdrand as sources for randomness
2023-05-23 05:16:14 -05:00
let _rand = xml::XMLElement::new("Random");
2023-05-15 02:19:34 -05:00
2023-07-08 23:22:44 -05:00
let bm = MOD_REQ.get_response().get();
let mut bootmodules = alloc::vec::Vec::new();
if bm.is_some() {
let bm = bm.unwrap();
for x in 0..bm.module_count {
let file = bm.modules().get(x as usize);
if file.is_some() {
let file = file.unwrap();
let raw_bytes = core::slice::from_raw_parts(
file.base.as_ptr().expect("invalid initrd"),
file.length as usize,
)
.to_vec();
let file_path = alloc::string::String::from_utf8(
file.path.to_str().unwrap().to_bytes().to_vec(),
);
if file_path.is_err() {
panic!("invalid file path: {:?}", file_path);
}
let file_cmd = alloc::string::String::from_utf8(
file.cmdline.to_str().unwrap().to_bytes().to_vec(),
);
if file_cmd.is_err() {
panic!("invalid module cmd: {:?}", file_cmd);
}
log::trace!("module path: {:?}", file_path);
log::trace!("module cmd: {:?}", file_cmd);
bootmodules.push(BootModule::new(
file_path.unwrap(),
raw_bytes,
file_cmd.unwrap(),
));
} else {
log::error!("You should not be here");
break;
}
}
log::info!("Boot module count: {:?}", bootmodules.len());
assert_eq!(bm.module_count, bootmodules.len() as u64);
}
2023-03-30 16:43:04 -05:00
crate::kmain::kmain(
KFILE_REQ
.get_response()
.get()
.and_then(|r| r.kernel_file.get())
.expect("failed to get kernel file from Limine")
.cmdline
.to_str()
.map(core::ffi::CStr::to_str)
.transpose()
.expect("expected valid cmdline string")
.unwrap_or_default(),
2023-07-08 23:22:44 -05:00
bootmodules,
2023-03-30 16:43:04 -05:00
)
}
2023-05-23 05:16:14 -05:00
2023-03-30 16:43:04 -05:00
/// Spin loop
2023-07-15 07:47:46 -05:00
pub fn spin_loop() -> ! {
2023-03-30 16:43:04 -05:00
loop {
x86_64::instructions::hlt();
}
}
2023-04-07 16:44:33 -05:00
pub fn hardware_random_u64() -> u64 {
2023-05-06 06:50:24 -05:00
use {log::trace, rdrand::RdRand};
2023-04-07 16:44:33 -05:00
let gen = RdRand::new().unwrap();
let ret = gen.try_next_u64().unwrap();
trace!("Random {}", ret);
ret
}
2023-05-08 03:53:15 -05:00
pub fn get_edid() {}