zyfeier opened a new pull request, #22261:
URL: https://github.com/apache/nuttx/pull/22261

   ## Summary
   
   This is the first part of a series that adds Bound Multi-Processing (BMP) to 
NuttX. In BMP mode every CPU runs its own NuttX kernel instance with its own 
data, while all CPUs share one copy of the kernel code. Applications and 
drivers are bound to the CPU that needs them; hardware shared by several CPUs 
is protected by spinlocks.
   
   This PR adds only the configuration and the per-CPU data infrastructure. It 
does not change how the kernel runs.
   
   **Per-CPU variables (`include/nuttx/percpu.h`)**
   
   - `DEFINE_PER_CPU()`, `DECLARE_PER_CPU()`, `this_cpu_var()` and 
`per_cpu_var_smp()`, plus `_SMP` and `_BMP` variants that collapse to a plain 
variable when SMP or BMP is off.
   - `CONFIG_PERCPU_METHOD` selects how the copies are laid out:
     - `PERCPU_ARRAY` (default): one array element per CPU.
     - `PERCPU_SECTION` (default for BMP): variables are placed in 
`.data..percpu` / `.bss..percpu`, and each CPU finds its copy at `PERCPU_OFFSET 
* cpu`. The architecture linker script provides `_percpu_offset`.
   - `this_cpu()`, `up_this_cpu()` and `up_cpu_index()` move from `sched.h` and 
`arch.h` into `percpu.h`, so the per-CPU macros do not depend on the include 
order.
   - `userspace.h` gets the matching macros for kernel code that reads per-CPU 
variables of the user blob in protected builds, and a single `USERSPACE_HEAP` 
accessor for the user heap.
   
   **Kconfig (`sched/Kconfig`)**
   
   - A new choice "Multi-Processing Support": `UP` (default), `SMP` or `BMP`.
   - `this_cpu()` returns the logical CPU index unless `CONFIG_UP` is set, so 
it is correct in both SMP and BMP.
   - `BOARDCTL_IRQ_AFFINITY` depends on `!UP` instead of `SMP`.
   
   `CONFIG_BMP` is already used in the tree: `system/nxinit` in nuttx-apps uses 
a per-core trigger key (`init.trigger.core<N>`) when `CONFIG_BMP` is set, and 
`boards/boardctl.c` documents `BOARDIOC_IRQ_AFFINITY` as a BMP feature. Until 
now the option itself did not exist.
   
   nxstyle is updated to accept `DEFINE_PER_CPU_*` declarations with an 
initializer.
   
   ## Follow-up PRs
   
   `CONFIG_BMP` can be selected after this PR, but a BMP image does not work 
until the following parts are merged. Planned follow-up PRs, in the intended 
merge order:
   
   | PR | Content |
   |---|---|
   | PR-2 | BMP core: per-CPU kernel startup (one `nx_start()` per CPU), 
scheduler, irq, mqueue, signal and mm state converted to per-CPU |
   | PR-T | TriCore port: per-CPU section linking, secondary CPU boot, 
interrupt, timer, MPU and trap support, plus a BMP configuration for an 
existing TC4x board |
   | PR-3 | fs, libc / binfmt / crypto, syslog and rpmsg / rptun / openamp 
converted to per-CPU where needed |
   | PR-4 / PR-5 | net and drivers, in several smaller batches |
   | later | arm / arm64 / sim reference ports |
   
   ## Impact
   
   - **Users / existing boards**: no change. No defconfig enables BMP or 
`PERCPU_SECTION`, and nothing in the tree uses the new macros yet. `make 
savedefconfig` gives no diff for `sim:smp` and `qemu-armv8a:nsh_smp`.
   - **Kconfig**: `SMP` is now one option of a choice. No Kconfig file in the 
tree selects or implies `SMP`, so this does not break any dependency. 
`SMP_NCPUS` is now defined in UP builds too (value 1); the code that checks 
`#ifdef CONFIG_SMP_NCPUS` already falls back to 1, so the result is the same. 
`SMP_DEFAULT_CPUSET` still depends on `SMP`.
   - **Headers**: `percpu.h` is included by `sched.h` and `arch.h`, and 
includes `arch/arch.h` for `up_cpu_index()`. Its C declarations are guarded by 
`__ASSEMBLY__`.
   - **Build / hardware / documentation / security**: no change.
   - **Compatibility**: `per_cpu_var()` is renamed to `per_cpu_var_smp()`; it 
had no users in nuttx or nuttx-apps.
   
   ## Testing
   
   Host: Linux x86_64. Cross builds use the 
`ghcr.io/apache/nuttx/apache-nuttx-ci-linux` image. All results are on the 
final branch.
   
   | Configuration | Result |
   |---|---|
   | `sim:nsh` | build OK |
   | `sim:ostest` | ostest passes, same 173 tests as on master `b5db91fe0da` |
   | `sim:smp` (4 CPUs) | build OK, `savedefconfig` no diff |
   | `qemu-armv8a:nsh_smp` (`CONFIG_SMP_NCPUS=2`) | build OK, `savedefconfig` 
no diff, ostest and smp pass |
   | `smartl-c906:knsh` (kernel build) | build OK |
   | `stm32l476vg-disco:knsh` (small RAM) | build OK |
   | `tools/checkpatch.sh -c -u -m -g` | all checks pass |
   
   sim:ostest:
   
   ```
   user_main: scheduler lock test
   ...
   user_main: pthread_sigqueue test
   ...
   ostest_main: Exiting with status 0
   ```
   
   qemu-armv8a:nsh_smp (`CONFIG_SMP=y`, `CONFIG_SMP_NCPUS=2`, 
`CONFIG_PERCPU_ARRAY=y`):
   
   ```
   ostest_main: Exiting with status 0
   nsh> smp
   ...
   Thread[3]: Now running on CPU0
   Thread[2]: Now running on CPU1
     Main[0]: Now running on CPU1
     Main[0]: Thread 1 completed with result=0
   ...
     Main[0]: Thread 8 completed with result=0
   ```
   
   smartl-c906:knsh:
   
   ```
            progmem:      112384 B         1 MB     10.72%
               sram:       10720 B         1 MB      1.02%
   CP: nuttx.hex
   CP: nuttx.bin
   ```
   
   stm32l476vg-disco:knsh:
   
   ```
              ksram:        8136 B         8 KB     99.32%
   CP: nuttx.hex
   CP: nuttx.bin
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to