diff --git a/core/os/os_openbsd.odin b/core/os/os_openbsd.odin index 9a3dbd874..fba079f15 100644 --- a/core/os/os_openbsd.odin +++ b/core/os/os_openbsd.odin @@ -705,4 +705,4 @@ _alloc_command_line_arguments :: proc() -> []string { res[i] = string(arg) } return res -} +} \ No newline at end of file diff --git a/core/sys/info/cpu_intel.odin b/core/sys/info/cpu_intel.odin index e0b5090ca..f1a3e7298 100644 --- a/core/sys/info/cpu_intel.odin +++ b/core/sys/info/cpu_intel.odin @@ -67,8 +67,8 @@ init_cpu_features :: proc "c" () { try_set(&set, .os_xsave, 27, ecx1) try_set(&set, .rdrand, 30, ecx1) - when ODIN_OS == .FreeBSD { - // xgetbv is an illegal instruction under FreeBSD 13 + when ODIN_OS == .FreeBSD || ODIN_OS == .OpenBSD { + // xgetbv is an illegal instruction under FreeBSD 13 & OpenBSD 7.1 // return before probing further cpu_features = set return diff --git a/core/sys/info/platform_openbsd.odin b/core/sys/info/platform_openbsd.odin new file mode 100644 index 000000000..080fad9b7 --- /dev/null +++ b/core/sys/info/platform_openbsd.odin @@ -0,0 +1,69 @@ +// +build openbsd +package sysinfo + +import sys "core:sys/unix" +import "core:strings" +import "core:strconv" + +@(private) +version_string_buf: [1024]u8 + +@(init, private) +init_os_version :: proc () { + os_version.platform = .OpenBSD + + kernel_version_buf: [1024]u8 + + b := strings.builder_from_bytes(version_string_buf[:]) + // Retrieve kernel info using `sysctl`, e.g. OpenBSD + mib := []i32{sys.CTL_KERN, sys.KERN_OSTYPE} + if !sys.sysctl(mib, &kernel_version_buf) { + return + } + os_type := string(cstring(raw_data(kernel_version_buf[:]))) + strings.write_string(&b, os_type) + + mib = []i32{sys.CTL_KERN, sys.KERN_OSRELEASE} + if !sys.sysctl(mib, &kernel_version_buf) { + return + } + + strings.write_rune(&b, ' ') + version := string(cstring(raw_data(kernel_version_buf[:]))) + strings.write_string(&b, version) + + // // Parse kernel version + triplet := strings.split(version, ".", context.temp_allocator) + if len(triplet) == 2 { + major, major_ok := strconv.parse_int(triplet[0]) + minor, minor_ok := strconv.parse_int(triplet[1]) + + if major_ok && minor_ok { + os_version.major = major + os_version.minor = minor + } + } + + // Retrieve kernel revision using `sysctl`, e.g. 199506 + mib = []i32{sys.CTL_KERN, sys.KERN_OSREV} + revision: int + if !sys.sysctl(mib, &revision) { + return + } + os_version.patch = revision + strings.write_string(&b, ", build ") + strings.write_int(&b, revision) + + // Finalize pretty name. + os_version.as_string = strings.to_string(b) +} + +@(init) +init_ram :: proc() { + // Retrieve RAM info using `sysctl` + mib := []i32{sys.CTL_HW, sys.HW_PHYSMEM64} + mem_size: u64 + if sys.sysctl(mib, &mem_size) { + ram.total_ram = int(mem_size) + } +} \ No newline at end of file diff --git a/core/sys/unix/syscalls_openbsd.odin b/core/sys/unix/syscalls_openbsd.odin new file mode 100644 index 000000000..703bc4d4e --- /dev/null +++ b/core/sys/unix/syscalls_openbsd.odin @@ -0,0 +1,6 @@ +package unix + +// OpenBSD 7.1 syscall numbers +// See: /usr/include/sys/syscall.h + +SYS_sysctl : uintptr : 202 \ No newline at end of file diff --git a/core/sys/unix/sysctl_freebsd.odin.odin b/core/sys/unix/sysctl_freebsd.odin similarity index 100% rename from core/sys/unix/sysctl_freebsd.odin.odin rename to core/sys/unix/sysctl_freebsd.odin diff --git a/core/sys/unix/sysctl_openbsd.odin b/core/sys/unix/sysctl_openbsd.odin new file mode 100644 index 000000000..b93e8f9bd --- /dev/null +++ b/core/sys/unix/sysctl_openbsd.odin @@ -0,0 +1,49 @@ +//+build openbsd +package unix + +import "core:c" +foreign import libc "system:c" + +@(default_calling_convention="c") +foreign libc { + @(link_name="sysctl") _unix_sysctl :: proc(name: [^]i32, namelen: u32, oldp: rawptr, oldlenp: ^c.size_t, newp: rawptr, newlen: c.size_t) -> i32 --- +} + +sysctl :: proc(mib: []i32, val: ^$T) -> (ok: bool) { + mib := mib + result_size := c.size_t(size_of(T)) + res := _unix_sysctl(raw_data(mib), u32(len(mib)), val, &result_size, nil, 0) + return res == 0 +} + +// See /usr/include/sys/sysctl.h for details +CTL_SYSCTL :: 0 +CTL_KERN :: 1 + KERN_OSTYPE :: 1 + KERN_OSRELEASE :: 2 + KERN_OSREV :: 3 + KERN_VERSION :: 4 +CTL_VM :: 2 +CTL_FS :: 3 +CTL_NET :: 4 +CTL_DEBUG :: 5 +CTL_HW :: 6 + HW_MACHINE :: 1 + HW_MODEL :: 2 + HW_NCPU :: 3 + HW_BYTEORDER :: 4 + HW_PHYSMEM :: 5 + HW_USERMEM :: 6 + HW_PAGESIZE :: 7 + HW_DISKNAMES :: 8 + HW_DISKSTATS :: 9 + HW_DISKCOUNT :: 10 + HW_SENSORS :: 11 + HW_CPUSPEED :: 12 + HW_SETPERF :: 13 + HW_VENDOR :: 14 + HW_PRODUCT :: 15 + HW_VERSION :: 16 + HW_SERIALNO :: 17 + HW_UUID :: 18 + HW_PHYSMEM64 :: 19 \ No newline at end of file