This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 1a7babf631aab2e417063047ca3234820043d28b
Author: buxiasen <[email protected]>
AuthorDate: Thu Aug 14 18:07:34 2025 +0800

    sched: Introduce Bound Multi-Processing (BMP) into NuttX
    
    Bound multiprocessing provides the scheduling control of an asymmetric
    multiprocessing model, while preserving the hardware abstraction and
    management of symmetric multiprocessing.
    
    BMP is similar to SMP, but you can specify which processors a thread
    can run on. You can use both SMP and BMP on the same system, allowing
    some threads to migrate from one processor to another, while other
    threads are restricted to one or more processors.
    
    As with SMP, a single copy of the OS maintains an overall view of all
    system resources, allowing them to be dynamically allocated and shared
    among applications. But, during application initialization, a setting
    determined by the system designer forces all of an application's threads
    to execute only on a specified CPU.
    
    Compared to full, floating SMP operation, this approach offers several
    advantages:
    
    1. It eliminates the cache thrashing that can reduce performance in an SMP
      system by allowing applications that share the same data set to run
      exclusively on the same CPU.
    2. It offers simpler application debugging than SMP since all execution
      threads within an application run on a single CPU.
    3. It helps legacy applications that use poor techniques for synchronizing
      shared data to run correctly, again by letting them run on a single CPU.
    
    Bound Multi-Processing (BMP):
    
    ---------------------------------------------
    | APP 0 | APP 1 | APP 2 | APP 3 | <- Application bound to CPU
    ---------------------------------------------
    | Data[0] | Data[1] | Data[2] | Data[3] | <- NuttX Kernel Data supports 
multiple CPU instances
    ---------------------------------------------
    | Share Code | <- NuttX kernel code shared for all CPUs
    ---------------------------------------------
    | UART 0 | SPI 0 | SPI 1 | I2C 0 | <- Driver is only registered to CPUs 
with application needs
    ---------------------------------------------
    | TIME 0 | TIME 1 | TIME 2 | TIME 3 | <- Core/CPU timers
    ---------------------------------------------
    | CPU0 | CPU1 | CPU2 | CPU3 | <- CPUs run independently
    ---------------------------------------------
    
    Some subsystem data does not need to be duplicated, especially the 
components bound to the application.
    For shared hardware devices, Use spinlock to avoid race-condition for 
multi-core.
    
    ---------------------------------------------
    | APP 0 | APP 1 | APP 2 | APP 3 |
    ---------------------------------------------
    | NetStack | BTStack | AUDIO | ... | <- Components bound to the 
application, data no need to duplicate.
    ---------------------------------------------
    | Share Code |
    ---------------------------------------------
    | Share UART (Protected by Spinlock) | <- Driver shared for all CPUS will 
protected by spinlock(e.g print logs)
    ---------------------------------------------
    | CPU0 | CPU1 | CPU2 | CPU3 |
    ---------------------------------------------
    
    Signed-off-by: chao an <[email protected]>
    Signed-off-by: buxiasen <[email protected]>
---
 boards/Kconfig         |  2 +-
 include/nuttx/percpu.h | 27 +++++++++++++++++++++++++++
 sched/Kconfig          | 38 ++++++++++++++++++++++++++++++++------
 3 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/boards/Kconfig b/boards/Kconfig
index 433abc11777..f8fc1f68a21 100644
--- a/boards/Kconfig
+++ b/boards/Kconfig
@@ -6236,7 +6236,7 @@ config BOARDCTL_USBDEVCTRL
 config BOARDCTL_IRQ_AFFINITY
        bool "Set an IRQ affinity to CPUs by software"
        default n
-       depends on SMP
+       depends on !UP
        ---help---
                Enables support for Set an IRQ affinity to CPUs by software.
 
diff --git a/include/nuttx/percpu.h b/include/nuttx/percpu.h
index e391e85da9b..528761cbbd6 100644
--- a/include/nuttx/percpu.h
+++ b/include/nuttx/percpu.h
@@ -28,6 +28,7 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+#include <nuttx/compiler.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -49,6 +50,20 @@
 #  define DECLARE_PER_CPU(t, v)        extern t v[CONFIG_NCPUS]
 #  define per_cpu_var_smp(v, c)        v[c]
 #  define this_cpu_var(v)              v[this_cpu()]
+#elif defined(CONFIG_PERCPU_SECTION)
+#  define DEFINE_PER_CPU(t, v)         locate_data(".data..percpu") t v
+#  define DEFINE_PER_CPU_BSS(t, v)     locate_data(".bss..percpu") t v
+#  define DECLARE_PER_CPU(t, v)        extern t v;
+
+/* Compile time percpu .data & .bss size */
+
+extern char _percpu_size[];
+#  define PERCPU_SIZE                  (size_t)_percpu_size
+
+/* For BMP case per_cpu_var is not supported */
+
+#  define per_cpu_var_smp(v, c)        (*(FAR typeof(v) *)((unsigned long)&(v) 
+ PERCPU_SIZE * (c)))
+#  define this_cpu_var(v)              (*(FAR typeof(v) *)((unsigned long)&(v) 
+ PERCPU_SIZE * up_cpu_index()))
 #endif
 
 #ifdef CONFIG_SMP
@@ -63,4 +78,16 @@
 #  define this_cpu_var_smp(v)          v[0]
 #endif
 
+#ifdef CONFIG_BMP
+#  define DEFINE_PER_CPU_BMP(t, v)     DEFINE_PER_CPU(t, v)
+#  define DEFINE_PER_CPU_BSS_BMP(t, v) DEFINE_PER_CPU_BSS(t, v)
+#  define DECLARE_PER_CPU_BMP(t, v)    DECLARE_PER_CPU(t, v)
+#  define this_cpu_var_bmp(v)          this_cpu_var(v)
+#else
+#  define DEFINE_PER_CPU_BMP(t, v)     t v
+#  define DEFINE_PER_CPU_BSS_BMP(t, v) t v
+#  define DECLARE_PER_CPU_BMP(t, v)    extern t v
+#  define this_cpu_var_bmp(v)          v
+#endif
+
 #endif
diff --git a/sched/Kconfig b/sched/Kconfig
index efb021cb5bb..e71c30b9267 100644
--- a/sched/Kconfig
+++ b/sched/Kconfig
@@ -410,9 +410,15 @@ config IRQCOUNT
        bool
        default n
 
+choice
+       prompt "Multi-Processing Support"
+       default UP
+
+config UP
+       bool "Uni-Processor/Single Processing as default"
+
 config SMP
        bool "Symmetric Multi-Processing (SMP)"
-       default n
        depends on ARCH_HAVE_MULTICPU
        depends on ARCH_INTERRUPTSTACK != 0
        select SPINLOCK
@@ -432,20 +438,41 @@ config SMP
                CPU0 IRQ handler use thread0's stack, but thread0 may switch to 
CPU1, that
                will caused IRQ handler stack corruption.
 
+config BMP
+       bool "Bound Multi-Processing (BMP)"
+       depends on ARCH_HAVE_MULTICPU
+       ---help---
+               Run a bound multi-processing configuration: every CPU runs
+               its own 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.  A CPU never
+               schedules work onto another one.
+
+               SMP and BMP are members of the same choice and mutually
+               exclusive: under SMP one scheduler drives all CPUs and tasks
+               migrate freely between cores; under BMP each CPU has an
+               independent scheduler.
+
+endchoice
+
 choice PERCPU_METHOD
        prompt "Support PERCPU expand method"
+       default PERCPU_SECTION if BMP
        default PERCPU_ARRAY
 
 config PERCPU_ARRAY
        bool "Expand PERCPU to ARRAY, for SMP by default"
 
-endchoice
+config PERCPU_SECTION
+       bool "Expand PERCPU by .ld, use section"
 
-if SMP
+endchoice
 
 config SMP_NCPUS
        int "Number of CPUs"
-       default 4
+       default 4 if !UP
+       default 1
        range 1 32
        ---help---
                This value identifies the number of CPUs supported by the 
processor
@@ -459,12 +486,11 @@ config SMP_NCPUS
 config SMP_DEFAULT_CPUSET
        hex "Default CPU bit set"
        default 0xffffffff
+       depends on SMP
        ---help---
                Set the Default CPU bits. The way to use the unset CPU is to 
call the
                sched_setaffinity function to bind a task to the CPU. bit0 
means CPU0.
 
-endif # SMP
-
 choice
        prompt "Initialization Task"
        default INIT_ENTRY if !BUILD_KERNEL

Reply via email to