aboutsummaryrefslogtreecommitdiff
path: root/mirai.old/invocations/os/cpuinfo.zig
blob: 900902b5d52aae0ad96d9a2076108806b62e5a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! CPUINFO invocation - Get CPU model string

const cpuid = @import("../../asm/cpuid.zig");
const handler = @import("../handler.zig");
const result = @import("../../utils/types/result.zig");

pub fn invoke(ctx: *handler.InvocationContext) void {
    const buffer_ptr: [*]u8 = @ptrFromInt(ctx.rdi);
    const buffer_len: usize = @intCast(ctx.rsi);

    if (buffer_len < 48) {
        result.set_error(ctx);
        return;
    }

    if (!cpuid.has_brand_string()) {
        const generic = "Unknown CPU";
        for (generic, 0..) |c, i| {
            buffer_ptr[i] = c;
        }
        result.set_value(ctx, generic.len);
        return;
    }

    var brand: [48]u8 = undefined;
    cpuid.get_brand_string(&brand);

    // Trim leading spaces
    var start: usize = 0;
    while (start < 48 and brand[start] == ' ') : (start += 1) {}

    // Trim trailing spaces and nulls
    var end: usize = 48;
    while (end > start and (brand[end - 1] == ' ' or brand[end - 1] == 0)) : (end -= 1) {}

    const trimmed_len = end - start;
    for (0..trimmed_len) |i| {
        buffer_ptr[i] = brand[start + i];
    }

    result.set_value(ctx, trimmed_len);
}