Skip to content

!syscalls: move signal handling to kernel#670

Open
etiaro wants to merge 8 commits into
masterfrom
etiaro/kernel-sighandlers
Open

!syscalls: move signal handling to kernel#670
etiaro wants to merge 8 commits into
masterfrom
etiaro/kernel-sighandlers

Conversation

@etiaro
Copy link
Copy Markdown
Contributor

@etiaro etiaro commented Jul 14, 2025

Description

  • keep actions for every signal and signal trampoline in process_t struct
    • allocate actions lazily on first change
  • remove unused sigmask from process_t struct, rely on thread_t sigmask field
  • add handler address as signal_trampoline stack parameter
  • use POSIX signal codes and default behaviors
  • replaced signalHandle syscall with signalAction
  • handled vforked process signal termination
  • remove signalPost syscall as duplicate to sys_tkill

Motivation and Context

Moving singal handlers to kernel enables it to know whether coredump should be run, by knowing whether a signal handler is installed for given signal.
Simplifies handling exceptions with problems like stackoverflow (JIRA: RTOS-1066, currently stackoverflow causes kernel exception during sigsegv handling even with default sigsegv action).
Allows improved POSIX compliability (eg. not interrupting sleep by ignored signals).

Resolves phoenix-rtos/phoenix-rtos-project#1367
Resolves phoenix-rtos/phoenix-rtos-project#1456
Resolves phoenix-rtos/phoenix-rtos-project#1464
Resolves phoenix-rtos/phoenix-rtos-project#1453
Resolves phoenix-rtos/phoenix-rtos-project#1432
Resolves phoenix-rtos/phoenix-rtos-project#1448
JIRA: RTOS-736

Depends-On: phoenix-rtos-tests:etiaro/signals

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @etiaro, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a major overhaul of the kernel's signal handling system, transitioning from a custom implementation to one that adheres closely to POSIX standards. The changes involve restructuring data types, centralizing signal configuration within the kernel's process and thread management, and updating the system call interface for signal manipulation. This refactoring aims to provide a more robust and standard-compliant signal delivery mechanism.

Highlights

  • POSIX Signal Alignment: The signal handling mechanism has been significantly refactored to align with POSIX standards. This includes replacing custom signal enumerations with standard POSIX signal numbers (e.g., SIGHUP, SIGSEGV) and introducing comprehensive sigaction, siginfo_t, and sigset_t structures.
  • Kernel-level Signal Management: Signal handling logic has been moved more deeply into the kernel. The process_t structure now stores an array of sigaction structures for per-signal configuration and a dedicated sigtrampoline address. Signal masks are now primarily managed per-thread, improving granularity.
  • Updated Signal Delivery and Syscalls: The core signal delivery functions (threads_sigpost, _threads_checkSignal) have been rewritten to utilize the new sigaction structure, supporting flags like SA_NODEFER and SA_RESETHAND. The signalHandle syscall has been renamed to signalAction and updated to accept sigaction parameters. Signal mask manipulation now uses a new threads_updateSigmask helper.
  • HAL Integration: The Hardware Abstraction Layer (HAL) function hal_cpuPushSignal has been modified to differentiate between the signal trampoline and the actual handler address, ensuring both are correctly passed to the signal context on the stack.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the signal handling mechanism, moving it into the kernel and aligning it more closely with POSIX standards. The changes are well-structured, replacing the old system with a more robust sigaction-based approach. I've identified areas for improvement to enhance correctness and robustness, specifically around input validation in the signal posting logic and a type mismatch in a new function's signature.

Comment thread proc/threads.c
Comment thread proc/threads.c Outdated
Comment thread proc/threads.h Outdated
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from da9f58a to cdc57ad Compare July 14, 2025 14:39
Copy link
Copy Markdown
Member

@nalajcie nalajcie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add me as a reviewer to draft PRs without any extra context (as per our coding guidelines - we're not reviewing drafts, they are work-in-progress until you explicitly mark it as ready-for-review).

As an exception I did a code review of the kernel part, I won't be looking at libphoenix part unless explicitly asked.

I'm missing a context regarding as to Why the change is needed.

Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/process.c Outdated
Comment thread syscalls.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread include/signal.h Outdated
Comment thread proc/threads.c Outdated
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch 5 times, most recently from 9ea0758 to c1877cf Compare July 23, 2025 14:46
Copy link
Copy Markdown
Member

@nalajcie nalajcie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you've summarised - the signal handling is now cluttered and really not readable. Probably all of the actual signal handling should be done in _threads_checkSignal (or maybe some wrapper function for that) - all other code paths are "optimizations" which are either prone to a race condition or a little bit convoluted (I'm not saying that we shouldn't eg. handle SIGKILL as early as possible as an optimization).

You may want to look at other OSes regarding how signal handling is being implemented there (usually in more complex way but the basics - like deciding on action to perform on signal dequeue in the context of receiving thread - are universal).

Please - present the additional test cases You've implemented the code you're pushing here passes - eg. I don't see setting return code of the process killed by a signal anywhere.

Comment thread include/signal.h Outdated
Comment thread proc/process.c Outdated
Comment thread include/signal.h
Comment thread include/signal.h Outdated
Comment thread proc/process.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread hal/armv7m/cpu.c Outdated


int hal_cpuPushSignal(void *kstack, void (*handler)(void), cpu_context_t *signalCtx, int n, unsigned int oldmask, const int src)
int hal_cpuPushSignal(void *kstack, void (*trampoline)(void), void *handler, cpu_context_t *signalCtx, int n, unsigned int oldmask, const int src)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably more kosher (but harder to implement currently) would be to create 2 stack frames - with trampoline (calling sigreturn) below the handler - and jump directly to the handler

Copy link
Copy Markdown
Contributor Author

@etiaro etiaro Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's worth the trouble, would require some additional changes apart from stack modification (eg. setting arm lr register in or before hal_jmp to handler)

@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch 3 times, most recently from c4bee02 to 55c6d5c Compare August 5, 2025 10:42
etiaro added a commit to phoenix-rtos/phoenix-rtos-tests that referenced this pull request Aug 5, 2025
Additional signal tests for phoenix-rtos/phoenix-rtos-kernel#670 moving signal handling to kernel
Showcasing issue phoenix-rtos/phoenix-rtos-project#1367

JIRA: RTOS-736
etiaro added a commit to phoenix-rtos/phoenix-rtos-tests that referenced this pull request Aug 6, 2025
Additional signal tests for phoenix-rtos/phoenix-rtos-kernel#670 moving signal handling to kernel

JIRA: RTOS-736
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch 3 times, most recently from 16d9ea3 to 315ade1 Compare August 6, 2025 07:30
@etiaro etiaro marked this pull request as ready for review August 6, 2025 07:58
@etiaro etiaro requested a review from nalajcie August 6, 2025 07:59
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch 4 times, most recently from 04e0116 to 4a62e15 Compare August 8, 2025 09:22
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from f7c61cb to 3390b9e Compare February 26, 2026 12:34
Copy link
Copy Markdown
Contributor

@julianuziemblo julianuziemblo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small things. I'm not an expert but for me it looks good.

Comment thread include/signal.h Outdated
Comment thread include/signal.h Outdated
Comment thread include/signal.h
Comment thread proc/threads.h Outdated
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from 3390b9e to 11d9d21 Compare February 26, 2026 17:51
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from 11d9d21 to cfee82c Compare April 24, 2026 14:19
etiaro added a commit to phoenix-rtos/phoenix-rtos-tests that referenced this pull request Apr 24, 2026
Additional signal tests for phoenix-rtos/phoenix-rtos-kernel#670 moving signal handling to kernel

JIRA: RTOS-736
Comment thread syscalls.c Fixed
Comment thread syscalls.c Fixed
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from cfee82c to e28ad29 Compare April 24, 2026 15:12
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 24, 2026

Unit Test Results (nightly)

     1 files   3 070 suites   6h 3m 50s ⏱️
26 559 tests 22 596 ✅ 3 963 💤 0 ❌
26 745 runs  22 743 ✅ 4 002 💤 0 ❌

Results for commit c01e4ee.

♻️ This comment has been updated with latest results.

@etiaro
Copy link
Copy Markdown
Contributor Author

etiaro commented Apr 30, 2026

Waiting with rerun for master nightly to work properly.

@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from e28ad29 to 54e18ec Compare May 6, 2026 08:18
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from 54e18ec to ae74901 Compare May 19, 2026 16:34
etiaro added a commit to phoenix-rtos/phoenix-rtos-tests that referenced this pull request May 19, 2026
Additional signal tests for phoenix-rtos/phoenix-rtos-kernel#670 moving signal handling to kernel

JIRA: RTOS-736
Comment thread proc/threads.c Fixed
Comment thread proc/threads.c Fixed
Comment thread syscalls.c Fixed
Comment thread syscalls.c Fixed
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from ae74901 to f3294a7 Compare May 19, 2026 17:02
Comment thread syscalls.c Outdated
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch 3 times, most recently from 181111b to 82a9805 Compare May 20, 2026 09:11
etiaro added a commit to phoenix-rtos/phoenix-rtos-tests that referenced this pull request May 20, 2026
Additional signal tests for phoenix-rtos/phoenix-rtos-kernel#670 moving signal handling to kernel

JIRA: RTOS-736
etiaro added 8 commits May 20, 2026 17:55
Previous approach to signal handling split between userspace libphoenix
and kernel made it hard, if not impossible, to provide full
POSIX-compliance.

JIRA: RTOS-736
Reduce size of process_t struct by allocating
sigactions array only when needed.

JIRA: RTOS-736
POSIX requirement:
It is not possible to block those signals which cannot be ignored. This
shall be enforced by the system without causing an error to be indicated

JIRA: RTOS-736
Make behavior of direct thread signaling consistent with signaling whole
process

JIRA: RTOS-736
Resume vfork parent when child is terminated via signal.

JIRA: RTOS-1101
Removed signalPost syscall as it duplicates functionality of sys_tkill

JIRA: RTOS-736
Ensure WIFSIGNALED works as expected when exit is called
with code above 255 and improve POSIX-compliance:
"... only the least significant 8 bits (that is, status & 0377) shall be
available to a waiting parent process."

JIRA: RTOS-1300
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from 82a9805 to c01e4ee Compare May 20, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants