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";
comptime {
@setEvalBranchQuota(2560000);
comptime var i = 0;
var buf: [512]u8 = undefined;
inline while (i < 256) : (i += 1) {
const code = std.fmt.bufPrint(buf[0..512],
\\.global isr_wrapper{}
\\.type isr_wrapper{}, @function
\\isr_wrapper{}:
\\ mov ${}, %rdi
\\ call interrupt_handler
\\ iretq
, .{i,i,i,i}) catch unreachable;
asm (code);
}
asm(
\\.macro isr_wrapperX number
\\ isr_wrapper\number:
\\ mov $\number, %rdi
\\ call interrupt_handler
\\ iretq
\\.endm
\\
\\.altmacro
\\.set i,0
\\.rept 256
\\ isr_wrapperX %i
\\ .set i,i+1
\\.endr
);
}
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 interrupt = @import("interrupt.zig");