std.zig.system.windows.assert
别名
别名目标 std.debug.assert
std.zig.system.windows.builtin
常量
std.zig.system.windows.detectNativeCpuAndFeatures
函数
pub fn detectNativeCpuAndFeatures() ?Target.Cpu
std.zig.system.windows.detectRuntimeVersion
函数
返回从报告的运行时信息推导出的最高已知 WindowsVersion。 丢弃那些未作区分的中间版本信息。
pub fn detectRuntimeVersion() WindowsVersion
std.zig.system.windows.genericCpuAndNativeFeatures
函数
如果通过 Windows 注册表精细检测 CPU 特性失败,
则回退到通用 CPU 型号,但使用 SharedUserData
内容覆盖特性集。
这实际上就是 LLVM 对 Windows 上所有 ARM 芯片的做法。
fn genericCpuAndNativeFeatures(arch: Target.Cpu.Arch) Target.Cpu
参数
std.zig.system.windows.getCpuInfoFromRegistry
函数
fn getCpuInfoFromRegistry(core: usize, args: anytype) !void
参数
core: usize
std.zig.system.windows.IsProcessorFeaturePresent
别名
std.zig.system.windows.max_value_len
常量
std.zig.system.windows.mem
别名
别名目标 std.mem
std.zig.system.windows.PF
别名
别名目标 std.os.windows.PF
std.zig.system.windows.REG
别名
别名目标 std.os.windows.REG
std.zig.system.windows.setFeature
函数
fn setFeature(comptime Feature: type, cpu: *Target.Cpu, feature: Feature, enabled: bool) void
参数
std.zig.system.windows.std
别名
别名目标 std
std.zig.system.windows.Target
别名
别名目标 std.Target
std.zig.system.windows.WindowsVersion
别名
源码
std/zig/system/windows.zig
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const mem = std.mem;
const Target = std.Target;
const WindowsVersion = std.Target.Os.WindowsVersion;
const PF = std.os.windows.PF;
const REG = std.os.windows.REG;
const IsProcessorFeaturePresent = std.os.windows.IsProcessorFeaturePresent;
/// 返回从报告的运行时信息推导出的最高已知 WindowsVersion。
/// 丢弃那些未作区分的中间版本信息。
pub fn detectRuntimeVersion() WindowsVersion {
var version_info: std.os.windows.RTL_OSVERSIONINFOW = undefined;
version_info.dwOSVersionInfoSize = @sizeOf(@TypeOf(version_info));
switch (std.os.windows.ntdll.RtlGetVersion(&version_info)) {
.SUCCESS => {},
else => unreachable,
}
// 根据系统信息构造类似 NTDDI 的版本
// 常量,格式如下:
// B0 B1 B2 B3
// `---` `` ``--> 子版本(从 Windows 10 开始)
// \ `--> 服务包(所定义常量中始终为零)
// `--> 操作系统版本(主版本与次版本)
const os_ver: u16 = @as(u16, @intCast(version_info.dwMajorVersion & 0xff)) << 8 |
@as(u16, @intCast(version_info.dwMinorVersion & 0xff));
const sp_ver: u8 = 0;
const sub_ver: u8 = if (os_ver >= 0x0A00) subver: {
// 除将构建号与一组已知
// 值比较之外,没有其他方式
// 获取此信息
var last_idx: usize = 0;
for (WindowsVersion.known_win10_build_numbers, 0..) |build, i| {
if (version_info.dwBuildNumber >= build)
last_idx = i;
}
break :subver @as(u8, @truncate(last_idx));
} else 0;
const version: u32 = @as(u32, os_ver) << 16 | @as(u16, sp_ver) << 8 | sub_ver;
return @as(WindowsVersion, @fromBackingInt(@intCast(version)));
}
// 技术上,注册表值最长可以为 1MB。不过,微软建议将超过 2048 字节的值存储到文件,而非直接存储于注册表。由于这里只访问系统配置单元 \Registry\Machine,因此遵循微软建议。
// https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-element-size-limits
//
//
const max_value_len = 2048;
fn getCpuInfoFromRegistry(core: usize, args: anytype) !void {
const ArgsType = @TypeOf(args);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .@"struct") {
@compileError("expected tuple or struct argument, found " ++ @typeName(ArgsType));
}
const fields_info = args_type_info.@"struct";
// 最初希望通过单次调用和更复杂的表结构,
// 依次访问注册表中的各个 CPU#d 子键,并将所需值取入
// 缓冲区。但 NT 似乎要求每个表只对应一个缓冲区,因此
// 最终只会取得最后一个 CPU 核心的信息,覆盖其他所有信息。
// 如果有人能提出解决方案,请务必实现!
const table_size = 1 + fields_info.field_names.len;
var table: [table_size + 1]std.os.windows.RTL_QUERY_REGISTRY_TABLE = undefined;
const topkey = std.unicode.utf8ToUtf16LeStringLiteral("\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor");
const max_cpu_buf = 4;
var next_cpu_buf: [max_cpu_buf]u8 = undefined;
const next_cpu = try std.mem.print(&next_cpu_buf, "{d}", .{core});
var subkey: [max_cpu_buf + 1]u16 = undefined;
const subkey_len = try std.unicode.utf8ToUtf16Le(&subkey, next_cpu);
subkey[subkey_len] = 0;
table[0] = .{
.QueryRoutine = null,
.Flags = std.os.windows.RTL_QUERY_REGISTRY_SUBKEY | std.os.windows.RTL_QUERY_REGISTRY_REQUIRED,
.Name = subkey[0..subkey_len :0],
.EntryContext = null,
.DefaultType = .NONE,
.DefaultData = null,
.DefaultLength = 0,
};
var tmp_bufs: [fields_info.field_names.len][max_value_len]u8 align(@alignOf(std.os.windows.UNICODE_STRING)) = undefined;
inline for (fields_info.field_names, 0..) |field_name, i| {
const ctx: *anyopaque = blk: {
switch (@field(args, field_name).value_type) {
.SZ,
.EXPAND_SZ,
.MULTI_SZ,
=> {
comptime assert(@sizeOf(std.os.windows.UNICODE_STRING) % 2 == 0);
const unicode: *std.os.windows.UNICODE_STRING = @ptrCast(&tmp_bufs[i]);
unicode.* = .{
.Length = 0,
.MaximumLength = max_value_len - @sizeOf(std.os.windows.UNICODE_STRING),
.Buffer = @ptrCast(tmp_bufs[i][@sizeOf(std.os.windows.UNICODE_STRING)..]),
};
break :blk unicode;
},
.DWORD,
.DWORD_BIG_ENDIAN,
.QWORD,
=> break :blk &tmp_bufs[i],
else => unreachable,
}
};
var key_buf: [max_value_len / 2 + 1]u16 = undefined;
const key_len = try std.unicode.utf8ToUtf16Le(&key_buf, @field(args, field_name).key);
key_buf[key_len] = 0;
table[i + 1] = .{
.QueryRoutine = null,
.Flags = std.os.windows.RTL_QUERY_REGISTRY_DIRECT | std.os.windows.RTL_QUERY_REGISTRY_REQUIRED,
.Name = key_buf[0..key_len :0],
.EntryContext = ctx,
.DefaultType = .NONE,
.DefaultData = null,
.DefaultLength = 0,
};
}
// 表哨兵
table[table_size] = .{
.QueryRoutine = null,
.Flags = 0,
.Name = null,
.EntryContext = null,
.DefaultType = .NONE,
.DefaultData = null,
.DefaultLength = 0,
};
const res = std.os.windows.ntdll.RtlQueryRegistryValues(
std.os.windows.RTL_REGISTRY_ABSOLUTE,
topkey,
&table,
null,
null,
);
switch (res) {
.SUCCESS => {
inline for (fields_info.field_names, 0..) |field_name, i| switch (@field(args, field_name).value_type) {
.SZ,
.EXPAND_SZ,
.MULTI_SZ,
=> {
var buf = @field(args, field_name).value_buf;
const entry: *const std.os.windows.UNICODE_STRING = @ptrCast(table[i + 1].EntryContext);
const len = try std.unicode.utf16LeToUtf8(buf, entry.slice());
buf[len] = 0;
},
.DWORD,
.DWORD_BIG_ENDIAN,
.QWORD,
=> {
const entry: [*]const u8 = @ptrCast(table[i + 1].EntryContext);
switch (@field(args, field_name).value_type) {
.DWORD, .DWORD_BIG_ENDIAN => {
@memcpy(@field(args, field_name).value_buf[0..4], entry[0..4]);
},
.QWORD => {
@memcpy(@field(args, field_name).value_buf[0..8], entry[0..8]);
},
else => unreachable,
}
},
else => unreachable,
};
},
else => return error.Unexpected,
}
}
fn setFeature(comptime Feature: type, cpu: *Target.Cpu, feature: Feature, enabled: bool) void {
const idx = @as(Target.Cpu.Feature.Set.Index, @backingInt(feature));
if (enabled) cpu.features.addFeature(idx) else cpu.features.removeFeature(idx);
}
/// 如果通过 Windows 注册表精细检测 CPU 特性失败,
/// 则回退到通用 CPU 型号,但使用 `SharedUserData`
/// 内容覆盖特性集。
/// 这实际上就是 LLVM 对 Windows 上所有 ARM 芯片的做法。
fn genericCpuAndNativeFeatures(arch: Target.Cpu.Arch) Target.Cpu {
var cpu: Target.Cpu = .{
.arch = arch,
.model = Target.Cpu.Model.generic(arch),
.features = .empty,
};
cpu.features.addFeatureSet(cpu.model.features);
switch (arch) {
.aarch64, .aarch64_be => {
const Feature = Target.aarch64.Feature;
// 覆盖任何已启用或已禁用的特性。
setFeature(Feature, &cpu, .neon, IsProcessorFeaturePresent(PF.ARM_NEON_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .crc, IsProcessorFeaturePresent(PF.ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .aes, IsProcessorFeaturePresent(PF.ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sha2, IsProcessorFeaturePresent(PF.ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .lse, IsProcessorFeaturePresent(PF.ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .dotprod, IsProcessorFeaturePresent(PF.ARM_V82_DP_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .jsconv, IsProcessorFeaturePresent(PF.ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .rcpc, IsProcessorFeaturePresent(PF.ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve, IsProcessorFeaturePresent(PF.ARM_SVE_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve2, IsProcessorFeaturePresent(PF.ARM_SVE2_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve2p1, IsProcessorFeaturePresent(PF.ARM_SVE2_1_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve_aes, IsProcessorFeaturePresent(PF.ARM_SVE_AES_INSTRUCTIONS_AVAILABLE) or IsProcessorFeaturePresent(PF.ARM_SVE_PMULL128_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve_bitperm, IsProcessorFeaturePresent(PF.ARM_SVE_BITPERM_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .bf16, IsProcessorFeaturePresent(PF.ARM_V86_BF16_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve_b16b16, IsProcessorFeaturePresent(PF.ARM_SVE_B16B16_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve_sha3, IsProcessorFeaturePresent(PF.ARM_SVE_SHA3_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sve_sm4, IsProcessorFeaturePresent(PF.ARM_SVE_SM4_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .i8mm, IsProcessorFeaturePresent(PF.ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .f32mm, IsProcessorFeaturePresent(PF.ARM_SVE_F32MM_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .f64mm, IsProcessorFeaturePresent(PF.ARM_SVE_F64MM_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme, IsProcessorFeaturePresent(PF.ARM_SME_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme2, IsProcessorFeaturePresent(PF.ARM_SME2_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .lse2, IsProcessorFeaturePresent(PF.ARM_LSE2_AVAILABLE));
setFeature(Feature, &cpu, .sha3, IsProcessorFeaturePresent(PF.ARM_SHA3_INSTRUCTIONS_AVAILABLE) and IsProcessorFeaturePresent(PF.ARM_SHA512_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .fullfp16, IsProcessorFeaturePresent(PF.ARM_V82_FP16_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme2p1, IsProcessorFeaturePresent(PF.ARM_SME2_1_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme2p2, IsProcessorFeaturePresent(PF.ARM_SME2_2_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .ssve_aes, IsProcessorFeaturePresent(PF.ARM_SME_AES_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .ssve_bitperm, IsProcessorFeaturePresent(PF.ARM_SME_SBITPERM_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .ssve_fp8dot2, IsProcessorFeaturePresent(PF.ARM_SME_SF8DP2_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .ssve_fp8dot4, IsProcessorFeaturePresent(PF.ARM_SME_SF8DP4_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .ssve_fp8fma, IsProcessorFeaturePresent(PF.ARM_SME_SF8FMA_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme_f8f32, IsProcessorFeaturePresent(PF.ARM_SME_F8F32_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme_f8f16, IsProcessorFeaturePresent(PF.ARM_SME_F8F16_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme_b16b16, IsProcessorFeaturePresent(PF.ARM_SME_B16B16_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme_f64f64, IsProcessorFeaturePresent(PF.ARM_SME_F64F64_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme_i16i64, IsProcessorFeaturePresent(PF.ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme_lutv2, IsProcessorFeaturePresent(PF.ARM_SME_LUTv2_INSTRUCTIONS_AVAILABLE));
setFeature(Feature, &cpu, .sme_fa64, IsProcessorFeaturePresent(PF.ARM_SME_FA64_INSTRUCTIONS_AVAILABLE));
},
else => {},
}
cpu.features.populateDependencies(cpu.arch.allFeaturesList());
return cpu;
}
pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
const current_arch = builtin.cpu.arch;
const cpu: ?Target.Cpu = switch (current_arch) {
.aarch64, .aarch64_be => blk: {
var registers: [12]u64 = undefined;
// CP 4000 -> MIDR_EL1
// CP 4020 -> ID_AA64PFR0_EL1
// CP 4021 -> ID_AA64PFR1_EL1
// CP 4028 -> ID_AA64DFR0_EL1
// CP 4029 -> ID_AA64DFR1_EL1
// CP 402C -> ID_AA64AFR0_EL1
// CP 402D -> ID_AA64AFR1_EL1
// CP 4030 -> ID_AA64ISAR0_EL1
// CP 4031 -> ID_AA64ISAR1_EL1
// CP 4038 -> ID_AA64MMFR0_EL1
// CP 4039 -> ID_AA64MMFR1_EL1
// CP 403A -> ID_AA64MMFR2_EL1
getCpuInfoFromRegistry(0, .{
.{ .key = "CP 4000", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[0])) },
.{ .key = "CP 4020", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[1])) },
.{ .key = "CP 4021", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[2])) },
.{ .key = "CP 4028", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[3])) },
.{ .key = "CP 4029", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[4])) },
.{ .key = "CP 402C", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[5])) },
.{ .key = "CP 402D", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[6])) },
.{ .key = "CP 4030", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[7])) },
.{ .key = "CP 4031", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[8])) },
.{ .key = "CP 4038", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[9])) },
.{ .key = "CP 4039", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[10])) },
.{ .key = "CP 403A", .value_type = REG.ValueType.QWORD, .value_buf = @as(*[8]u8, @ptrCast(®isters[11])) },
}) catch break :blk null;
break :blk @import("arm.zig").aarch64.detectNativeCpuAndFeatures(current_arch, registers);
},
else => null,
};
return cpu orelse genericCpuAndNativeFeatures(current_arch);
}