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

   ## Summary
   
   This patch adds CMake build support for NuttX protected mode (kernel/user 
space separation) on STM32F4Discovery and similar ARM boards. Previously, 
protected mode configurations like `stm32f4discovery:kostest` could only be 
built with the Make build system. The CMake build system failed due to several 
architectural issues:
   
   ### Key Changes:
   
   1. **Multiple Linker Script Support** (CMakeLists.txt)
      - Modified linker script preprocessing to handle multiple scripts via 
`foreach` loops
      - Protected mode requires two linker scripts: `memory.ld` (memory 
regions) and `kernel-space.ld`/`user-space.ld` (section placements)
      - Previous implementation assumed single linker script via 
`get_filename_component()`
      - Now preprocesses each script individually and builds list with `-T` 
flags
   
   2. **User Space Binary Build** (boards/.../kernel/CMakeLists.txt)
      - Changed from `nuttx_add_aux_library()` to `target_sources(nuttx_user 
PRIVATE ...)`
      - The `nuttx_add_aux_library()` was creating an empty target without 
source files
      - Direct `target_sources()` properly adds userspace initialization code
   
   3. **Linker Script Selection** (boards/.../src/CMakeLists.txt)
      - Added conditional logic to select correct linker scripts based on 
`CONFIG_BUILD_PROTECTED`
      - Protected mode: `memory.ld` + `kernel-space.ld` for kernel, `memory.ld` 
+ `user-space.ld` for user
      - Flat mode: `ld.script` (existing behavior)
      - Removed hardcoded `-funwind-tables` flag that was adding ~4.7KB bloat 
per binary
   
   4. **Critical .userspace Section** (boards/.../scripts/user-space.ld)
      - Added `KEEP(*(.userspace))` directive to prevent linker garbage 
collection
      - Added `EXTERN()` declarations for memory management functions
      - The `.userspace` section contains `struct userspace_s` with memory map 
and entry points
      - Without `KEEP()`, CMake's static library linking discards unreferenced 
sections
   
   5. **Memory Layout Updates** (boards/.../scripts/memory.ld)
      - Fixed memory region sizes: ksram 4KB→16KB, usram 4KB→16KB, xsram 
104KB→80KB
      - Updated comments to reflect correct memory map for protected mode
      - Aligns with kernel heap size requirement 
(`CONFIG_MM_KERNEL_HEAPSIZE=16384`)
   
   6. **Configuration Updates** (boards/.../configs/kostest/)
      - Added `Make.defs` for kostest configuration (defines dual linker 
scripts)
      - Updated `defconfig`: enabled FPU, increased idle stack, excluded CCM, 
set kernel heap size
   
   ### Technical Details:
   
   The root cause was that Make builds link userspace object files directly to 
the linker, while CMake links from static library archives. When sections 
aren't explicitly referenced, the linker's garbage collector removes them from 
archives. The `.userspace` section at address `0x08020000` is critical—the 
kernel reads it during boot to initialize user space memory regions, entry 
points, and heap structures.
   
   ## Impact
   
   **Positive Impacts:**
   - Enables CMake build system for protected mode configurations on STM32 and 
similar ARM platforms
   - Maintains binary size parity with Make builds (~62KB user, ~60KB kernel 
for kostest)
   - No changes to existing flat mode builds or other architectures
   - Provides path forward for deprecating Make build system
   
   **Build Process:**
   - CMake 3.x or higher required (tested with 4.2.3)
   - Ninja generator recommended for parallel builds
   - Build command: `cmake -B build -DBOARD_CONFIG=stm32f4discovery:kostest 
-GNinja && ninja -C build`
   - Compatible with existing Make builds: `./tools/configure.sh 
stm32f4discovery:kostest && make`
   
   ## Testing
   
   **Test Environment:**
   - Host: Linux (Ubuntu/similar distribution)
   - CMake version: 4.2.3
   - Ninja version: 1.11.1
   - Toolchain: arm-none-eabi-gcc 15.2.1 (arm-gnu-toolchain)
   - Board: STM32F4Discovery (STM32F407VG, ARM Cortex-M4 @ 168MHz)
   - Configuration: `stm32f4discovery:kostest` (protected mode OS test suite)


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