700 Commits

Author SHA1 Message Date
dongjiuzhu1
3c486b98dd fs/timerfd: implement TFD_TIMER_CANCEL_ON_SET to detect clock changes
Implement Linux-compatible TFD_TIMER_CANCEL_ON_SET flag for timerfd to
allow applications to detect discontinuous changes to CLOCK_REALTIME.

Background:
According to Linux timerfd_create(2) man page, when a timerfd is created
with CLOCK_REALTIME and TFD_TIMER_CANCEL_ON_SET flag is specified, the
read() operation should fail with ECANCELED if the real-time clock
undergoes a discontinuous change. This allows applications to detect
and respond to system time changes.

Implementation:
1. Add clock notifier infrastructure (clock_notifier.h/c) to notify
   interested parties when system time changes
2. Implement TFD_TIMER_CANCEL_ON_SET flag support in timerfd
3. Register timerfd with clock notifier when flag is set
4. Cancel timer and return ECANCELED when clock change is detected
5. Notify clock changes in:
   - clock_settime()
   - clock_initialize()
   - clock_timekeeping_set_wall_time()
   - rpmsg_rtc time synchronization

Changes include:
- New files: include/nuttx/clock_notifier.h, sched/clock/clock_notifier.c
- Modified: fs/vfs/fs_timerfd.c to handle TFD_TIMER_CANCEL_ON_SET
- Modified: clock subsystem to call notifier chain on time changes
- Modified: rpmsg_rtc to notify time changes during sync

Use case example:
Applications using timerfd with absolute time can now detect when
system time is adjusted (e.g., NTP sync, manual time change) and
take appropriate action such as recalculating timeouts or updating
scheduled events.

Reference: https://man7.org/linux/man-pages/man2/timerfd_create.2.html

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2026-01-30 17:20:24 +08:00
yukangzhi
2f9d16372c syscall: add sched_note_event_ip syscall for instrumentation dump
Export the sched_note_event_ip function as a syscall under
CONFIG_SCHED_INSTRUMENTATION_DUMP. This allows user-space or
other kernel components to trigger event-type logs via the
instrumentation dump mechanism.

Signed-off-by: yukangzhi <yukangzhi@xiaomi.com>
2026-01-28 02:06:23 +08:00
Gao Jiawei
564c9d7dcd arm: systick: fix off-by-one in SysTick RELOAD programming
ARM specifies that SysTick RELOAD must be programmed with N-1 to get a period of N CPU cycles.
Adjust the timeout/reload conversions accordingly: subtract 1 when writing RELOAD and add 1 back when converting RELOAD to a timeout/interval.
Also clamp RELOAD to the valid 24-bit range (1..0x00ffffff) for Armv7-M/Armv8-M to avoid invalid values.
Reference: https://developer.arm.com/documentation/dui0646/c/Cortex-M7-Peripherals/System-timer--SysTick/SysTick-Reload-Value-Register

Signed-off-by: Gao Jiawei <gaojiawei@xiaomi.com>
2026-01-26 19:34:00 +08:00
hujun5
217e685beb pthread: move pthread_cond to userspace
Move pthread condition variable implementation from kernel (sched/pthread)
to userspace library (libs/libc/pthread). This allows userspace to handle
condition variable operations directly, reducing syscall overhead and
improving performance for pthread applications.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-26 16:26:39 +08:00
yukangzhi
1b22f8c65b driver/note: Fix compilation error when CONFIG_SYSLOG_TO_SCHED_NOTE=y
Add compilation condition for 'nx_vsyslog' syscall.
Export 'sched_note_printf_ip' syscall when CONFIG_SYSLOG_TO_SCHED_NOTE=y
Put the implementation of sched_note_printf_ip in libc/misc/lib_note.c

Signed-off-by: yukangzhi <yukangzhi@xiaomi.com>
2026-01-23 07:48:08 -03:00
hujun5
5051721298 sched/pthread: move pthread mutex from syscall to user-space
Move pthread mutex operations from kernel-space syscall
interface to user-space implementations
to reduce syscall overhead. Relocate mutex holder list
tracking from task control block (tcb) to
thread local storage (tls) to improve memory layout and
cache efficiency. Add helper macros for
conditional mutex implementations and update syscall
interface accordingly.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-22 12:40:49 -03:00
daichuan
763caabf6d boards/boardctl: Add BOARDIOC_MACADDR command
Add a new boardctl command BOARDIOC_MACADDR to retrieve the MAC address of the network interface.

The board_macaddr function needs to be implemented by the board logic.

Signed-off-by: daichuan <daichuan@xiaomi.com>
2026-01-22 22:13:31 +08:00
hujun5
8bb6758490 sched: move process ID from kernel to TLS for faster access
Add pid field to task_info_s and move getpid() implementation to user
space TLS access. Remove getpid from syscall interface as it now returns
cached PID from thread local storage instead of kernel lookup.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-20 22:33:45 +08:00
wangchengdong
faf864b04f sched/signal: Add support for disabling all signal functions
Signals in NuttX serve two primary purposes:

      1. Synchronization and wake-up:
        Signals can be used to block threads on specific signal sets and later
        wake them up by delivering the corresponding signals to those threads.

      2. Asynchronous notification:
        Signals can also be used to install callback handlers for specific signals, allowing threads to
        asynchronously invoke those handlers when the signals are delivered.

    This change introduces the ability to  disable all signal functionality to reduce footprint for NuttX.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2026-01-18 08:24:13 -03:00
wangchengdong
0ea686bc5b sched/signal: Add support for partially disabling signals
Signals in NuttX serve two primary purposes:

    1. Synchronization and wake-up:
    Signals can be used to block threads on specific signal sets and later
    wake them up by delivering the corresponding signals to those threads.

    2. Asynchronous notification:
    Signals can also be used to install callback handlers for specific signals, allowing threads to
    asynchronously invoke those handlers when the signals are delivered.

This change introduces the ability to partially disable signal functionality: to disable only signal functions for
Asynchronous notification, keeping functions for Synchronization and wake-up.
This enables finer-grained control over signal usage while preserving existing behavior for supported use cases.

Co-authored-by: Guo Shichao guoshichao@xiaomi.com
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2026-01-18 08:24:13 -03:00
guoshichao
57637e5927 net: make the sendmsg param type consistent with posix specification
To ensure consistency, in all places where the "sendmsg" function is used
either directly or indirectly, the type of the "struct msghdr *msg" parameter
needs to be modified to "const struct msghdr *msg".

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-16 21:11:39 +08:00
guoshichao
54ff28a47f sys/types.h: Fixed the conflict issue with the "OK" constant definition
When cross-compiling PSE52 VSX testcases based on Vela, VSX defines its
own "OK" constant as a macro, which conflicts with the "OK" constant
defined as an enum in <sys/types.h>. To ensure compatibility, we
have undef'd the macro version of "OK" in advance.

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-16 16:16:21 +08:00
guoshichao
d78d94e1fa pthread: reorganized the locations of pthread-related functions and constants
1. According to the POSIX specification, the functions `pthread_kill`
and `pthread_sigmask` have been moved from `pthread.h` to `signal.h`.
2. As required by the POSIX standard, the pthread-related constants
have been relocated from `pthread.h` to `sys/types.h`.

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-15 16:23:04 -03:00
guoshichao
2662473b83 sys/resouce: add RLIM_NLIMITS definition
Add the RLIM_NLIMITS macro with value 16 to sys/resource.h, indicating
the total number of supported resource limit types. This improves
POSIX header completeness and aids resource enumeration in applications.

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-15 16:08:23 -03:00
guoshichao
ebd4173a95 sys/wait: add WCOREDUMP defintion
Add the WCOREDUMP() macro to sys/wait.h to indicate whether a terminated
process produced a core dump. This resolves implicit declaration warnings
in LTP kernel testcases and improves POSIX compliance.

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-15 23:16:27 +08:00
guoshichao
d8717aab78 sys/wait: add WTERMSIG implementation
Update the WTERMSIG() macro to extract the signal number from the wait
status, changing from constant 'false' to '(((s) >> 8) & 0x7f)' to match
POSIX semantics for processes terminated by signals.

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-15 23:16:27 +08:00
guoshichao
ba8abb5643 sys/ioctl: extend the IOCTL defintion that compatible with linux kernel
Add the generic _IO() macro and aliases for FS_IOC_GETFLAGS and FS_IOC_SETFLAGS,
aligning the ioctl.h definitions with the Linux kernel API to improve source
compatibility when porting LTP linux kernel testcase code to NuttX

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-14 09:39:51 +08:00
dongjiuzhu1
778e16a555 syscall/adjtime: fix minior issue about adjtime and add clock_adjtime to syscall
add new syscall clock_adjtime

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
dongjiuzhu1
752a405f86 sched/clock: support using CLOCKFD to call clock_adjtime
This patch implements clock_adjtime() with CLOCKFD support, enabling
precise time and frequency adjustments for PTP clocks through the
standard POSIX clock API.

Key changes include:

1. New clock_adjtime() system call:
   - Added sched/clock/clock_adjtime.c implementation
   - Provides POSIX-compliant time adjustment interface
   - Supports both standard clocks and CLOCKFD-based dynamic clocks
   - Added CONFIG_CLOCK_ADJTIME Kconfig option

2. CLOCKFD support in clock_adjtime():
   - Detects CLOCKFD-encoded clockids via CLOCKFD_TO_FD() check
   - Extracts file descriptor and validates through fs_getfilep()
   - Issues PTP_CLOCK_ADJTIME ioctl to underlying PTP clock device
   - Returns clock status and adjustment results via struct timex

3. Enhanced struct timex support:
   - Extended include/sys/timex.h with additional ADJ_* flags
   - Added ADJ_OFFSET, ADJ_FREQUENCY, ADJ_MAXERROR, ADJ_ESTERROR
   - Added ADJ_STATUS, ADJ_TIMECONST, ADJ_TAI, ADJ_SETOFFSET
   - Added ADJ_MICRO, ADJ_NANO for time unit selection
   - Added STA_* status flags for clock state reporting

4. Clock operations supported:
   - ADJ_SETOFFSET: Apply time offset adjustment
   - ADJ_FREQUENCY: Adjust clock frequency (PPM)
   - ADJ_MAXERROR: Set maximum error estimate
   - ADJ_ESTERROR: Set estimated error
   - ADJ_STATUS: Modify clock status bits
   - ADJ_NANO: Use nanosecond resolution
   - ADJ_SETOFFSET: Set absolute time offset

5. API declarations:
   - Added clock_adjtime() prototype in include/nuttx/clock.h
   - Enabled by CONFIG_CLOCK_ADJTIME configuration
   - Compatible with Linux clock_adjtime() interface

Usage example:
  int fd = open("/dev/ptp0", O_RDWR);
  struct timex tx = {0};
  tx.modes = ADJ_FREQUENCY;
  tx.freq = 10000000;  /* Adjust frequency by 10 PPM */
  clock_adjtime(CLOCKFD(fd), &tx);
  close(fd);

This completes the PTP clock framework's POSIX clock API integration,
providing comprehensive time control capabilities for precision timing
applications including PTP synchronization daemons (ptp4l, timemaster).

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
dongjiuzhu1
31eb339356 drivers/timers/ptp: support ptp clock driver model
This patch introduces the foundational PTP (Precision Time Protocol) clock
driver framework for NuttX, enabling precise time synchronization support
based on IEEE 1588 standard.

Key changes include:

1. New PTP clock driver infrastructure:
   - Added drivers/timers/ptp_clock.c implementing upper-half driver
   - Created include/nuttx/timers/ptp_clock.h with PTP clock interfaces
   - Implemented upper/lower half driver architecture for hardware abstraction

2. Core functionality:
   - Clock time get/set operations (gettime, settime)
   - Frequency adjustment support (adjtime, adjfine)
   - Phase adjustment capabilities
   - System-device cross-timestamping for precise synchronization

3. IOCTL commands:
   - PTP_CLOCK_SETTIME/GETTIME for time manipulation
   - PTP_CLOCK_GETRES for resolution queries
   - PTP_CLOCK_ADJTIME for time adjustment
   - PTP_CLOCK_GETCAPS for capability queries
   - PTP_SYS_OFFSET* for system offset measurements

4. Supporting structures:
   - struct ptp_lowerhalf_s: lower-half driver interface
   - struct ptp_clock_caps: clock capabilities descriptor
   - struct ptp_sys_offset: system time offset measurement
   - Added timex structures in include/sys/timex.h for ADJ_* operations

5. Build system integration:
   - Added CONFIG_PTP_CLOCK Kconfig option
   - Updated CMakeLists.txt and Make.defs
   - Added PTPCLK debug macros in include/debug.h

This framework provides the base for PTP clock implementations, allowing
hardware-specific drivers to register and provide precise time services
through a standardized interface.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
gaohedong
81f42cb2cf net/ethernet: add timestamp for socket packet
add timestamp for socket packet

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2025-12-10 23:44:14 +08:00
zhanghongyu
f7526522a1 icmp/icmpv6: fix the level parameter type
use a more standard definition

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-06 17:55:40 +08:00
Karel Kočí
b9066e42bb include: add BSD rounding macros to sys/param.h
These macros are commonly provided on BSD systems. They are not part of
C nor POSIX but these types of operations can be required in drivers
implementation as well as in user space. The operations themself are
simple but kind of cryptic when placed in the code as is. Thus having
macro with an appropriate name is beneficial.

The use of BSD naming here is only to cling to at least some other
implementation instead of creating completelly NuttX specific one.

Kudos to @hartmannathan for the source code documentation.

Signed-off-by: Karel Kočí <kkoci@elektroline.cz>
2025-11-24 14:11:41 +08:00
nuttxs
7470fd9274 sys/socket.h: Since ss_family is of type uint16_t (2-byte alignment),
packing the internal substructures will reduce the overall alignment of
`sockaddr_storage` to 2, which triggers an unaligned access warning
in 32-bit systems.
Add an overall 8-byte alignment to the outermost layer of the structure.

Related commit: 855336bf
Refer to https://datatracker.ietf.org/doc/html/rfc2553#section-3.10

Signed-off-by: nuttxs <zhaoqing.zhang@sony.com>
2025-08-15 09:59:19 +08:00
jingfei
c3e87dd3d1 drivers/fs: Control the behavior of FTL by passing oflags during the open process.
To save more space (equivalent to the size of one erase sector of
MTD device) and to achieve faster read and write speeds, a method
for direct writing was introduced at the FTL layer.
This can be accomplished simply by using the following oflags during
the open operation:

1. O_DIRECT. when this flag is passed in, ftl internally uses
   the direct write strategy and no read cache is used in ftl;
   otherwise, each write will be executed with the minimum
   granularity of flash erase sector size which means a
   "sector read back - erase sector - write sector" operation
   is performed by using a read cache buffer in heap.

2. O_SYNC. When this flag is passed in, we assume that the
   flash has been erased in advance and no erasure operation
   will be performed internally within ftl. O_SYNC will take
   effect only when both O_DIRECT and O_SYNC are passed in
   simultaneously.

3. For uniformity, we remapped the mount flag in mount.h and
   unified it with the open flag in fcntl.h. The repetitive
   parts of their definitions were reused, and the remaining
   part of the mount flag redefine to the unused bit of open
   flags.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2025-07-16 14:11:41 +08:00
dongjiuzhu1
4f57ebce66 fs/poll: remove poll_fdsetup
Some checks failed
Build Documentation / build-html (push) Has been cancelled
Allow users to operate poll in the kernel using the file_poll
approach, as file is protected with reference counting,
making it more secure.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-05-26 18:49:45 +08:00
Lars Kruse
3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
Ville Juven
04e760b1c2 sched/gettid: Move thread ID to TLS
There is no need for a gettid() syscall, as the thread ID is stable through
the life of the process. It is safe to put a copy of TID to TLS. This way
a user processes can access TID quickly via its own stack, instead of
having to use an expensive syscall.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
2025-04-08 22:53:10 +08:00
chao an
58ed9934c2 syscall/csv: add nxsem_tickwait() to userspace
mutex_tickwait() depends on this API

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-04-03 20:13:55 +08:00
Jukka Laitinen
19a8e2403f libc/semaphore: Move fast mutex wait/post paths to libc
This avoids unnecessary syscalls in memory protected builds, when mutex
lock/unlock can be done with only atomic counter access

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-04-01 20:37:23 +08:00
buxiasen
7a94a4553d syscall: add missing epoll API
Add missing epoll syscall API for kernelbuild
epoll_create, epoll_pwait, epoll_close

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2025-01-24 01:30:02 +08:00
guoshichao
a58d6552e6 fcntl: add sys/fcntl.h header file
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
guoshichao
1701ca1e11 sem: add sys/sem.h header file
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
guoshichao
0af01312fd sys/socket: add SOL_PACKET packet operation type definition
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
guoshichao
2badabfb67 signal: add include/sys/signal.h to the signal
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
guoshichao
8fcdae3544 sys/types.h: add loff_t data type definition
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
guoshichao
092f4a1b79 sys/mount: add mount flag definition
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
guoshichao
a8cdd63d56 proctl: add PR_SET_DUMPABLE support
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
guoshichao
23e0b70290 sys/resource: add RLIMIT_NICE consntant
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-12-20 11:46:14 -03:00
hujun5
dcc75048be Revert "system: pthread_barrierwait should be moved to kernel space"
reason:
new implementation does not requires the use of enter_critical_section,
so the source code needs to be moved to user space

This reverts commit d189a86a35.
2024-11-15 22:07:25 +08:00
Xiang Xiao
c5381c3eb2 Replace __attribute__((packed)) with [begin|end]_packed_struct
since the different compiler express the packed structure in the different way
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2024-11-03 10:20:38 +08:00
nuttxs
3818ce8268 sys/socket: use __attribute__((packed)) to avoid changes in
the size of struct sockaddr_storage due to struct alignment
2024-11-01 11:15:54 -03:00
ouyangxiangzhen
17c51c0667 userspace: Exclude nuttx/arch.h
This patch fixed userspace headers conflict. Architecture-related definition and API should not be exposed to users.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2024-11-01 16:59:37 +08:00
YAMAMOTO Takashi
761ee81956 move readv/writev to the kernel
currently, nuttx implements readv/writev on the top of read/write.
while it might work for the simplest cases, it's broken by design.
for example, it's impossible to make it work correctly for files
which need to preserve data boundaries without allocating a single
contiguous buffer. (udp socket, some character devices, etc)

this change is a start of the migration to a better design.
that is, implement read/write on the top of readv/writev.

to avoid a single huge change, following things will NOT be done in
this commit:

* fix actual bugs caused by the original readv-based-on-read design.
  (cf. https://github.com/apache/nuttx/pull/12674)

* adapt filesystems/drivers to actually benefit from the new interface.
  (except a few trivial examples)

* eventually retire the old interface.

* retire read/write syscalls. implement them in libc instead.

* pread/pwrite/preadv/pwritev (except the introduction of struct uio,
  which is a preparation to back these variations with the new
  interface.)
2024-10-30 17:07:54 +08:00
Xiang Xiao
32784b0898 libc: Refine the arc4random_buf implementation
fill the buffer with getrandom instead random pool
and move the implementation to from crypto to libc

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2024-10-26 18:04:21 -03:00
Bowen Wang
b5b0430c39 net/vsock: add AF_VSOCK address family
Add AF_VSOCK address family support, This is the vsock framework
preparation patch.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-13 02:02:03 +08:00
makejian
e8a890ef42 nuttx/syscall: export nxsem_getprioceiling and nxsem_setprioceiling via syscall
Signed-off-by: makejian <makejian@xiaomi.com>
2024-10-12 09:50:54 +08:00
yinshengkai
211a56910a syslog: support syslog redirection to sched_note
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-11 01:30:11 +08:00
yinshengkai
d375a09c0a libs: add gprof arm64 support
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-10 18:46:51 +08:00
yinshengkai
eb8449cb0c sched/gprof: add gprof support
gprof can analyze code hot spots based on scheduled sampling.
After adding the "-pg" parameter when compiling, you can view the code call graph.

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-10 18:46:51 +08:00