Skip to content

Conversation

etiaro
Copy link
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

Test results

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)

Resolves phoenix-rtos/phoenix-rtos-project#1367
JIRA: RTOS-736

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

@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

@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.

@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from da9f58a to cdc57ad Compare July 14, 2025 14:39
Copy link
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.

@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
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.

@@ -138,7 +138,7 @@ int hal_cpuCreateContext(cpu_context_t **nctx, void *start, void *kstack, size_t
}


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
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
Contributor Author

Choose a reason for hiding this comment

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

I guess i can give it a try once the threads.c logic would be fine

Copy link
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 requested a review from ziemleszcz August 12, 2025 10:29
@etiaro etiaro mentioned this pull request Aug 12, 2025
14 tasks
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from f7f2833 to 1089083 Compare August 18, 2025 13:04
etiaro added 6 commits August 22, 2025 14:36
* keep actions for every signal and signal trampoline in process_t struct
* 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
* ensure that termination by signal happens before returning from syscall or exception

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
Interrupt when signaling specific intrruptible thread

JIRA: RTOS-736
Before this change, a vforked process that received a signal would die
and its parent would not be resumed nor killed.
Now the parent is resumed and the vforked process is killed.

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

JIRA: RTOS-736
@etiaro etiaro force-pushed the etiaro/kernel-sighandlers branch from 1089083 to 4b21ec0 Compare August 22, 2025 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libphoenix: calling signal or sigaction causes temporary unmasking of other signals
2 participants