Commit graph

3045 commits

Author SHA1 Message Date
hujun5
92bbe4f6ac sched/tls: fix uninitialized argv pointer in task info
The TCB initializes the pid early, but the argvstack is not initialized
at that time. This may cause invalid addresses to be obtained when
nxsched_get_stackargs is called during task enumeration.
Initialize the argv pointer to NULL to enable safe validity checks.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2026-01-15 16:10:11 -03:00
zhangyu117
909e63b63b arch/tricore: upcsa/lowcsa process && dumpinfo
tricore csa is not continuous. when assert prints information, we need to handle the regs specially in order to dump all the registers.

Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
2026-01-15 16:09:13 -03:00
wangchengdong
3e9190396e sched/hrtimer: fix Kconfig typo
Remove the duplicated dependency on SYSTEM_TIME64.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-15 12:25:54 +01:00
wangchengdong
c025cd96b4 sched/sched: Improve sched make and cmake scripts
Improve sched make and cmake scripts to select sched_processtimer.c
  and sched_timerexpiration.c separatly for tickless and non-tickless mode

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-14 16:59:39 -08:00
guoshichao
f47ff76589 signal: add default sigaction for SIGURG
according to the posix standard: the default action for SIGURG is to
ignore the signal, we need to add the default sigaction handler for
SIGURG
this fix the /tset/ANSI.os/signal/signal_X/T.signal_X testcase

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-13 13:56:39 -08:00
ouyangxiangzhen
946bd659d8 sched/sched: Remove nxsched_tick_expiration.
This commit removed nxsched_tick_expiration to simplify the scheduler.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-13 21:41:35 +08:00
ouyangxiangzhen
a518c28add sched/hrtimer: Add callback pointer in hrtimer_start.
The hrtimer is designed to replace the wdog timer in future, so it
should have the callback function in its API. The evaluation results showed there is no any
performance degradation since the CPU pipeline can hide the latency.
This commit also decoupled the rb-tree implementation with the pending
state checking.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-13 21:41:35 +08:00
wangchengdong
51bcec53c4 nuttx/sched: rename nxsched_timer_expiration
rename nxsched_timer_expiration to nxsched_tick_expiration
    to align with nxsched_process_tick()

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-12 16:20:42 +08:00
wangchengdong
9f44bff3cc sched/sched:merge tickless and tick process together
Merge tickless and tick process into  nxsched_process_timer
    to improve code level efficient

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-12 16:20:42 +08:00
ouyangxiangzhen
4c1228059f sched/sched: Fix comments in sched_timerexpiration.
This commit fixed comments in sched_timerexpiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-10 17:30:25 +08:00
ouyangxiangzhen
c69cf36b7c sched/wdog: Simplify the wdog expiration handling.
This commit simplified the wdog expiration handling by introducing the
`g_sched_event` timer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-10 17:30:25 +08:00
ouyangxiangzhen
7b47b9de17 sched/wdog: Simplify the wd_gettime.
This commit simplified the wd_gettime.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-10 17:30:25 +08:00
wangchengdong
96e8fce4de [EXPERIMENTAL] sched/hrtimer: add SYSTEM_TIME64 dependency
The hrtimer infrastructure only works with 64-bit system timers.
  Add a dependency on SYSTEM_TIME64 in the hrtimer Kconfig.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-08 22:47:16 +08:00
wangchengdong
6bffad62de [EXPERIMENTAL] sched/hrtimer: separate SMP logic from hrtimer
Separate SMP-specific logic from the hrtimer core to improve
    performance and maintainability.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-08 22:47:16 +08:00
ouyangxiangzhen
254df1721a sched/hrtimer: Fix functional correctness issue.
This commit added dependency on CONFIG_SYSTEM_TIME64 for the hrtimer, sin
ce the hrtimer use `clock_compare` to compare 64-bit time.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-08 22:26:11 +08:00
ouyangxiangzhen
6bd9ef6992 sched/hrtimer: Rename the callback type to hrtimer_entry_t.
This commit renamed the callback type to `hrtimer_entry_t`, aligning with
the `wdentry_t` in wdog..

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-08 22:26:11 +08:00
ouyangxiangzhen
8db732afb1 sched/hrtimer: Fix functional correctness issue.
The expired field can not be used to indicate the cancelled state. Because
the `UINT64_MAX` is valid expired time. A periodic hrtimer started with
the hrtimer_start_absolute(hrtimer, period_func, UINT64_MAX) can
not be cancelled via `hrtimer_cancel`.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-08 22:26:11 +08:00
ouyangxiangzhen
3fa62118f1 sched/hrtimer: Fix hrtimer compilation.
This commit fixed hrtimer compilation issue.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-08 22:26:11 +08:00
Yongrong Wang
eac3cea465 sched/sem_rw.c: Add downgrade_write API for sem_rw
support downgrad a write lock to a read lock

Signed-off-by: Yongrong Wang <wangyongrong@xiaomi.com>
2026-01-07 22:47:09 +08:00
wangchengdong
1020bc74e8 [EXPERIMENTAL]sched/hrtimr: Allow running/armed hrtimer to be restarted
Allow running/armed hrtimer to be restarted to
  fix hrtimer bug: #17567

Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-06 23:14:04 +08:00
wangzhi16
ea180792a3 sched/task: fix Redundant memory allocation.
Since the task and group structures are no longer allocated in the same space, the memory for group is allocated in the group_allocate() function, and no further allocation is needed during the task allocation process.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2026-01-01 07:25:04 -03:00
guohao15
0625b7a760 sched/clock: [bugfix] should return -EINVAL when clock_id is invalid
bug fix about clock_gettime

Signed-off-by: guohao15 <guohao15@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
6802d3510b sched/clock: support using CLOCKFD to call clock_getres
This patch extends CLOCKFD support to clock_getres(), allowing
applications to query the resolution of PTP clocks through file
descriptors using the standard POSIX clock API.

Key changes include:

1. Move clock_getres() from libc to kernel:
   - Relocated from libs/libc/sched/clock_getres.c to sched/clock/
   - Enables direct kernel-level file descriptor handling
   - Maintains POSIX compliance while adding CLOCKFD support

2. CLOCKFD support in clock_getres():
   - Detects CLOCKFD-encoded clockids using CLOCKFD_TO_FD() check
   - Extracts file descriptor from clockid parameter
   - Validates file descriptor through fs_getfilep()
   - Issues PTP_CLOCK_GETRES ioctl to query clock resolution

3. PTP clock resolution query:
   - Retrieves resolution from PTP clock device via ioctl
   - Returns nanosecond-precision timing resolution
   - Enables applications to determine clock accuracy capabilities
   - Falls back to standard clock resolution for non-CLOCKFD clockids

4. Error handling:
   - Returns EINVAL for invalid CLOCKFD file descriptors
   - Properly manages file reference counting with fs_putfilep()
   - Maintains backward compatibility with existing clock types

5. Build system updates:
   - Updated libs/libc/sched/CMakeLists.txt and Make.defs
   - Updated sched/clock/CMakeLists.txt to include clock_getres.c
   - Ensures proper compilation in kernel context

Usage example:
  int fd = open("/dev/ptp0", O_RDONLY);
  struct timespec res;
  clock_getres(CLOCKFD(fd), &res);  /* Query PTP clock resolution */
  printf("Resolution: %ld.%09ld sec\n", res.tv_sec, res.tv_nsec);
  close(fd);

This completes the basic POSIX clock API support for dynamic PTP clocks,
providing applications with comprehensive clock information access.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
dongjiuzhu1
1c643b5cc3 sched/clock: support using CLOCKFD to call clock_gettime/settime
This patch implements POSIX-compliant CLOCKFD support, enabling user
applications to access dynamic PTP clocks through file descriptors
combined with clock_gettime() and clock_settime() system calls.

Key changes include:

1. CLOCKFD macro support:
   - Added CLOCKFD() macro in include/nuttx/clock.h
   - Allows encoding file descriptor into clockid_t: CLOCKFD(fd)
   - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts)

2. clock_gettime() enhancements:
   - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids
   - Extracts file descriptor from clockid using CLOCKFD_TO_FD()
   - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME
   - Falls back to standard clock handling for non-CLOCKFD clockids

3. clock_settime() enhancements:
   - Modified sched/clock/clock_settime.c to support CLOCKFD clockids
   - Extracts file descriptor and validates accessibility
   - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device
   - Maintains backward compatibility with standard POSIX clocks

4. File descriptor validation:
   - Checks file descriptor validity using fs_getfilep()
   - Ensures proper reference counting with fs_putfilep()
   - Returns appropriate error codes (EINVAL, EBADF) on failures

5. Integration with PTP clock framework:
   - Seamlessly integrates with PTP clock driver's ioctl interface
   - Enables standard POSIX clock API usage for dynamic PTP clocks
   - No changes required to existing applications using standard clocks

Usage example:
  int fd = open("/dev/ptp0", O_RDONLY);
  struct timespec ts;
  clock_gettime(CLOCKFD(fd), &ts);  /* Get PTP clock time */
  clock_settime(CLOCKFD(fd), &ts);  /* Set PTP clock time */
  close(fd);

This implementation follows the Linux kernel's PTP clock model, providing
a familiar interface for developers working with PTP hardware.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-30 10:22:09 -03:00
ouyangxiangzhen
dfdbf4dcf9 sched: Simplify the timer_start/cancel in sched_timerexpiration.
This commit simplified the timer_start/cancel in sched_timerexpiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-26 11:51:38 +08:00
ouyangxiangzhen
299ece7cbe sched: Simplify the interval adjustment in nxsched_timer_start.
This commit simplified the interval adjustment in nxsched_timer_start.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-26 11:51:38 +08:00
chao an
6300bc5522 sched/wdog: fix race condition in wd_gettime()
Move WDOG_ISACTIVE check inside critical section protection to avoid race condition

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-12-26 11:51:02 +08:00
ouyangxiangzhen
2dc2b30a6a sched/wdog: Simplify the code and correct the critical_section.
This commmit simplified the code and correct the critical_section.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
jiangtao16
c5814ac910 sched/wdog: compatible with MISRA-C
This commit fixed the MISRA C-2004 violation rule 11.1, 10.4 and more.

Signed-off-by: jiangtao16 <jiangtao16@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
6b63a81f54 sched/wdog: Simplify wd_timer to reduce WCET.
This commit simplified wd_timer() to reduce WCET when `noswitches` is false.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
43e0c50aa7 sched/wdog: Revert the spin-lock to big-kernel-lock (enter_critical_section).
If the wdog use the fine-grained spin-lock, and allow the callback execution without the lock held, there will be incorrect interleaving.
E.g. The first `nxsem_timeout` callback function caused the second semaphore wait to fail.

    Core 0 [nxsem_clockwait]       |  Core 1
    enter_critical_section()       |  ...
    wd_start(nxsem_timeout)        |  ...
    nxsem_wait(sem)                |  wd_expiration() --> nxsem_timeout
    wd_cancel(&rtcb->waitdog)      |  try enter_critical_section()
    leave_critical_section()       |  Failed retry...
    ....nxsem_clockwait            |  Failed retry...
    enter_critical_section()       |   Failed retry...
    wd_start(nxsem_timeout)        |  Failed retry...
    nxsem_wait(sem)                |  Core 1 enter the critical section
                                   |  nxsem_wait_irq(wtcb, ETIMEDOUT) -> incorrectly wake-up the rtcb.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
164b075d3f sched/wdog: Remove workaround for wdog latency.
This commit removed the workaround for wdog latency.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
476e02c7e0 sched/wdog: Faster wdog deleting.
This commit reduced 1 write operation for wdog deleting and decoupled
the WDOG_ISACTIVE with the list implementation.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
694dfc7a0c sched/wdog: Remove wd_recover.
This commit Removed wd_recover.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
7d01d8aab5 sched/wdog: Move the g_wdtimernested to sched_timerexpiration.
This commit moved the g_wdtimernested to sched_timerexpiration, since
wdog and hrtimer can share it.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen
e5db83d7db sched/sched: Remove nxsched_alarm_expiration
This commit removed nxsched_alarm_expiration to simplify the timer
expiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen
3aed2485f8 sched/sched: Remove nxsched_alarm_tick_expiration
This commit removed nxsched_alarm_tick_expiration to simplify the timer
expiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen
b048984dd4 sched/sched: Simplify nxsched_process_scheduler.
This commit simplified nxsched_process_scheduler.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen
5569755868 sched/sched: Change the return value semantics.
This commit changed the return value semantics.
In past designs, timers that were expected to fire immediately were delayed by at least one tick, resulting in a decrease in real-time performance.
This commit re-encoded the return value to allow the timers fire
immediately.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen
c785ed7175 sched/sched: Change the uint32_t ticks to clock_t.
This commit changed the uint32_t ticks to clock_t.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen
9848e5ecec sched: Simplify the timer expiration.
This commit simplified the timer expiration.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
anjiahao
994c9d8929 sched:use tcb inside of pthread_tcb_s, remove all cast
Simplify the code and remove different tcb

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-22 17:55:45 +08:00
anjiahao
0a2b01950c sched:use tcb_s inside of task_tcb_s , remove all cast
Simplify the code and remove different tcb

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-22 17:55:45 +08:00
wangzhi16
31e562b10f Revert "sched/group: move task group into task_tcb_s to improve performance"
This reverts commit 29e50ffa73.

reason:
Placing the main thread and the gourd in the same memory block, and allocating and freeing memory simultaneously, presents the following two problems:

When the main thread creates a child thread and performs a detach operation, there is a possibility that the main thread may have exited, but the main thread's TCB (Transaction Control Block) may not have been released.

This could potentially cause the main thread's TCB to be double-freed. The core contradiction in this problem lies in binding the main thread's TCB (Trust Container Registry) and the group together. When releasing the main thread's TCB, an additional check is needed to ensure the main thread was the last to leave the group. If this check and the free operation are atomically guaranteed, the logic is sound, and double freeing won't occur. However, this atomicity cannot be completely guaranteed. If other free operations cause a block, problems still arise.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2025-12-21 08:53:46 -03:00
anjiahao
fe7a877c4e sched/env_putenv:putenv is kernel function need use kmm_free
strdup if use __KERNEL__ defined by nx_strdup
so need kmm_free it

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-18 21:59:31 +08:00
wangchengdong
edf89ddedd sched/hrtimer: add safe synchronous hrtimer cancel API
Add a safe synchronous hrtimer cancel API, hrtimer_cancel_sync().
If the timer callback is currently executing, this function waits
until the callback has completed and the timer state transitions
to HRTIMER_STATE_INACTIVE.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-12-18 18:36:28 +08:00
wangchengdong
ed1cb6f9b8 sched/hrtimer: Add high-resolution timer support for NuttX
NuttX provides the wdog module to implement timers for the scheduler.
While it is lightweight and efficient, wdog only supports tick-level timers,
typically in milliseconds. Although the tick duration can be configured,
setting it to microsecond or nanosecond resolution is not practical.
Doing so may cause an interrupt storm, where the CPU is constantly
occupied handling tick interrupts.

To address this limitation, a new hrtimer module is introduced.
It coexists with the wdog module, providing both tick-level timers
for wdog and high-resolution timers (nanosecond-level) directly to users.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-12-18 18:36:28 +08:00
Xiang Xiao
0ba93e5b68 sched/init: Add OSINIT_IS_PANIC to replace 'g_nx_initstate == OSINIT_PANIC'
to simplify the code logic which need handle the panic in special way

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2025-12-18 18:21:55 +08:00
anjiahao
6f1e99c9bd sched: assert if call timedwait from interrupt
Nuttx does not allow calling interfaces like TIME_WAIT in interrupts, so we need to directly assert.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2025-12-18 18:19:42 +08:00