From 29bcce58b382d7ee52b14f012cee5444df30f712 Mon Sep 17 00:00:00 2001 From: able Date: Wed, 20 Sep 2023 12:01:12 -0500 Subject: [PATCH] CPUID support --- .../src/arch/aarch64/device_info_collector.rs | 49 +++++++++++++++++++ kernel/src/arch/aarch64/mod.rs | 16 +++--- 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 kernel/src/arch/aarch64/device_info_collector.rs diff --git a/kernel/src/arch/aarch64/device_info_collector.rs b/kernel/src/arch/aarch64/device_info_collector.rs new file mode 100644 index 00000000..dd200d3e --- /dev/null +++ b/kernel/src/arch/aarch64/device_info_collector.rs @@ -0,0 +1,49 @@ +use { + crate::{alloc::string::ToString, device_tree::DeviceTree, kmain::DEVICE_TREE}, + alloc::string::String, + core::arch::asm, + xml::XMLElement, +}; + +pub fn collect_device_info() { + log::trace!("Collecting devices on aarch64"); + // Lock device tree + unsafe { + DEVICE_TREE.force_unlock(); + } + let device_tree = &mut DEVICE_TREE.lock(); + collect_cpu_info(device_tree); + // let dt = DEVICE_TREE.lock(); +} + +fn collect_cpu_info(device_tree: &mut DeviceTree) { + let mut cpu = XMLElement::new("cpu"); + + let cpu_id = cpu_id(); + + cpu.set_attribute("CPU Name", cpu_id.0); + cpu.set_attribute("CPU Id", cpu_id.1); + + let cpus = device_tree.devices.get_mut("CPUs").unwrap(); + cpus.push(cpu); +} + +fn cpu_id() -> (String, u64) { + let mut cpu_id: u64 = 0; + unsafe { + asm!("mrs {cpu_id}, MIDR_EL1", + cpu_id = out(reg) cpu_id, + ) + } + + let cpu_name = match cpu_id { + // the source of these two was a stackoverflow question + // https://raspberrypi.stackexchange.com/questions/117175/how-do-i-read-the-cpuid-in-aarch64-asm + 0x410FD034 => "Cortex-A53".to_string(), + 0x410FD083 => "Cortex-A72".to_string(), + _ => "Unknown".to_string(), + }; + log::trace!("CPU Name: {cpu_name} - CPU ID: 0x{:X}", cpu_id); + + (cpu_name, cpu_id) +} diff --git a/kernel/src/arch/aarch64/mod.rs b/kernel/src/arch/aarch64/mod.rs index a6e359c5..793288ab 100644 --- a/kernel/src/arch/aarch64/mod.rs +++ b/kernel/src/arch/aarch64/mod.rs @@ -4,6 +4,9 @@ use { core::arch::asm, limine::FramebufferRequest, }; +mod device_info_collector; +use device_info_collector::collect_device_info; + pub mod logging; use limine::HhdmRequest; extern "C" { @@ -14,22 +17,13 @@ const INITIAL_KERNEL_HEAP_START: *mut u8 = _initial_kernel_heap_start as _; const INITIAL_KERNEL_HEAP_SIZE: *const () = _initial_kernel_heap_size as _; pub const PAGE_SIZE: usize = 4096; +static FB_REQ: FramebufferRequest = FramebufferRequest::new(0); #[no_mangle] unsafe extern "C" fn _kernel_start() -> ! { crate::logger::init().expect("failed to set logger"); log::info!("Initialising AKern {}", crate::VERSION); - static FB_REQ: FramebufferRequest = FramebufferRequest::new(0); - let fb1 = &FB_REQ.get_response().get().unwrap().framebuffers()[0]; - - for i in 0..100_usize { - let offset = i * fb1.pitch as usize + i * 4; - unsafe { - *(fb1.address.as_ptr().unwrap().offset(offset as isize) as *mut u32) = 0xFFFFFFFF; - } - } - static HDHM_REQ: HhdmRequest = HhdmRequest::new(0); // memory::init_pt(VirtAddr::new( // HDHM_REQ @@ -40,6 +34,8 @@ unsafe extern "C" fn _kernel_start() -> ! { // )); allocator::init(INITIAL_KERNEL_HEAP_START, INITIAL_KERNEL_HEAP_SIZE as _); + collect_device_info(); + let bm = MOD_REQ.get_response().get(); use limine::{KernelFileRequest, ModuleRequest}; static KFILE_REQ: KernelFileRequest = KernelFileRequest::new(0);