implement CPUID

This commit is contained in:
Quinten Kock 2021-01-30 22:10:11 +01:00
parent 062917d75b
commit 627150b170
4 changed files with 57 additions and 1 deletions

View File

@ -0,0 +1 @@
usingnamespace @import("../x86/x86.zig");

View File

@ -0,0 +1,54 @@
const std = @import("std");
const cpuid_c = @cImport(@cInclude("cpuid.h"));
const cpuid_ret = struct {
eax: u32,
ebx: u32,
ecx: u32,
edx: u32,
};
// static inline int cpuid_string(int code, uint32_t where[4]) {
// asm volatile("cpuid":"=a"(*where),"=b"(*(where+1)),
// "=c"(*(where+2)),"=d"(*(where+3)):"a"(code));
// return (int)where[0];
// }
fn getcpuid(leaf: u32) [4]u32 {
// cpuid_c.__cpuid(leaf, &ret.eax, &ret.ebx, &ret.ecx, &ret.edx);
var eax: u32 = undefined;
var ebx: u32 = undefined;
var ecx: u32 = undefined;
var edx: u32 = undefined;
asm volatile("cpuid"
: [eax] "={eax}" (eax),
[ebx] "={ebx}" (ebx),
[ecx] "={ecx}" (ecx),
[edx] "={edx}" (edx),
: [leaf] "{eax}" (leaf)
:
);
std.log.info("cpuid {x}: {x} {x} {x} {x}", .{leaf, eax, ebx, ecx, edx});
return .{eax, ebx, ecx, edx};
}
var vendor: [12]u8 = undefined;
pub fn getVendor() [12]u8 {
const v = getcpuid(0);
@memcpy(@ptrCast([*]u8, &vendor[0]), @ptrCast([*]const u8, &v[1]), 4);
@memcpy(@ptrCast([*]u8, &vendor[4]), @ptrCast([*]const u8, &v[3]), 4);
@memcpy(@ptrCast([*]u8, &vendor[8]), @ptrCast([*]const u8, &v[2]), 4);
std.log.notice("CPU vendor: {}", .{std.mem.span(&vendor)});
return vendor;
}
var name: [48]u8 = undefined;
pub fn getName() []u8 {
var i: u32 = 0x80000002;
while(i <= 0x80000004) : (i+=1) {
const v = getcpuid(i);
@memcpy(@ptrCast([*]u8, &name[(i-0x80000002)*16]), @ptrCast([*]const u8, &v), 16);
}
std.log.notice("CPU model name: {}", .{std.mem.span(&name)});
return std.mem.span(&name);
}

View File

@ -0,0 +1 @@
pub const cpuid = @import("cpuid.zig");

View File

@ -1,4 +1,4 @@
const x86 = @import("src/arch/x86/x86.zig");
usingnamespace @import("../x86/x86.zig");
pub const paging = @import("paging.zig");
pub const interrupt = @import("interrupt.zig");