Kernel: Add CPU functions in a portable way
This commit is contained in:
parent
ed29870172
commit
70d78d86be
2
moon/src/arch/cpu.rs
Normal file
2
moon/src/arch/cpu.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#![cfg(target_arch = "x86_64")]
|
||||||
|
pub use super::x86_64::cpu::*;
|
4
moon/src/arch/mod.rs
Normal file
4
moon/src/arch/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
pub mod cpu;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "x86_64")]
|
||||||
|
mod x86_64;
|
27
moon/src/arch/x86_64/cpu.rs
Normal file
27
moon/src/arch/x86_64/cpu.rs
Normal 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"); }
|
||||||
|
}
|
1
moon/src/arch/x86_64/mod.rs
Normal file
1
moon/src/arch/x86_64/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod cpu;
|
@ -1,6 +1,15 @@
|
|||||||
use crate::bootboot::{BOOTBOOT, BOOTBOOT_MAGIC};
|
use crate::bootboot::{BOOTBOOT, BOOTBOOT_MAGIC};
|
||||||
use crate::util::get_bootboot;
|
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() -> () {
|
pub fn check_magic() -> () {
|
||||||
let boot: BOOTBOOT = get_bootboot();
|
let boot: BOOTBOOT = get_bootboot();
|
||||||
assert_eq!(boot.magic, BOOTBOOT_MAGIC[..4]);
|
assert_eq!(boot.magic, BOOTBOOT_MAGIC[..4]);
|
||||||
|
@ -6,12 +6,20 @@ mod bootboot;
|
|||||||
mod panic;
|
mod panic;
|
||||||
mod util;
|
mod util;
|
||||||
mod init;
|
mod init;
|
||||||
|
mod arch;
|
||||||
|
|
||||||
|
use arch::cpu;
|
||||||
|
|
||||||
use video::Color;
|
use video::Color;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
|
|
||||||
|
init::halt_other_cores();
|
||||||
init::check_magic();
|
init::check_magic();
|
||||||
|
|
||||||
video::clear(Color::White);
|
video::clear(Color::White);
|
||||||
loop {}
|
|
||||||
|
cpu::halt();
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user