Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
🆙
Browse files Browse the repository at this point in the history
  • Loading branch information
otofune committed Aug 15, 2020
1 parent 1303d77 commit 5dab9d0
Showing 1 changed file with 76 additions and 8 deletions.
84 changes: 76 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
#![feature(asm)]

unsafe fn cpuid_vendor_id() -> (String, u32, u32, u32) {
struct BrandPart {
text: String,
eax_in: u32,
eax: u32,
ebx: u32,
ecx: u32,
edx: u32,
}

unsafe fn cpuid_brand() -> Vec<BrandPart> {
let mut brands: Vec<BrandPart> = vec![];
for eax_in in 0x80000002 as u32..0x80000005 as u32 {
let eax: u32;
let ebx: u32;
let ecx: u32;
let edx: u32;
asm!(
"cpuid",
inout("eax") eax_in => eax,
out("ebx") ebx,
out("ecx") ecx,
out("edx") edx,
);
let mut brand_part: Vec<u8> = vec![];
brand_part.extend_from_slice(&eax.to_le_bytes());
brand_part.extend_from_slice(&ebx.to_le_bytes());
brand_part.extend_from_slice(&ecx.to_le_bytes());
brand_part.extend_from_slice(&edx.to_le_bytes());
let brand_part = String::from_utf8(brand_part);
match brand_part {
Ok(r) => brands.push(BrandPart {
text: r,
eax_in,
eax,
ebx,
ecx,
edx,
}),
Err(r) => {
println!("{}", r);
panic!("not ok...");
}
}
}
brands
}

struct Vendor {
text: String,
ebx: u32,
edx: u32,
ecx: u32,
}

unsafe fn cpuid_vendor_id() -> Vendor {
let ebx: u32;
let edx: u32;
let ecx: u32;
Expand All @@ -16,29 +70,43 @@ unsafe fn cpuid_vendor_id() -> (String, u32, u32, u32) {
vendor.extend_from_slice(&edx.to_le_bytes());
vendor.extend_from_slice(&ecx.to_le_bytes());
if let Ok(r) = String::from_utf8(vendor) {
return (r, ebx, edx, ecx);
return Vendor {
text: r,
ebx,
edx,
ecx,
};
}
panic!("not ok...")
}

unsafe fn cpuid_signature() -> u32 {
let signature: u32;
let extra: u32;
asm!(
"cpuid",
// EAX 1 selects the procesor information
inout("eax") 1 as i32 => signature,
lateout("edx") _, // flags
lateout("ecx") _,
lateout("ebx") _, // extra
out("edx") _, // flags
out("ecx") _,
out("ebx") extra, // extra
);
println!("extra(ebx) = {:#b}", extra);
signature
}

fn main() {
unsafe {
let (vendor, vendor_ebx, vendor_edx, vendor_ecx) = cpuid_vendor_id();
println!("vendor = {}, ebx = {:0x}, edx = {:0x}, ecx = {:0x}", vendor, vendor_ebx, vendor_edx, vendor_ecx);
let vendor = cpuid_vendor_id();
println!(
"vendor = {} (ebx = {:#b}, edx = {:#b}, ecx = {:#b})",
vendor.text, vendor.ebx, vendor.edx, vendor.ecx
);
let signature = cpuid_signature();
println!("processor signature = {:0x}", signature);
println!("processor signature = eax = {:#b}", signature);
let brand = cpuid_brand();
for brand_part in &brand {
println!("brand_part(eax_in = {:#x}, eax = {:#b}, ebx: {:#b}, ecx: {:#b}, edx: {:#b}) = '{}'", brand_part.eax_in, brand_part.eax, brand_part.ebx, brand_part.ecx, brand_part.edx, brand_part.text);
}
}
}

0 comments on commit 5dab9d0

Please sign in to comment.