Compare commits

..

2 Commits

Author SHA1 Message Date
Quinten Kock 627150b170 implement CPUID 2021-01-30 22:10:11 +01:00
Quinten Kock 062917d75b x86_64: replace comptime loop by GAS wrapper 2021-01-30 22:08:38 +01:00
5 changed files with 72 additions and 15 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

@ -5,20 +5,21 @@ const c_idt = @cImport(@cInclude("arch/x86_64/idt.h"));
export const int_msg = "INTERRUPTED!\n"; export const int_msg = "INTERRUPTED!\n";
comptime { comptime {
@setEvalBranchQuota(2560000); asm(
comptime var i = 0; \\.macro isr_wrapperX number
var buf: [512]u8 = undefined; \\ isr_wrapper\number:
inline while (i < 256) : (i += 1) { \\ mov $\number, %rdi
const code = std.fmt.bufPrint(buf[0..512], \\ call interrupt_handler
\\.global isr_wrapper{} \\ iretq
\\.type isr_wrapper{}, @function \\.endm
\\isr_wrapper{}: \\
\\ mov ${}, %rdi \\.altmacro
\\ call interrupt_handler \\.set i,0
\\ iretq \\.rept 256
, .{i,i,i,i}) catch unreachable; \\ isr_wrapperX %i
asm (code); \\ .set i,i+1
} \\.endr
);
} }
export fn interrupt_handler(int_type: u64) void { export fn interrupt_handler(int_type: u64) void {

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 paging = @import("paging.zig");
pub const interrupt = @import("interrupt.zig"); pub const interrupt = @import("interrupt.zig");