From 54ada291b199a9f9839dc803cc6ba1c67f9e5708 Mon Sep 17 00:00:00 2001 From: Trung Nguyen <57174311+trungnt2910@users.noreply.github.com> Date: Sat, 22 Nov 2025 01:33:05 +1100 Subject: [PATCH] Haiku: Initial managed libraries support This contains the code required to build the first managed runtime libraries for Haiku, namely `System.Private.CoreLib`. Co-authored-by: Jessica Hamilton --- .../Common/src/Interop/Haiku/Interop.OS.cs | 28 +++++++++++++ .../System.Private.CoreLib.Shared.projitems | 6 +++ .../src/System/Environment.Haiku.cs | 28 +++++++++++++ .../src/System/OperatingSystem.cs | 2 + src/native/libs/Common/pal_config.h.in | 1 + src/native/libs/System.Native/CMakeLists.txt | 1 + src/native/libs/System.Native/entrypoints.c | 2 + src/native/libs/System.Native/pal_getosinfo.c | 42 +++++++++++++++++++ src/native/libs/System.Native/pal_getosinfo.h | 15 +++++++ src/native/libs/configure.cmake | 4 ++ 10 files changed, 129 insertions(+) create mode 100644 src/libraries/Common/src/Interop/Haiku/Interop.OS.cs create mode 100644 src/libraries/System.Private.CoreLib/src/System/Environment.Haiku.cs create mode 100644 src/native/libs/System.Native/pal_getosinfo.c create mode 100644 src/native/libs/System.Native/pal_getosinfo.h diff --git a/src/libraries/Common/src/Interop/Haiku/Interop.OS.cs b/src/libraries/Common/src/Interop/Haiku/Interop.OS.cs new file mode 100644 index 00000000000000..3246a7c45bd0cd --- /dev/null +++ b/src/libraries/Common/src/Interop/Haiku/Interop.OS.cs @@ -0,0 +1,28 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class OS + { + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct AreaInfo + { + public nuint size; + public uint ram_size; + } + + /// + /// Gets information about areas owned by a team. + /// + /// The team ID of the areas to iterate. + /// A cookie to track the iteration. + /// The structure to fill in. + /// Returns 0 on success. Returns an error code on failure or when there are no more areas to iterate. + [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNextAreaInfo")] + internal static unsafe partial int GetNextAreaInfo(int team, ref nint cookie, out AreaInfo areaInfo); + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 2db9af120d00cc..fe63de3b15d6f7 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -2720,6 +2720,7 @@ + @@ -2739,6 +2740,11 @@ + + + Common\Interop\Haiku\Interop.OS.cs + + Common\Interop\OSX\Interop.libobjc.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.Haiku.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.Haiku.cs new file mode 100644 index 00000000000000..fba9180e81a204 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.Haiku.cs @@ -0,0 +1,28 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.InteropServices; + +namespace System +{ + public static partial class Environment + { + public static unsafe long WorkingSet + { + get + { + nint cookie = 0; + long workingSet = 0; + + Interop.OS.AreaInfo areaInfo; + + while (Interop.OS.GetNextAreaInfo(ProcessId, ref cookie, out areaInfo) == 0) + { + workingSet += areaInfo.ram_size; + } + + return workingSet; + } + } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs b/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs index d8b3c14b103aae..8134e38145590a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs +++ b/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs @@ -40,6 +40,8 @@ public sealed class OperatingSystem : ISerializable, ICloneable "ILLUMOS" #elif TARGET_SOLARIS "SOLARIS" +#elif TARGET_HAIKU + "HAIKU" #else #error Unknown OS, add a corresponding TARGET_* constant to System.Private.CoreLib.Shared.projitems #endif diff --git a/src/native/libs/Common/pal_config.h.in b/src/native/libs/Common/pal_config.h.in index b546abba1c91c3..b0fe427baeeb50 100644 --- a/src/native/libs/Common/pal_config.h.in +++ b/src/native/libs/Common/pal_config.h.in @@ -139,6 +139,7 @@ #cmakedefine01 HAVE_NET_IF_H #cmakedefine01 HAVE_SYS_PROCINFO_H #cmakedefine01 HAVE_IOSS_H +#cmakedefine01 HAVE_OS_H #cmakedefine01 HAVE_ALIGNED_ALLOC #cmakedefine01 HAVE_MALLOC_SIZE #cmakedefine01 HAVE_MALLOC_USABLE_SIZE diff --git a/src/native/libs/System.Native/CMakeLists.txt b/src/native/libs/System.Native/CMakeLists.txt index 8fa04ec15a9c9e..0beca0c2bb0ba2 100644 --- a/src/native/libs/System.Native/CMakeLists.txt +++ b/src/native/libs/System.Native/CMakeLists.txt @@ -6,6 +6,7 @@ endif () set(NATIVE_SOURCES pal_errno.c + pal_getosinfo.c pal_io.c pal_maphardwaretype.c pal_memory.c diff --git a/src/native/libs/System.Native/entrypoints.c b/src/native/libs/System.Native/entrypoints.c index ffff33bfadd6cc..34131dee0312e5 100644 --- a/src/native/libs/System.Native/entrypoints.c +++ b/src/native/libs/System.Native/entrypoints.c @@ -10,6 +10,7 @@ #include "pal_dynamicload.h" #include "pal_environment.h" #include "pal_errno.h" +#include "pal_getosinfo.h" #include "pal_interfaceaddresses.h" #include "pal_io.h" #include "pal_iossupportversion.h" @@ -305,6 +306,7 @@ static const Entry s_sysNative[] = DllImportEntry(SystemNative_LowLevelCrossProcessMutex_IsAbandoned) DllImportEntry(SystemNative_LowLevelCrossProcessMutex_SetAbandoned) DllImportEntry(SystemNative_Select) + DllImportEntry(SystemNative_GetNextAreaInfo) }; EXTERN_C const void* SystemResolveDllImport(const char* name); diff --git a/src/native/libs/System.Native/pal_getosinfo.c b/src/native/libs/System.Native/pal_getosinfo.c new file mode 100644 index 00000000000000..c499e9b9be68cb --- /dev/null +++ b/src/native/libs/System.Native/pal_getosinfo.c @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "pal_config.h" +#include "pal_getosinfo.h" +#include "pal_utilities.h" + +#include +#include + +#if HAVE_OS_H +#include +#endif + +int32_t SystemNative_GetNextAreaInfo(int32_t team, intptr_t* cookie, AreaInfo* areaInfo) +{ +#if HAVE_OS_H + if (cookie == NULL || areaInfo == NULL) + { + return EINVAL; + } + + area_info nativeAreaInfo; + memset(&nativeAreaInfo, 0, sizeof(nativeAreaInfo)); + + status_t status = get_next_area_info((team_id)team, cookie, &nativeAreaInfo); + if (status != B_OK) + { + return (int32_t)status; + } + + areaInfo->size = (uintptr_t)nativeAreaInfo.size; + areaInfo->ram_size = (uint32_t)nativeAreaInfo.ram_size; + + return (int32_t)status; +#else + (void)team; + (void)cookie; + (void)areaInfo; + return ENOTSUP; +#endif +} diff --git a/src/native/libs/System.Native/pal_getosinfo.h b/src/native/libs/System.Native/pal_getosinfo.h new file mode 100644 index 00000000000000..464269e1054347 --- /dev/null +++ b/src/native/libs/System.Native/pal_getosinfo.h @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#pragma once + +#include "pal_compiler.h" +#include "pal_types.h" + +typedef struct +{ + uintptr_t size; + uint32_t ram_size; +} AreaInfo; + +PALEXPORT int32_t SystemNative_GetNextAreaInfo(int32_t team, intptr_t* cookie, AreaInfo* areaInfo); diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index d5fbbc082dd3d4..905801237dfcb3 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -1004,6 +1004,10 @@ check_include_files( IOKit/serial/ioss.h HAVE_IOSS_H) +check_include_files( + OS.h + HAVE_OS_H) + check_symbol_exists( getpeereid "unistd.h;sys/types.h;sys/socket.h"