Skip to content

hal/armvXm: use cpu_context_t as exc_context_t #685

Open
etiaro wants to merge 2 commits intomasterfrom
etiaro/armvXm-excctx
Open

hal/armvXm: use cpu_context_t as exc_context_t #685
etiaro wants to merge 2 commits intomasterfrom
etiaro/armvXm-excctx

Conversation

@etiaro
Copy link
Copy Markdown
Contributor

@etiaro etiaro commented Aug 12, 2025

Description

This way armv7m & armv8m targets follow convention of having cpu_context_t as substructure of exc_context_t and residing at the end of kernel stack when being in kernel space even during fault handling.

Also adjusted SIZE_FPUCTX to include fpscr and padding as described in ARM docs.

Currently tested only on imxrt117x

Motivation and Context

This assumption simplifies producing coredump in unified way among all targets.

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?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: imxrt117x-evk

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

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

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 refactors the exception context handling for ARMv7m and ARMv8m targets. By aliasing exc_context_t to cpu_context_t, it standardizes how exception contexts are managed, ensuring they reside at the end of the kernel stack even during fault handling. This change is motivated by the goal of simplifying unified coredump generation across different targets.

Highlights

  • Unified Exception Context: The exc_context_t type definition has been changed to directly use cpu_context_t across both ARMv7m and ARMv8m architectures, streamlining context management.
  • Context Member Renaming: References to excret and mspctx within the exception context structure have been updated to irq_ret and hwctx respectively, aligning with the new cpu_context_t structure.
  • Assembly Context Handling Adjustments: The assembly code for exception dispatch (_exceptions_dispatch) in various ARMv7m and ARMv8m target initialisation files (_init.S) has been modified to correctly save and restore the updated cpu_context_t structure, including adjustments for FPU context and stack alignment.
  • FPU Context Size Update: The SIZE_FPUCTX macro in hal/armv7m/exceptions.c has been increased to accommodate the full FPU context size.
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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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 exception handling on armv7m and armv8m to use cpu_context_t directly as exc_context_t, which is a good change for consistency and simplifying coredump generation. The changes in the header files and C code are correct and consistent with this goal. However, I've identified a recurring issue in several assembly files where a register (r2) is saved to the exception context without being initialized first. This leads to uninitialized data in the context structure, which can cause unpredictable behavior. I've added specific comments with suggestions to fix this in each affected file.

@etiaro etiaro mentioned this pull request Aug 12, 2025
14 tasks
@etiaro etiaro force-pushed the etiaro/armvXm-excctx branch from 1a68372 to ff4062b Compare August 13, 2025 10:48
@etiaro etiaro marked this pull request as ready for review August 13, 2025 10:52
@github-actions
Copy link
Copy Markdown

github-actions bot commented Aug 13, 2025

Unit Test Results

9 445 tests  ±0   8 856 ✅ ±0   51m 44s ⏱️ + 2m 23s
  545 suites ±0     589 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit c076305. ± Comparison against base commit c827247.

♻️ This comment has been updated with latest results.

/* if we came from userspace, make space for hw-saved regs */
tst lr, #(1 << 2)
it ne
subne sp, sp, #(26 * 4) /* space for hw-saved ctx */
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.

I think the comment is somewhat misleading - we're aligning the cpu_context_t struct, not making space for the ctx - it's never put in there. I would like to also suggest writing the size out more explicitly - #((8 + 18) * 4)

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.

Also, I think we can skip this aliment - part of the struct will point to the trash, but it's never accessed. On the other hand we'll save some stack space (that is used extensively in the exception handler)

Copy link
Copy Markdown
Contributor Author

@etiaro etiaro Aug 18, 2025

Choose a reason for hiding this comment

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

Sure, the comment could be better indeed.

The problem shows up when we start accesing this context as kstack + kstacksz - sizeof(cpu_context_t):
Even if we don't want to deliver signals from exceptions on this target, it's still useful for coredump to have single kind of context

Copy link
Copy Markdown
Contributor Author

@etiaro etiaro Aug 18, 2025

Choose a reason for hiding this comment

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

I'm also wondering if we shouldn't move s0-s15, fpscr, pad1 from cpu_context_t to cpu_hwContext_t for more clarity, as it seems to me like these are also hardware-saved.
Or am I missing something here?

@etiaro etiaro force-pushed the etiaro/armvXm-excctx branch from ff4062b to 2df7005 Compare August 18, 2025 12:19
@etiaro etiaro force-pushed the etiaro/armvXm-excctx branch from 790747f to c076305 Compare November 4, 2025 15:57
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.

3 participants