From ab6e9b6d889ecf73e33c21317e8fd2cf7e9177ef Mon Sep 17 00:00:00 2001 From: Shoji Tokunaga Date: Tue, 26 May 2026 22:10:39 +0900 Subject: [PATCH] arch/sim: Translate Apple clock IDs for macOS sim On the macOS simulator, that Darwin clock ID can reach NuttX clock_gettime(), where it is outside the NuttX clockid_t namespace and causes EINVAL. Add an optional architecture hook to translate clock IDs at clock_gettime() boundary. Use it for macOS sim to translate non-conflicting Apple monotonic clock IDs to NuttX CLOCK_MONOTONIC. Signed-off-by: Shoji Tokunaga --- arch/Kconfig | 1 + arch/sim/src/Makefile | 3 ++ arch/sim/src/sim/CMakeLists.txt | 4 ++ arch/sim/src/sim/posix/sim_clockid.c | 69 ++++++++++++++++++++++++++++ include/nuttx/arch.h | 13 ++++++ sched/Kconfig | 4 ++ sched/clock/clock_gettime.c | 4 ++ 7 files changed, 98 insertions(+) create mode 100644 arch/sim/src/sim/posix/sim_clockid.c diff --git a/arch/Kconfig b/arch/Kconfig index 7cd6ec4f10d41..6c8b107c92f6a 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -104,6 +104,7 @@ config ARCH_RISCV config ARCH_SIM bool "Simulation" select ARCH_HAVE_BACKTRACE + select ARCH_HAVE_CLOCKID_TRANSLATE if HOST_MACOS select ARCH_HAVE_MULTICPU if !CONFIG_WINDOWS_NATIVE select ARCH_HAVE_RTC_SUBSECONDS select ARCH_HAVE_SERIAL_TERMIOS diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index d3857e52025c9..9610ff36a3d03 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -103,6 +103,9 @@ ifeq ($(CONFIG_WINDOWS_NATIVE),y) VPATH += :sim/win else VPATH += :sim/posix +ifeq ($(CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE),y) + CSRCS += sim_clockid.c +endif endif DEPPATH = $(patsubst %,--dep-path %,$(subst :, ,$(VPATH))) diff --git a/arch/sim/src/sim/CMakeLists.txt b/arch/sim/src/sim/CMakeLists.txt index 42c569f145e9b..9289658775ad2 100644 --- a/arch/sim/src/sim/CMakeLists.txt +++ b/arch/sim/src/sim/CMakeLists.txt @@ -150,6 +150,10 @@ list( sim_hosttime.c sim_hostuart.c) +if(CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE) + list(APPEND SRCS posix/sim_clockid.c) +endif() + if(CONFIG_HOST_MACOS AND CONFIG_HAVE_CXXINITIALIZE) # Keep classic __mod_init_func format so post-link lief patching works target_link_options(nuttx PRIVATE -Wl,-ld_classic,-no_fixup_chains) diff --git a/arch/sim/src/sim/posix/sim_clockid.c b/arch/sim/src/sim/posix/sim_clockid.c new file mode 100644 index 0000000000000..4479acfb7db16 --- /dev/null +++ b/arch/sim/src/sim/posix/sim_clockid.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_clockid.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Rust std uses CLOCK_UPTIME_RAW for Instant on Apple targets. That Darwin + * clock ID is not part of the NuttX clockid_t namespace, so translate it at + * the sim/posix boundary instead of teaching the common clock_gettime() + * implementation about host ABI values. + * + * Do not translate Darwin CLOCK_MONOTONIC_RAW (4) or + * CLOCK_MONOTONIC_RAW_APPROX (5): those values overlap with NuttX + * CLOCK_BOOTTIME and dynamic clock IDs. + */ + +#define DARWIN_CLOCK_MONOTONIC 6 +#define DARWIN_CLOCK_UPTIME_RAW 8 +#define DARWIN_CLOCK_UPTIME_RAW_APPROX 9 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_translate_clockid + ****************************************************************************/ + +clockid_t up_translate_clockid(clockid_t clockid) +{ + switch (clockid) + { + case DARWIN_CLOCK_MONOTONIC: + case DARWIN_CLOCK_UPTIME_RAW: + case DARWIN_CLOCK_UPTIME_RAW_APPROX: + return CLOCK_MONOTONIC; + + default: + return clockid; + } +} diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index e1a598c222b2d..0bc38e2a0aa90 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -1999,6 +1999,19 @@ int up_timer_gettime(FAR struct timespec *ts); int up_timer_gettick(FAR clock_t *ticks); void up_timer_getmask(FAR clock_t *mask); +/**************************************************************************** + * Name: up_translate_clockid + * + * Description: + * Translate architecture-specific clock IDs before the common clock layer + * handles them. + * + ****************************************************************************/ + +#ifdef CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE +clockid_t up_translate_clockid(clockid_t clockid); +#endif + /**************************************************************************** * Name: up_alarm_cancel * diff --git a/sched/Kconfig b/sched/Kconfig index a2c6fbff12f9c..1f0c8c521fb1f 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -215,6 +215,10 @@ config ARCH_HAVE_TIMEKEEPING bool default n +config ARCH_HAVE_CLOCKID_TRANSLATE + bool + default n + config CLOCK_TIMEKEEPING bool "Support timekeeping algorithms" default n diff --git a/sched/clock/clock_gettime.c b/sched/clock/clock_gettime.c index 7d5b0566090d8..2e6f3daca8a1b 100644 --- a/sched/clock/clock_gettime.c +++ b/sched/clock/clock_gettime.c @@ -219,6 +219,10 @@ int clock_gettime(clockid_t clock_id, FAR struct timespec *tp) { int ret; +#ifdef CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE + clock_id = up_translate_clockid(clock_id); +#endif + ret = nxclock_gettime(clock_id, tp); if (ret < 0) {