Skip to content

Commit ffcef3d

Browse files
Haiku: Add Haiku support for CoreCLR/PAL (#93907)
* Haiku: Add Haiku support for CoreCLR/PAL This contains a part of the code required to build CoreCLR and get `paltests` to pass on Haiku. This commit covers `src/coreclr/pal/**`. Co-authored-by: Jessica Hamilton <jessica.l.hamilton@gmail.com> * ilasm: Haiku build fixes Some OSes like Haiku define the `_EXPORT` and `_IMPORT` marcos. Symbols beginning with an underscore followed immediately by an uppercase letter are reserved anyway. Add the guards to get them out of the way. * pal: Remove usage of MAP_FILE This flag is a compatibility flag and not required on most OSes. Some, including Haiku, does not even define it. --------- Co-authored-by: Jessica Hamilton <jessica.l.hamilton@gmail.com>
1 parent 2d07d3d commit ffcef3d

File tree

25 files changed

+152
-35
lines changed

25 files changed

+152
-35
lines changed

src/coreclr/ilasm/ilasmpch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@
2525
#include "mdfileformat.h"
2626
#include "stgpooli.h"
2727

28+
#ifdef _EXPORT
29+
#undef _EXPORT
30+
#endif
31+
32+
#ifdef _IMPORT
33+
#undef _IMPORT
34+
#endif
35+
2836
#endif

src/coreclr/pal/inc/pal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,6 +2644,8 @@ PALIMPORT BOOL PALAPI PAL_GetUnwindInfoSize(SIZE_T baseAddress, ULONG64 ehFrameH
26442644
#define PAL_CS_NATIVE_DATA_SIZE 96
26452645
#elif defined(__linux__) && defined(__riscv) && __riscv_xlen == 64
26462646
#define PAL_CS_NATIVE_DATA_SIZE 96
2647+
#elif defined(__HAIKU__) && defined(__x86_64__)
2648+
#define PAL_CS_NATIVE_DATA_SIZE 56
26472649
#else
26482650
#error PAL_CS_NATIVE_DATA_SIZE is not defined for this architecture
26492651
#endif
@@ -3952,7 +3954,9 @@ unsigned int __cdecl _rotr(unsigned int value, int shift)
39523954
PALIMPORT DLLEXPORT char * __cdecl PAL_getenv(const char *);
39533955
PALIMPORT DLLEXPORT int __cdecl _putenv(const char *);
39543956

3957+
#ifndef ERANGE
39553958
#define ERANGE 34
3959+
#endif
39563960

39573961
/****************PAL Perf functions for PInvoke*********************/
39583962
#if PAL_PERF

src/coreclr/pal/inc/rt/safecrt.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,6 @@ typedef int errno_t; /* standard */
112112
/* error codes */
113113
#if !defined(_SECURECRT_ERRCODE_VALUES_DEFINED)
114114
#define _SECURECRT_ERRCODE_VALUES_DEFINED
115-
#if !defined(EINVAL)
116-
#define EINVAL 22
117-
#endif
118-
#if !defined(ERANGE)
119-
#define ERANGE 34
120-
#endif
121-
#if !defined(EILSEQ)
122-
#define EILSEQ 42
123-
#endif
124115
#if !defined(STRUNCATE)
125116
#define STRUNCATE 80
126117
#endif

src/coreclr/pal/src/arch/amd64/signalhandlerhelper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ SET_DEFAULT_DEBUG_CHANNEL(EXCEPT); // some headers have code with asserts, so do
88
#include "pal/context.h"
99
#include "pal/signal.hpp"
1010
#include "pal/utils.h"
11+
12+
#if HAVE_SYS_UCONTEXT_H
1113
#include <sys/ucontext.h>
14+
#endif
1215

1316
/*++
1417
Function :

src/coreclr/pal/src/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#cmakedefine01 HAVE_PTHREAD_NP_H
2222
#cmakedefine01 HAVE_AUXV_HWCAP_H
2323
#cmakedefine01 HAVE_SYS_PTRACE_H
24+
#cmakedefine01 HAVE_SYS_UCONTEXT_H
25+
#cmakedefine01 HAVE_SYS_MOUNT_H
2426
#cmakedefine01 HAVE_UCONTEXT_H
2527
#cmakedefine01 HAVE_GETAUXVAL
2628

src/coreclr/pal/src/configure.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ check_include_files(semaphore.h HAVE_SEMAPHORE_H)
4747
check_include_files(sys/prctl.h HAVE_PRCTL_H)
4848
check_include_files("sys/auxv.h;asm/hwcap.h" HAVE_AUXV_HWCAP_H)
4949
check_include_files("sys/ptrace.h" HAVE_SYS_PTRACE_H)
50+
check_include_files("sys/ucontext.h" HAVE_SYS_UCONTEXT_H)
51+
check_include_files("sys/mount.h" HAVE_SYS_MOUNT_H)
5052
check_include_files(ucontext.h HAVE_UCONTEXT_H)
5153
check_symbol_exists(getauxval sys/auxv.h HAVE_GETAUXVAL)
5254

src/coreclr/pal/src/debug/debug.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ SET_DEFAULT_DEBUG_CHANNEL(DEBUG); // some headers have code with asserts, so do
4545
#include <unistd.h>
4646
#elif defined(HAVE_TTRACE) // HAVE_PROCFS_CTL
4747
#include <sys/ttrace.h>
48-
#else // defined(HAVE_TTRACE)
48+
#elif HAVE_SYS_PTRACE_H
4949
#include <sys/ptrace.h>
5050
#endif // HAVE_PROCFS_CTL
5151
#if HAVE_VM_READ
@@ -635,7 +635,7 @@ PAL_CloseProcessMemory(
635635
PAL_ReadProcessMemory
636636
637637
Abstract
638-
Reads process memory.
638+
Reads process memory.
639639
640640
Parameter
641641
handle : from PAL_OpenProcessMemory
@@ -753,7 +753,7 @@ PAL_ProbeMemory(
753753

754754
flags = fcntl(fds[0], F_GETFL, 0);
755755
fcntl(fds[0], F_SETFL, flags | O_NONBLOCK);
756-
756+
757757
flags = fcntl(fds[1], F_GETFL, 0);
758758
fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
759759

src/coreclr/pal/src/exception/seh.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ PAL_ERROR SEHEnable(CPalThread *pthrCurrent)
305305
{
306306
#if HAVE_MACH_EXCEPTIONS
307307
return pthrCurrent->EnableMachExceptions();
308-
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun)
308+
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__)
309309
return NO_ERROR;
310310
#else// HAVE_MACH_EXCEPTIONS
311311
#error not yet implemented
@@ -330,7 +330,7 @@ PAL_ERROR SEHDisable(CPalThread *pthrCurrent)
330330
{
331331
#if HAVE_MACH_EXCEPTIONS
332332
return pthrCurrent->DisableMachExceptions();
333-
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun)
333+
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__)
334334
return NO_ERROR;
335335
#else // HAVE_MACH_EXCEPTIONS
336336
#error not yet implemented

src/coreclr/pal/src/exception/signal.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ SET_DEFAULT_DEBUG_CHANNEL(EXCEPT); // some headers have code with asserts, so do
4444
#include "pal/utils.h"
4545

4646
#include <string.h>
47-
#include <sys/ucontext.h>
4847
#include <sys/utsname.h>
4948
#include <unistd.h>
5049
#include <sys/mman.h>
5150

51+
#if HAVE_SYS_UCONTEXT_H
52+
#include <sys/ucontext.h>
53+
#endif // HAVE_SYS_UCONTEXT_H
5254

5355
#endif // !HAVE_MACH_EXCEPTIONS
5456
#include "pal/context.h"

src/coreclr/pal/src/file/file.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ SET_DEFAULT_DEBUG_CHANNEL(FILE); // some headers have code with asserts, so do t
3232
#include <sys/types.h>
3333
#include <sys/stat.h>
3434
#include <sys/param.h>
35-
#include <sys/mount.h>
3635
#include <errno.h>
3736
#include <limits.h>
3837
#include <fcntl.h>
3938

39+
#if HAVE_SYS_MOUNT_H
40+
#include <sys/mount.h>
41+
#endif
42+
4043
using namespace CorUnix;
4144

4245
int MaxWCharToAcpLengthFactor = 3;

src/coreclr/pal/src/include/pal/context.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ extern "C"
3333
/* A type to wrap the native context type, which is ucontext_t on some
3434
* platforms and another type elsewhere. */
3535
#if HAVE_UCONTEXT_T
36+
#if HAVE_UCONTEXT_H
3637
#include <ucontext.h>
38+
#endif // HAVE_UCONTEXT_H
3739

3840
typedef ucontext_t native_context_t;
3941
#else // HAVE_UCONTEXT_T
@@ -935,6 +937,41 @@ inline void *FPREG_Xstate_Hi16Zmm(const ucontext_t *uc, uint32_t *featureSize)
935937
*featureSize = sizeof(_STRUCT_ZMM_REG) * 16;
936938
return reinterpret_cast<void *>(&((_STRUCT_X86_AVX512_STATE64&)FPSTATE(uc)).__fpu_zmm16);
937939
}
940+
#elif defined(TARGET_HAIKU)
941+
942+
#define MCREG_Rbp(mc) ((mc).rbp)
943+
#define MCREG_Rip(mc) ((mc).rip)
944+
#define MCREG_Rsp(mc) ((mc).rsp)
945+
#define MCREG_Rsi(mc) ((mc).rsi)
946+
#define MCREG_Rdi(mc) ((mc).rdi)
947+
#define MCREG_Rbx(mc) ((mc).rbx)
948+
#define MCREG_Rdx(mc) ((mc).rdx)
949+
#define MCREG_Rcx(mc) ((mc).rcx)
950+
#define MCREG_Rax(mc) ((mc).rax)
951+
#define MCREG_R8(mc) ((mc).r8)
952+
#define MCREG_R9(mc) ((mc).r9)
953+
#define MCREG_R10(mc) ((mc).r10)
954+
#define MCREG_R11(mc) ((mc).r11)
955+
#define MCREG_R12(mc) ((mc).r12)
956+
#define MCREG_R13(mc) ((mc).r13)
957+
#define MCREG_R14(mc) ((mc).r14)
958+
#define MCREG_R15(mc) ((mc).r15)
959+
#define MCREG_EFlags(mc) ((mc).rflags)
960+
// Haiku: missing SegCs
961+
962+
#define FPSTATE(uc) ((uc)->uc_mcontext.fpu)
963+
#define FPREG_ControlWord(uc) FPSTATE(uc).fp_fxsave.control
964+
#define FPREG_StatusWord(uc) FPSTATE(uc).fp_fxsave.status
965+
#define FPREG_TagWord(uc) FPSTATE(uc).fp_fxsave.tag
966+
#define FPREG_MxCsr(uc) FPSTATE(uc).fp_fxsave.mxcsr
967+
#define FPREG_MxCsr_Mask(uc) FPSTATE(uc).fp_fxsave.mscsr_mask
968+
#define FPREG_ErrorOffset(uc) *(DWORD*) &(FPSTATE(uc).fp_fxsave.rip)
969+
#define FPREG_ErrorSelector(uc) *((WORD*) &(FPSTATE(uc).fp_fxsave.rip) + 2)
970+
#define FPREG_DataOffset(uc) *(DWORD*) &(FPSTATE(uc).fp_fxsave.rdp)
971+
#define FPREG_DataSelector(uc) *((WORD*) &(FPSTATE(uc).fp_fxsave.rdp) + 2)
972+
973+
#define FPREG_Xmm(uc, index) *(M128A*) &(FPSTATE(uc).fp_fxsave.xmm[index])
974+
#define FPREG_St(uc, index) *(M128A*) &(FPSTATE(uc).fp_fxsave.fp[index].value)
938975
#else //TARGET_OSX
939976

940977
// For FreeBSD, as found in x86/ucontext.h

src/coreclr/pal/src/include/pal/palinternal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ function_name() to call the system's implementation
193193
#undef sigsetjmp
194194
#undef siglongjmp
195195

196-
#undef _SIZE_T_DEFINED
197-
198196
#define _DONT_USE_CTYPE_INLINE_
199197
#if HAVE_RUNETYPE_H
200198
#include <runetype.h>

src/coreclr/pal/src/include/pal/thread.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Module Name:
2424
#include "cs.hpp"
2525

2626
#include <pthread.h>
27-
#include <sys/syscall.h>
2827
#if HAVE_MACH_EXCEPTIONS
2928
#include <mach/mach.h>
3029
#endif // HAVE_MACH_EXCEPTIONS

src/coreclr/pal/src/loader/module.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,16 @@ LPCSTR FixLibCName(LPCSTR shortAsciiName)
497497
// As a result, we have to use the full name (i.e. lib.so.6) that is defined by LIBC_SO.
498498
// * For macOS, use constant value absolute path "/usr/lib/libc.dylib".
499499
// * For FreeBSD, use constant value "libc.so.7".
500+
// * For Haiku, use constant value "libroot.so".
500501
// * For rest of Unices, use constant value "libc.so".
501502
if (strcmp(shortAsciiName, LIBC_NAME_WITHOUT_EXTENSION) == 0)
502503
{
503504
#if defined(__APPLE__)
504505
return "/usr/lib/libc.dylib";
505506
#elif defined(__FreeBSD__)
506507
return "libc.so.7";
508+
#elif defined(__HAIKU__)
509+
return "libroot.so";
507510
#elif defined(LIBC_SO)
508511
return LIBC_SO;
509512
#else

src/coreclr/pal/src/map/map.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,7 +2110,7 @@ void * MAPMapPEFile(HANDLE hFile, off_t offset)
21102110
#endif
21112111
SIZE_T reserveSize = 0;
21122112
bool forceOveralign = false;
2113-
int readWriteFlags = MAP_FILE|MAP_PRIVATE|MAP_FIXED;
2113+
int readWriteFlags = MAP_PRIVATE|MAP_FIXED;
21142114
int readOnlyFlags = readWriteFlags;
21152115

21162116
ENTRY("MAPMapPEFile (hFile=%p offset=%zx)\n", hFile, offset);
@@ -2317,7 +2317,7 @@ void * MAPMapPEFile(HANDLE hFile, off_t offset)
23172317
// If PAL_MAP_READONLY_PE_HUGE_PAGE_AS_SHARED is set to 1. map the readonly sections as shared
23182318
// which works well with the behavior of the hugetlbfs
23192319
if (mapAsShared != NULL && (strcmp(mapAsShared, "1") == 0))
2320-
readOnlyFlags = MAP_FILE|MAP_SHARED|MAP_FIXED;
2320+
readOnlyFlags = MAP_SHARED|MAP_FIXED;
23212321
}
23222322

23232323
//we have now reserved memory (potentially we got rebased). Walk the PE sections and map each part

src/coreclr/pal/src/map/virtual.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ static LPVOID ReserveVirtualMemory(
594594
}
595595
#endif
596596

597+
#ifdef __HAIKU__
598+
mmapFlags |= MAP_NORESERVE;
599+
#endif
600+
597601
LPVOID pRetVal = mmap((LPVOID) StartBoundary,
598602
MemSize,
599603
PROT_NONE,

src/coreclr/pal/src/misc/cgroup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SET_DEFAULT_DEBUG_CHANNEL(MISC);
2323
#if defined(__APPLE__) || defined(__FreeBSD__)
2424
#include <sys/param.h>
2525
#include <sys/mount.h>
26-
#else
26+
#elif !defined(__HAIKU__)
2727
#include <sys/vfs.h>
2828
#endif
2929

src/coreclr/pal/src/misc/sysinfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ Revision History:
6565
#include <mach/mach_host.h>
6666
#endif // defined(TARGET_OSX)
6767

68+
#ifdef __HAIKU__
69+
#include <OS.h>
70+
#endif // __HAIKU__
71+
72+
#ifdef __FreeBSD__
6873
// On some platforms sys/user.h ends up defining _DEBUG; if so
6974
// remove the definition before including the header and put
7075
// back our definition afterwards
@@ -78,6 +83,7 @@ Revision History:
7883
#define _DEBUG OLD_DEBUG
7984
#undef OLD_DEBUG
8085
#endif
86+
#endif // __FreeBSD__
8187

8288
#include "pal/dbgmsg.h"
8389
#include "pal/process.h"
@@ -211,6 +217,8 @@ GetSystemInfo(
211217
lpSystemInfo->lpMaximumApplicationAddress = (PVOID) (1ull << 47);
212218
#elif defined(__sun)
213219
lpSystemInfo->lpMaximumApplicationAddress = (PVOID) 0xfffffd7fffe00000ul;
220+
#elif defined(__HAIKU__)
221+
lpSystemInfo->lpMaximumApplicationAddress = (PVOID) 0x7fffffe00000ul;
214222
#elif defined(USERLIMIT)
215223
lpSystemInfo->lpMaximumApplicationAddress = (PVOID) USERLIMIT;
216224
#elif defined(HOST_64BIT)

src/coreclr/pal/src/thread/context.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,22 @@ typedef int __ptrace_request;
5959
#endif // !HAVE_MACH_EXCEPTIONS
6060

6161
#ifdef HOST_AMD64
62+
#ifdef __HAIKU__
6263
#define ASSIGN_CONTROL_REGS \
63-
ASSIGN_REG(Rbp) \
64-
ASSIGN_REG(Rip) \
65-
ASSIGN_REG(SegCs) \
66-
ASSIGN_REG(EFlags) \
67-
ASSIGN_REG(Rsp) \
64+
ASSIGN_REG(Rbp) \
65+
ASSIGN_REG(Rip) \
66+
ASSIGN_REG(EFlags) \
67+
ASSIGN_REG(Rsp) \
68+
69+
#else // __HAIKU__
70+
#define ASSIGN_CONTROL_REGS \
71+
ASSIGN_REG(Rbp) \
72+
ASSIGN_REG(Rip) \
73+
ASSIGN_REG(SegCs) \
74+
ASSIGN_REG(EFlags) \
75+
ASSIGN_REG(Rsp) \
6876

77+
#endif // __HAIKU__
6978
#define ASSIGN_INTEGER_REGS \
7079
ASSIGN_REG(Rdi) \
7180
ASSIGN_REG(Rsi) \

src/coreclr/pal/src/thread/process.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ extern "C"
119119
#include <sys/user.h>
120120
#endif
121121

122+
#ifdef __HAIKU__
123+
#include <image.h>
124+
#include <OS.h>
125+
#endif
126+
122127
extern char *g_szCoreCLRPath;
123128
extern bool g_running_in_exe;
124129

@@ -1397,7 +1402,7 @@ PALIMPORT
13971402
VOID
13981403
PALAPI
13991404
PAL_SetCreateDumpCallback(
1400-
IN PCREATEDUMP_CALLBACK callback)
1405+
IN PCREATEDUMP_CALLBACK callback)
14011406
{
14021407
_ASSERTE(g_createdumpCallback == nullptr);
14031408
g_createdumpCallback = callback;
@@ -1680,6 +1685,23 @@ GetProcessIdDisambiguationKey(DWORD processId, UINT64 *disambiguationKey)
16801685

16811686
return TRUE;
16821687

1688+
#elif defined(__HAIKU__)
1689+
1690+
// On Haiku, we return the process start time expressed in microseconds since boot time.
1691+
1692+
team_info info;
1693+
1694+
if (get_team_info(processId, &info) == B_OK)
1695+
{
1696+
*disambiguationKey = info.start_time;
1697+
return TRUE;
1698+
}
1699+
else
1700+
{
1701+
WARN("Failed to get start time of a process.");
1702+
return FALSE;
1703+
}
1704+
16831705
#elif HAVE_PROCFS_STAT
16841706

16851707
// Here we read /proc/<pid>/stat file to get the start time for the process.

0 commit comments

Comments
 (0)