Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary support of NetBSD/amd64 #3835

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/user/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ $ pkg install sbt
$ pkg_add sbt
```

**NetBSD**

``` shell
$ pkg_add scala-sbt
```

## Installing clang and runtime dependencies

Scala Native requires Clang, which is part of the
Expand Down Expand Up @@ -118,6 +124,16 @@ architecture.
$ pkg_add boehm-gc # optional
```

**NetBSD 9.3 and later**

*Note 1:* NetBSD support is experimental and limited to only AMD64
architecture.

``` shell
$ pkg_add clang
$ pkg_add boehm-gc # optional
```

**Nix/NixOS**

``` shell
Expand Down
7 changes: 6 additions & 1 deletion javalib/src/main/resources/scala-native/net/if_dl.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#ifdef _WIN32
// NO Windows support
#elif defined(__linux__)
#elif defined(__linux__) || defined(__NetBSD__)
// Does not exist on Linux, so no check
// Does exist on NetBSD but it has defines:
// #define sdl_type sdl_addr.dl_type
// #define sdl_nlen sdl_addr.dl_nlen
// ...
// what requires to rewrite whole file from scratch
#else // macOS, FreeBSD, etc.

#if defined(__FreeBSD__) || defined(__OpenBSD__)
Expand Down
2 changes: 1 addition & 1 deletion javalib/src/main/resources/scala-native/time_nano.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ long long scalanative_nano_time() {
#else
#if defined(__FreeBSD__)
int clock = CLOCK_MONOTONIC_PRECISE; // OS has no CLOCK_MONOTONIC_RAW
#elif defined(__OpenBSD__)
#elif defined(__OpenBSD__) || defined(__NetBSD__)
int clock = CLOCK_MONOTONIC; // OpenBSD has only CLOCK_MONOTONIC
#else // Linux, macOS
int clock = CLOCK_MONOTONIC_RAW;
Expand Down
16 changes: 12 additions & 4 deletions javalib/src/main/scala/java/lang/impl/PosixThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,20 @@ private[java] class PosixThread(val thread: Thread, stackSize: Long)
threadPriority: Int,
schedulerPolicy: CInt
): Int = {

// min and max priority usually defines behavior for SCHED_FIFO or
// SCHED_RR. Other policies may ignore priority or require a special
// value such as 0 or some constant. Such a constant may be outside
// the valid range for priority. For example, NetBSD uses -1 for
// NONE priority, and the same -1 is returned on error. However, in
// the case of an error, these functions should also change errno.
// So, use modified errno as a flag.

errno = 0
val minPriority = sched_get_priority_min(schedulerPolicy)
val maxPriority = sched_get_priority_max(schedulerPolicy)
assert(
minPriority >= 0 && maxPriority >= 0,
"Failed to resolve priority range"
)
assert(errno == 0, "Failed to resolve priority range")

val priorityRange = maxPriority - minPriority
val javaPriorityRange = Thread.MAX_PRIORITY - Thread.MIN_PRIORITY
val priority =
Expand Down
5 changes: 3 additions & 2 deletions nativelib/src/main/resources/scala-native/delimcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
#if defined(__aarch64__) // ARM64
#define ASM_JMPBUF_SIZE 192
#define JMPBUF_STACK_POINTER_OFFSET (104 / 8)
#elif defined(__x86_64__) && (defined(__linux__) || defined(__APPLE__) || \
defined(__FreeBSD__) || defined(__OpenBSD__))
#elif defined(__x86_64__) && \
(defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
defined(__OpenBSD__) || defined(__NetBSD__))
#define ASM_JMPBUF_SIZE 72
#define JMPBUF_STACK_POINTER_OFFSET (16 / 8)
#elif defined(__i386__) && \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if defined(__x86_64__) && (defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__))
#if defined(__x86_64__) && (defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__))

/* ----------------------------------------------------------------------------
Copyright (c) 2016, Microsoft Research, Daan Leijen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ long long Time_current_nanos() {
#else
#if defined(__FreeBSD__)
int clock = CLOCK_MONOTONIC_PRECISE; // OS has no CLOCK_MONOTONIC_RAW
#elif defined(__OpenBSD__)
int clock = CLOCK_MONOTONIC; // OpenBSD has only CLOCK_MONOTONIC
#elif defined(__OpenBSD__) || defined(__NetBSD__)
int clock = CLOCK_MONOTONIC; // OpenBSD and NetBSD has only CLOCK_MONOTONIC
#else // Linux, macOS
int clock = CLOCK_MONOTONIC_RAW;
#endif // !FreeBSD || !OpenBSD
Expand Down
8 changes: 8 additions & 0 deletions nativelib/src/main/resources/scala-native/platform/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ int scalanative_platform_is_openbsd() {
#endif
}

int scalanative_platform_is_netbsd() {
#if defined(__NetBSD__)
return 1;
#else
return 0;
#endif
}

int scalanative_platform_is_linux() {
#ifdef __linux__
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ void scalanative_process_monitor_init() {
// on M1 chips) leading to deadlocks
char semaphoreName[SEM_MAX_LENGTH];

#if defined(__FreeBSD__)
#define SEM_NAME_PREFIX "/" // FreeBSD semaphore names must start with '/'
#if defined(__FreeBSD__) || defined(__NetBSD__)
#define SEM_NAME_PREFIX \
"/" // FreeBSD and NetBSD semaphore names must start with '/'
#else
#define SEM_NAME_PREFIX ""
#endif // __FreeBSD__
#endif // __FreeBSD__ || __NetBSD__

snprintf(semaphoreName, SEM_MAX_LENGTH,
SEM_NAME_PREFIX "__sn_%d-process-monitor", getpid());
Expand Down
26 changes: 26 additions & 0 deletions nativelib/src/main/resources/scala-native/platform/posix/unwind.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,52 @@
#include "../unwind.h"
#include "libunwind/libunwind.h"

// The unwinding on NetBSD is unstable, they don't provide CFI
// annotations for most of libc and other places, nor for the signal
// trampoline. So it can't work properly, and probably leads to
// segmentation errors. To minimize the impact, I allow to get context
// and initialize unwind, but cursor returns nothing.

int scalanative_unwind_get_context(void *context) {
#ifdef __NetBSD__
return 0;
#else
return unw_getcontext((unw_context_t *)context);
#endif
}

int scalanative_unwind_init_local(void *cursor, void *context) {
#ifdef __NetBSD__
return 0;
#else
return unw_init_local((unw_cursor_t *)cursor, (unw_context_t *)context);
#endif
}

int scalanative_unwind_step(void *cursor) {
#ifdef __NetBSD__
return 0;
#else
return unw_step((unw_cursor_t *)cursor);
#endif
}

int scalanative_unwind_get_proc_name(void *cursor, char *buffer, size_t length,
void *offset) {
#ifdef __NetBSD__
return UNW_EUNSPEC;
#else
return unw_get_proc_name((unw_cursor_t *)cursor, buffer, length,
(unw_word_t *)offset);
#endif
}

int scalanative_unwind_get_reg(void *cursor, int regnum, size_t *valp) {
#ifdef __NetBSD__
return UNW_EUNSPEC;
#else
return unw_get_reg((unw_cursor_t *)cursor, regnum, (unw_word_t *)valp);
#endif
}

int scalanative_unw_reg_ip() { return UNW_REG_IP; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ object LinktimeInfo {
@resolvedAtLinktime
def isOpenBSD: Boolean = target.os == "openbsd"

@resolvedAtLinktime
def isNetBSD: Boolean = target.os == "netbsd"

@resolvedAtLinktime("scala.scalanative.meta.linktimeinfo.is32BitPlatform")
def is32BitPlatform: Boolean = resolved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ object Platform {
@name("scalanative_platform_is_openbsd")
def isOpenBSD(): Boolean = extern

@name("scalanative_platform_is_netbsd")
def isNetBSD(): Boolean = extern

@name("scalanative_platform_is_linux")
def isLinux(): Boolean = extern

Expand Down
16 changes: 14 additions & 2 deletions posixlib/src/main/resources/scala-native/errno.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ int scalanative_enotdir() { return ENOTDIR; }

int scalanative_enotempty() { return ENOTEMPTY; }

int scalanative_enotrecoverable() { return ENOTRECOVERABLE; }
int scalanative_enotrecoverable() {
#ifdef ENOTRECOVERABLE
return ENOTRECOVERABLE;
#else
return 0;
#endif
}

int scalanative_enotsock() { return ENOTSOCK; }

Expand All @@ -166,7 +172,13 @@ int scalanative_eopnotsupp() { return EOPNOTSUPP; }

int scalanative_eoverflow() { return EOVERFLOW; }

int scalanative_eownerdead() { return EOWNERDEAD; }
int scalanative_eownerdead() {
#ifdef EOWNERDEAD
return EOWNERDEAD;
#else
return 0;
#endif
}

int scalanative_eperm() { return EPERM; }

Expand Down
6 changes: 4 additions & 2 deletions posixlib/src/main/resources/scala-native/netdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ _Static_assert(offsetof(struct scalanative_addrinfo, ai_addrlen) ==
offsetof(struct addrinfo, ai_addrlen),
"Unexpected offset: scalanative_addrinfo.ai_addrlen");

#if !(defined(__APPLE__) || defined(__FreeBSD__) || defined(_WIN32))
#if !(defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(_WIN32))
// Linux, etc.

_Static_assert(offsetof(struct scalanative_addrinfo, ai_addr) ==
Expand All @@ -70,7 +71,8 @@ _Static_assert(offsetof(struct scalanative_addrinfo, ai_canonname) ==
offsetof(struct addrinfo, ai_addr),
"Unexpected offset: BSD addrinfo ai_canonname fixup");

#endif // (defined(__APPLE__) || defined(__FreeBSD__) || defined(_WIN32))
#endif // (defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) ||
// defined(_WIN32))

_Static_assert(offsetof(struct scalanative_addrinfo, ai_next) ==
offsetof(struct addrinfo, ai_next),
Expand Down
6 changes: 3 additions & 3 deletions posixlib/src/main/resources/scala-native/sys/times.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// 2023-01-03 FIXME -- need to fuss with timesOps in times.scala
// 2023-01-03 FIXME -- need to explain here the useful lie.

#if !defined(__FreeBSD__)
#if !defined(__FreeBSD__) && !defined(__NetBSD__)
// C long will mirror machine architecture: 64 bits or 32 bit.
typedef long scalanative_clock_t;
#else // __FreeBSD
Expand All @@ -26,10 +26,10 @@ typedef long scalanative_clock_t;
*/
#import <sys/types.h>
typedef __int32_t scalanative_clock_t;
#endif // __FreeBSD__
#endif // __FreeBSD__ || __NetBSD__

struct scalanative_tms {
scalanative_clock_t tms_utime; // User CPU time
scalanative_clock_t tms_utime; // User CPU time
scalanative_clock_t tms_stime; // System CPU time
scalanative_clock_t tms_cutime; // User CPU time terminated children
scalanative_clock_t tms_cstime; // System CPU time of terminated children
Expand Down
14 changes: 11 additions & 3 deletions posixlib/src/main/resources/scala-native/termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#define VTDLY VTDELAY
#endif

#if defined(__OpenBSD__)
// OpenBSD has missed some constatn, use 0 instead
#if defined(__OpenBSD__) || defined(__NetBSD__)
// OpenBSD and NetBSD has missed some constatn, use 0 instead
#define NLDLY 0
#define CRDLY 0
#define BSDLY 0
Expand All @@ -28,7 +28,15 @@
#define NL1 0
#define TAB1 0
#define TAB2 0
#endif

// NetBSD requires a few more
#ifdef __NetBSD__
#define TABDLY 0
#define TAB0 0
#define TAB3 0
#endif // NetBSD

#endif // OpenBSD || NetBSD

// symbolic constants for use as subscripts for the array c_cc

Expand Down