Kernel: Add CPU functions in a portable way

This commit is contained in:
apio 2022-10-31 16:18:05 +01:00
parent ed29870172
commit 70d78d86be
6 changed files with 52 additions and 1 deletions

2
moon/src/arch/cpu.rs Normal file
View File

@ -0,0 +1,2 @@
#![cfg(target_arch = "x86_64")]
pub use super::x86_64::cpu::*;

4
moon/src/arch/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod cpu;
#[cfg(target_arch = "x86_64")]
mod x86_64;

View File

@ -0,0 +1,27 @@
#![cfg(target_arch = "x86_64")]
#![allow(dead_code)]
use core::arch::x86_64::{__cpuid as do_cpuid, CpuidResult};
use core::arch::asm;
fn safe_cpuid(leaf: u32) -> CpuidResult {
return unsafe { do_cpuid(leaf) };
}
pub fn get_processor_id() -> u32 {
let cpuid_result = safe_cpuid(1);
return cpuid_result.ebx >> 24;
}
pub fn halt() -> ! {
loop {
unsafe {
asm!("cli");
asm!("hlt");
}
}
}
pub fn wait_for_interrupt() -> () {
unsafe { asm!("hlt"); }
}

View File

@ -0,0 +1 @@
pub mod cpu;

View File

@ -1,6 +1,15 @@
use crate::bootboot::{BOOTBOOT, BOOTBOOT_MAGIC};
use crate::util::get_bootboot;
use crate::arch::cpu;
pub fn halt_other_cores() -> () {
let boot: BOOTBOOT = get_bootboot();
if cpu::get_processor_id() != boot.bspid as u32 {
cpu::halt();
}
}
pub fn check_magic() -> () {
let boot: BOOTBOOT = get_bootboot();
assert_eq!(boot.magic, BOOTBOOT_MAGIC[..4]);

View File

@ -6,12 +6,20 @@ mod bootboot;
mod panic;
mod util;
mod init;
mod arch;
use arch::cpu;
use video::Color;
#[no_mangle]
pub extern "C" fn _start() -> ! {
init::halt_other_cores();
init::check_magic();
video::clear(Color::White);
loop {}
cpu::halt();
}