Skip to content

Commit bf44aaa

Browse files
Haiku: Initial CoreCLR support (dotnet#109580)
* Haiku: Initial CoreCLR support This contains a part of the code required to build CoreCLR and get `paltests` to pass on Haiku. This commit covers `src/coreclr/**`, except the PAL files included in the previous commit. Co-authored-by: Jessica Hamilton <[email protected]> * pal: Add back checks for HAVE_CLOCK_THREAD_CPUTIME This was removed in dotnet#103441 but required for Haiku builds. --------- Co-authored-by: Jessica Hamilton <[email protected]>
1 parent 5fe72c1 commit bf44aaa

File tree

16 files changed

+120
-35
lines changed

16 files changed

+120
-35
lines changed

eng/native/tryrun.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if(DARWIN)
5555
set_cache_value(HAVE_CLOCK_MONOTONIC_COARSE_EXITCODE 1)
5656
set_cache_value(HAVE_CLOCK_MONOTONIC_EXITCODE 0)
5757
set_cache_value(HAVE_CLOCK_REALTIME_EXITCODE 0)
58+
set_cache_value(HAVE_CLOCK_THREAD_CPUTIME_EXITCODE 0)
5859
set_cache_value(HAVE_CLOCK_GETTIME_NSEC_NP_EXITCODE 0)
5960
set_cache_value(HAVE_FUNCTIONAL_PTHREAD_ROBUST_MUTEXES_EXITCODE 1)
6061
set_cache_value(HAVE_MMAP_DEV_ZERO_EXITCODE 1)
@@ -79,6 +80,7 @@ elseif(TARGET_ARCH_NAME MATCHES "^(armel|arm|armv6|arm64|loongarch64|riscv64|s39
7980
set_cache_value(HAVE_CLOCK_MONOTONIC_COARSE_EXITCODE 0)
8081
set_cache_value(HAVE_CLOCK_MONOTONIC_EXITCODE 0)
8182
set_cache_value(HAVE_CLOCK_REALTIME_EXITCODE 0)
83+
set_cache_value(HAVE_CLOCK_THREAD_CPUTIME_EXITCODE 0)
8284
set_cache_value(HAVE_MMAP_DEV_ZERO_EXITCODE 0)
8385
set_cache_value(HAVE_PROCFS_CTL_EXITCODE 1)
8486
set_cache_value(HAVE_PROCFS_STAT_EXITCODE 0)

eng/native/tryrun_ios_tvos.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set_cache_value(HAS_POSIX_SEMAPHORES_EXITCODE 1)
1414
set_cache_value(HAVE_BROKEN_FIFO_KEVENT_EXITCODE 1)
1515
set_cache_value(HAVE_BROKEN_FIFO_SELECT_EXITCODE 1)
1616
set_cache_value(HAVE_CLOCK_REALTIME_EXITCODE 0)
17+
set_cache_value(HAVE_CLOCK_THREAD_CPUTIME_EXITCODE 0)
1718
set_cache_value(HAVE_CLOCK_GETTIME_NSEC_NP_EXITCODE 0)
1819
set_cache_value(HAVE_FUNCTIONAL_PTHREAD_ROBUST_MUTEXES_EXITCODE 1)
1920
set_cache_value(HAVE_MMAP_DEV_ZERO_EXITCODE 1)

src/coreclr/debug/dbgutil/elfreader.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@
99
#include <inttypes.h>
1010
#include "elfreader.h"
1111

12+
#ifndef Elf_Ehdr
1213
#define Elf_Ehdr ElfW(Ehdr)
14+
#endif
15+
#ifndef Elf_Phdr
1316
#define Elf_Phdr ElfW(Phdr)
17+
#endif
18+
#ifndef Elf_Shdr
1419
#define Elf_Shdr ElfW(Shdr)
20+
#endif
21+
#ifndef Elf_Nhdr
1522
#define Elf_Nhdr ElfW(Nhdr)
23+
#endif
24+
#ifndef Elf_Dyn
1625
#define Elf_Dyn ElfW(Dyn)
26+
#endif
27+
#ifndef Elf_Sym
1728
#define Elf_Sym ElfW(Sym)
29+
#endif
1830

1931
#if TARGET_64BIT
2032
#define PRIx PRIx64
@@ -314,7 +326,7 @@ ElfReader::GetStringAtIndex(int index, std::string& result)
314326
return true;
315327
}
316328

317-
#ifdef HOST_UNIX
329+
#if defined(HOST_UNIX) && !defined(TARGET_HAIKU)
318330

319331
//
320332
// Enumerate all the ELF info starting from the root program header. This
@@ -414,7 +426,7 @@ ElfReader::EnumerateLinkMapEntries(Elf_Dyn* dynamicAddr)
414426
return true;
415427
}
416428

417-
#endif // HOST_UNIX
429+
#endif // defined(HOST_UNIX) && !defined(TARGET_HAIKU)
418430

419431
bool
420432
ElfReader::EnumerateProgramHeaders(uint64_t baseAddress, uint64_t* ploadbias, Elf_Dyn** pdynamicAddr)

src/coreclr/debug/dbgutil/elfreader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ElfReader
3838
void* m_symbolTableAddr; // DT_SYMTAB
3939

4040
GnuHashTable m_hashTable; // gnu hash table info
41-
int32_t* m_buckets; // gnu hash table buckets
41+
int32_t* m_buckets; // gnu hash table buckets
4242
void* m_chainsAddress;
4343

4444
public:

src/coreclr/gc/unix/cgroup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Module Name:
2424
#if defined(__APPLE__) || defined(__FreeBSD__)
2525
#include <sys/param.h>
2626
#include <sys/mount.h>
27-
#else
27+
#elif !defined(__HAIKU__)
2828
#include <sys/vfs.h>
2929
#endif
3030
#include <errno.h>
@@ -55,7 +55,7 @@ Module Name:
5555

5656
extern bool ReadMemoryValueFromFile(const char* filename, uint64_t* val);
5757

58-
namespace
58+
namespace
5959
{
6060
class CGroup
6161
{

src/coreclr/gc/unix/gcenv.unix.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ extern "C"
9090

9191
#endif // __APPLE__
9292

93+
#ifdef __HAIKU__
94+
#include <OS.h>
95+
#endif // __HAIKU__
96+
9397
#ifdef __linux__
9498
#include <sys/syscall.h> // __NR_membarrier
9599
// Ensure __NR_membarrier is defined for portable builds.
@@ -572,7 +576,11 @@ static void* VirtualReserveInner(size_t size, size_t alignment, uint32_t flags,
572576
}
573577

574578
size_t alignedSize = size + (alignment - OS_PAGE_SIZE);
575-
void * pRetVal = mmap(nullptr, alignedSize, PROT_NONE, MAP_ANON | MAP_PRIVATE | hugePagesFlag, -1, 0);
579+
int mmapFlags = MAP_ANON | MAP_PRIVATE | hugePagesFlag;
580+
#ifdef __HAIKU__
581+
mmapFlags |= MAP_NORESERVE;
582+
#endif
583+
void * pRetVal = mmap(nullptr, alignedSize, PROT_NONE, mmapFlags, -1, 0);
576584

577585
if (pRetVal != MAP_FAILED)
578586
{
@@ -721,7 +729,11 @@ bool GCToOSInterface::VirtualDecommit(void* address, size_t size)
721729
// that much more clear to the operating system that we no
722730
// longer need these pages. Also, GC depends on re-committed pages to
723731
// be zeroed-out.
724-
bool bRetVal = mmap(address, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0) != MAP_FAILED;
732+
int mmapFlags = MAP_FIXED | MAP_ANON | MAP_PRIVATE;
733+
#ifdef TARGET_HAIKU
734+
mmapFlags |= MAP_NORESERVE;
735+
#endif
736+
bool bRetVal = mmap(address, size, PROT_NONE, mmapFlags, -1, 0) != MAP_FAILED;
725737

726738
#ifdef MADV_DONTDUMP
727739
if (bRetVal)
@@ -1022,7 +1034,7 @@ static uint64_t GetMemorySizeMultiplier(char units)
10221034
return 1;
10231035
}
10241036

1025-
#ifndef __APPLE__
1037+
#if !defined(__APPLE__) && !defined(__HAIKU__)
10261038
// Try to read the MemAvailable entry from /proc/meminfo.
10271039
// Return true if the /proc/meminfo existed, the entry was present and we were able to parse it.
10281040
static bool ReadMemAvailable(uint64_t* memAvailable)
@@ -1055,7 +1067,7 @@ static bool ReadMemAvailable(uint64_t* memAvailable)
10551067

10561068
return foundMemAvailable;
10571069
}
1058-
#endif // __APPLE__
1070+
#endif // !defined(__APPLE__) && !defined(__HAIKU__)
10591071

10601072
// Get size of the largest cache on the processor die
10611073
// Parameters:
@@ -1284,6 +1296,12 @@ uint64_t GetAvailablePhysicalMemory()
12841296
sysctlbyname("vm.stats.vm.v_free_count", &free_count, &sz, NULL, 0);
12851297

12861298
available = (inactive_count + laundry_count + free_count) * sysconf(_SC_PAGESIZE);
1299+
#elif defined(__HAIKU__)
1300+
system_info info;
1301+
if (get_system_info(&info) == B_OK)
1302+
{
1303+
available = info.free_memory;
1304+
}
12871305
#else // Linux
12881306
static volatile bool tryReadMemInfo = true;
12891307

src/coreclr/gc/unix/numasupport.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#include <dirent.h>
1111
#include <string.h>
1212
#include <limits.h>
13-
#include <sys/syscall.h>
1413
#include <minipal/utils.h>
1514

15+
#ifdef TARGET_LINUX
16+
#include <sys/syscall.h>
17+
#endif
18+
1619
// The highest NUMA node available
1720
int g_highestNumaNode = 0;
1821
// Is numa available

src/coreclr/inc/crosscomp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ typedef struct _T_KNONVOLATILE_CONTEXT_POINTERS {
720720
#define DAC_CS_NATIVE_DATA_SIZE 56
721721
#elif defined(__sun) && defined(TARGET_AMD64)
722722
#define DAC_CS_NATIVE_DATA_SIZE 48
723+
#elif defined(TARGET_HAIKU) && defined(TARGET_AMD64)
724+
#define DAC_CS_NATIVE_DATA_SIZE 56
723725
#else
724726
#warning
725727
#error DAC_CS_NATIVE_DATA_SIZE is not defined for this architecture. This should be same value as PAL_CS_NATIVE_DATA_SIZE (aka sizeof(PAL_CS_NATIVE_DATA)).

src/coreclr/minipal/Unix/doublemapping.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,22 @@ bool VMToOSInterface::CreateDoubleMemoryMapper(void** pHandle, size_t *pMaxExecu
4848

4949
#ifdef TARGET_FREEBSD
5050
int fd = shm_open(SHM_ANON, O_RDWR | O_CREAT, S_IRWXU);
51-
#elif defined(TARGET_SUNOS) // has POSIX implementation
52-
char name[24];
53-
sprintf(name, "/shm-dotnet-%d", getpid());
54-
name[sizeof(name) - 1] = '\0';
55-
shm_unlink(name);
56-
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
57-
#else // TARGET_FREEBSD
51+
#elif defined(TARGET_LINUX)
5852
int fd = memfd_create("doublemapper", MFD_CLOEXEC);
59-
#endif // TARGET_FREEBSD
53+
#else
54+
int fd = -1;
55+
#endif
56+
57+
// POSIX fallback
58+
if (fd == -1)
59+
{
60+
char name[24];
61+
sprintf(name, "/shm-dotnet-%d", getpid());
62+
name[sizeof(name) - 1] = '\0';
63+
shm_unlink(name);
64+
fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
65+
shm_unlink(name);
66+
}
6067

6168
if (fd == -1)
6269
{
@@ -135,10 +142,14 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs
135142

136143
void* result = PAL_VirtualReserveFromExecutableMemoryAllocatorWithinRange(rangeStart, rangeEnd, size, 0 /* fStoreAllocationInfo */);
137144
#ifndef TARGET_OSX
145+
int mmapFlags = MAP_SHARED;
146+
#ifdef TARGET_HAIKU
147+
mmapFlags |= MAP_NORESERVE;
148+
#endif // TARGET_HAIKU
138149
if (result != NULL)
139150
{
140151
// Map the shared memory over the range reserved from the executable memory allocator.
141-
result = mmap(result, size, PROT_NONE, MAP_SHARED | MAP_FIXED, fd, offset);
152+
result = mmap(result, size, PROT_NONE, mmapFlags | MAP_FIXED, fd, offset);
142153
if (result == MAP_FAILED)
143154
{
144155
assert(false);
@@ -154,15 +165,15 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs
154165
}
155166

156167
#ifndef TARGET_OSX
157-
result = mmap(NULL, size, PROT_NONE, MAP_SHARED, fd, offset);
168+
result = mmap(NULL, size, PROT_NONE, mmapFlags, fd, offset);
158169
#else
159170
int mmapFlags = MAP_ANON | MAP_PRIVATE;
160171
if (IsMapJitFlagNeeded())
161172
{
162173
mmapFlags |= MAP_JIT;
163174
}
164175
result = mmap(NULL, size, PROT_NONE, mmapFlags, -1, 0);
165-
#endif
176+
#endif
166177
if (result == MAP_FAILED)
167178
{
168179
assert(false);

src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <sched.h>
3333
#include <sys/mman.h>
3434
#include <sys/types.h>
35-
#include <sys/syscall.h>
3635
#include <dlfcn.h>
3736
#include <dirent.h>
3837
#include <string.h>
@@ -44,6 +43,10 @@
4443
#include <signal.h>
4544
#include <minipal/thread.h>
4645

46+
#ifdef TARGET_LINUX
47+
#include <sys/syscall.h>
48+
#endif
49+
4750
#if HAVE_PTHREAD_GETTHREADID_NP
4851
#include <pthread_np.h>
4952
#endif
@@ -60,6 +63,10 @@
6063
#include <mach/mach.h>
6164
#endif
6265

66+
#ifdef TARGET_HAIKU
67+
#include <OS.h>
68+
#endif
69+
6370
using std::nullptr_t;
6471

6572
#define PalRaiseFailFastException RaiseFailFastException

0 commit comments

Comments
 (0)