Skip to content

Commit ae242fe

Browse files
committed
cpu: Added a CPU module identifying CPU features and branding
Signed-off-by: SlyMarbo <[email protected]>
1 parent d70ee51 commit ae242fe

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

kernel/src/cpu/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2022 The Firefly Authors.
2+
//
3+
// Use of this source code is governed by a BSD 3-clause
4+
// license that can be found in the LICENSE file.
5+
6+
//! Analyses the CPU for supported features and branding.
7+
8+
use crate::println;
9+
use lazy_static::lazy_static;
10+
use raw_cpuid::CpuId;
11+
12+
lazy_static! {
13+
static ref CPU_ID: CpuId = CpuId::new();
14+
}
15+
16+
/// Checks that the CPU supports all the features we need.
17+
///
18+
/// # Panics
19+
///
20+
/// `init` will panic if the CPU does not support any features
21+
/// Firefly requires.
22+
///
23+
pub fn init() {
24+
match CPU_ID.get_feature_info() {
25+
None => panic!("unable to determine CPU features"),
26+
Some(features) => {
27+
if !features.has_sysenter_sysexit() {
28+
panic!("CPU does not support the sysexit instruction");
29+
}
30+
}
31+
}
32+
}
33+
34+
/// Prints the CPU's branding information.
35+
///
36+
pub fn print_branding() {
37+
if let Some(branding) = CPU_ID.get_processor_brand_string() {
38+
println!("Kernel running on {} CPU.", branding.as_str());
39+
} else if let Some(version) = CPU_ID.get_vendor_info() {
40+
println!("Kernel running on {} CPU.", version.as_str());
41+
} else {
42+
println!("Kernel running on unknown CPU.");
43+
}
44+
}

kernel/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ use crate::memory::KERNEL_STACK_0;
4242
use crate::multitasking::{cpu_local, thread};
4343
use bootloader::BootInfo;
4444
use core::panic::PanicInfo;
45-
use lazy_static::lazy_static;
46-
use raw_cpuid::CpuId;
4745
use x86_64::instructions::port::Port;
4846

47+
pub mod cpu;
4948
pub mod drivers;
5049
pub mod gdt;
5150
pub mod interrupts;
@@ -56,15 +55,11 @@ pub mod random;
5655
pub mod time;
5756
pub mod utils;
5857

59-
lazy_static! {
60-
#[doc(hidden)]
61-
pub static ref CPU_ID: CpuId = CpuId::new();
62-
}
63-
6458
/// Initialise the kernel and its core components.
6559
///
6660
/// `init` currently performs the following steps:
6761
///
62+
/// - Check that the CPU supports the features we need.
6863
/// - Initialise the [Global Descriptor Table](gdt).
6964
/// - Initialise the [Programmable Interrupt Controller](interrupts).
7065
/// - Initialise the [Real-time clock and Programmable Interval Timer](time)
@@ -74,6 +69,7 @@ lazy_static! {
7469
/// - Initialise the [scheduler](multitasking/thread).
7570
///
7671
pub fn init(boot_info: &'static BootInfo) {
72+
cpu::init();
7773
gdt::init();
7874
interrupts::init();
7975
time::init();

kernel/src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,11 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
5555
#[cfg(not(test))]
5656
fn kmain() {
5757
use kernel::multitasking::thread::{scheduler, Thread};
58-
use kernel::{network, time, CPU_ID};
58+
use kernel::{cpu, network, time};
5959

6060
println!("Kernel ready!");
6161
println!("Kernel booted at {}.", time::boot_time());
62-
if let Some(branding) = CPU_ID.get_processor_brand_string() {
63-
println!("Kernel running on {} CPU.", branding.as_str());
64-
} else if let Some(version) = CPU_ID.get_vendor_info() {
65-
println!("Kernel running on {} CPU.", version.as_str());
66-
} else {
67-
println!("Kernel running on unknown CPU.");
68-
}
62+
cpu::print_branding();
6963

7064
// Set up our initial workload for when
7165
// we get a DHCP configuration.

0 commit comments

Comments
 (0)