Skip to content

SIMIA32: make port_lock/port_unlock nesting-aware - #66

Draft
FDSoftware wants to merge 1 commit into
rusefi:stable_21.11.x.rusefi_clean_historyfrom
FDSoftware:bugfix/SIMAI32_isr_lock
Draft

FDSoftware wants to merge 1 commit into
rusefi:stable_21.11.x.rusefi_clean_historyfrom
FDSoftware:bugfix/SIMAI32_isr_lock

Conversation

@FDSoftware

Copy link
Copy Markdown
Member

The SIMIA32 simulator port used a simple boolean flag (0/1) for port_lock()/port_unlock(). When ChibiOS timer callbacks call code that uses S-class chSysLock/chSysUnlock (e.g. CriticalSectionLocker inside efiPrintfInternal), chSysUnlock() would reset the flag to 0 even though chSysLockFromISR() had set it to 1 — effectively dropping the ISR lock and corrupting the cooperative scheduler's ready list and delta list.

On real ARM hardware this never happens because both S-class and I-class locks manipulate BASEPRI identically, so nesting is implicit.

Switch to a counter (++/--) so nested lock/unlock pairs balance correctly, matching the ARM port's behavior.

The SIMIA32 simulator port used a simple boolean flag (0/1) for
port_lock()/port_unlock().  When ChibiOS timer callbacks call code
that uses S-class chSysLock/chSysUnlock (e.g. CriticalSectionLocker
inside efiPrintfInternal), chSysUnlock() would reset the flag to 0
even though chSysLockFromISR() had set it to 1 — effectively dropping
the ISR lock and corrupting the cooperative scheduler's ready list and
delta list.

On real ARM hardware this never happens because both S-class and
I-class locks manipulate BASEPRI identically, so nesting is implicit.

Switch to a counter (++/--) so nested lock/unlock pairs balance
correctly, matching the ARM port's behavior.
@FDSoftware
FDSoftware marked this pull request as draft April 13, 2026 18:51
@rusefillc
rusefillc requested a review from dron0gus April 13, 2026 19:24
dron0gus pushed a commit to dron0gus/ChibiOS that referenced this pull request Sep 4, 2026
…usefi#66)

🤖 chibios-sheriff — backport (advisory)

Backport of **rusefi#61** (main commit `2447f6a`) to `stable-21.11.x`.
Stable's `ch.hpp` carries the same defects, so this is a real fix
backport.

**Fixes**
- `Scheduler::rescheduleS()` now actually calls `chSchRescheduleS()`
(was a no-op function *declaration*).
- Typed `Mailbox<T>` `fetch`/`peek` no longer overrun the caller's
storage when `sizeof(T) < sizeof(msg_t)` — the **memory-corruption** fix
(fetch into a local `msg_t`, narrow on `MSG_OK`); `post`/`peek` use
value casts.
- `ObjectsPool` rounds its object size up to pointer alignment (the
free-list stores a next-pointer per slot).
- The synchronization wrappers are no longer copyable, and the
`Registry::nextThread()` reference handoff is corrected.

**Adaptation:** none — cherry-pick auto-merged cleanly (stable's
`ch.hpp` matches main's structure). The `nextThread` handoff fix is
semantically correct here because stable's registry iterators are
reference-counting since rusefi#54.

**Local gates** (stable has no CI): `RT-STM32F407-DISCOVERY-G++` builds
clean (g++ 14.3.1); a compile-only probe instantiating
`Mailbox<uint8_t,8>` and `ObjectsPool<uint8_t,4>`
(`post`/`fetch`/`alloc`) compiles clean — exercising the small-T
template fixes. (`.hpp` is not in the CI style glob.)

**Changelog:** none — `os/various/cpp_wrappers` is not changelog-tracked
(matches rusefi#61 on main, which added no entry).

Sheriff-authored backport — a human reviews/merges.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant