threads: Kernel handling of unsafe operations on user memory#664
threads: Kernel handling of unsafe operations on user memory#664
Conversation
There was a problem hiding this comment.
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 robust exception handling mechanism within the kernel, inspired by try-catch blocks and setjmp/longjmp semantics. This mechanism is designed to safely manage operations that interact with user memory, preventing processor exceptions and improving system stability. A key application demonstrated is enhanced stack overflow handling on MMU-enabled devices.
Highlights
- Kernel Exception Handling Framework: Implemented a
setjmp/longjmp-like mechanism (hal_setexcjmp,hal_excjmp) at the hardware abstraction layer (HAL) for ARMv7-A and IA32 architectures. This allows kernel code to gracefully recover from processor exceptions (e.g., page faults) that occur during operations on user memory, providing a 'try-catch' semantic. - Thread-Specific Exception Context: Introduced a new
excjmpctxfield to thethread_tstructure and addedthreads_setexcjmp/threads_getexcjmpfunctions. This enables the storage and retrieval of a thread-local exception jump context, ensuring that exception handling is managed on a per-thread basis. - Safer Signal Handling: Modified
hal_cpuPushSignalto utilize the new exception handling framework. This change protects the operations involved in setting up signal contexts against potential memory faults, returning an-EFAULTerror if a fault occurs, which then leads to the termination of the affected process inthreads_setupUserReturn. - Internal Code Refactoring: Replaced all internal calls to the static
_proc_currentfunction with the publicproc_currentfunction across theproc/threads.cfile, streamlining access to the current thread's context.
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
-
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. ↩
There was a problem hiding this comment.
Code Review
The code changes introduce a mechanism for handling kernel unsafe operations, similar to try-catch, using setjmp/longjmp. It includes functions for setting and getting exception jump contexts, and integrates this mechanism into signal handling. The changes span multiple files, modifying assembly code, header files, and C code to implement this new exception handling feature.
| } | ||
| else { | ||
| signalCtx->psr &= ~THUMB_STATE; | ||
| threads_setexcjmp(oldctx, NULL); |
There was a problem hiding this comment.
Perhaps we could clear old ctx on hal_excjmp
There was a problem hiding this comment.
That's problematic, as it would require putting pointer to oldctx in struct excjmp_context_t (as hal_excjmp doesn't have access to local variables from this context).
And we need to call threads_setexcjmp with oldctx after else anyways.
| /* | ||
| * Write to current[i] only by i'th cpu, with spinlock set. | ||
| * Read from current[i] by i'th cpu allowed without spinlock. | ||
| */ |
There was a problem hiding this comment.
Spinlock does nothing if it's used only for writes - let's just assume atomicity (and make these pointers volatile)
There was a problem hiding this comment.
It's not used only for writes - it's used for reads on other cores.
The idea was that on the i'th core it's not required for reading because it cannot be writing at the same time as it's reading.
3c8b5ff to
1472edb
Compare
1472edb to
a1498e6
Compare
Description
There are some events in kernel code that require operating on user's memory.
There's no good way ensure such memory operations won't trigger a processor exception apart from interrupting all threads that could access that memory fragment.
Inspired by Linux's exception tables and fixup code for managing unsafe operations, this draft introduces a mechanism with semantics similar to try - catch and based on idea similar to setjmp/longjmp by introducing
hal_setexcjmp(similar to setjmp) andthreads_setexcjmp(which only swaps pointers inthread_tstruct and should be used for cleaning after unsafe section is finished)As an example of usage it solves stack overflow handling on MMU devices: Jira RTOS-1066
phoenix-rtos/phoenix-rtos-project#1200
Motivation and Context
Types of changes
How Has This Been Tested?
Checklist:
Special treatment