LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/kernel/include - ksmp.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 3 3
Test Date: 2026-11-06 14:43:19 Functions: 100.0 % 1 1
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright The Zephyr Project Contributors
       3                 :             :  * SPDX-License-Identifier: Apache-2.0
       4                 :             :  */
       5                 :             : 
       6                 :             : /*
       7                 :             :  * Hooks through which the common kernel code reaches the SMP support in
       8                 :             :  * kernel/smp/. Each hook has a uniprocessor variant at the end of this file
       9                 :             :  * that compiles to nothing, so the callers carry no SMP conditionals.
      10                 :             :  */
      11                 :             : 
      12                 :             : #ifndef ZEPHYR_KERNEL_INCLUDE_KSMP_H_
      13                 :             : #define ZEPHYR_KERNEL_INCLUDE_KSMP_H_
      14                 :             : 
      15                 :             : #include <zephyr/kernel.h>
      16                 :             : #include <zephyr/kernel_structs.h>
      17                 :             : #include <zephyr/spinlock.h>
      18                 :             : #include <zephyr/sys/barrier.h>
      19                 :             : #include <wait_q.h>
      20                 :             : 
      21                 :             : #ifdef CONFIG_SMP
      22                 :             : 
      23                 :             : /* Architecture SMP setup, run between PRE_KERNEL_1 and PRE_KERNEL_2. */
      24                 :             : static inline void z_smp_arch_init(void)
      25                 :             : {
      26                 :             :         (void)arch_smp_init();
      27                 :             : }
      28                 :             : 
      29                 :             : /* Start the secondary CPUs once the static threads are ready. */
      30                 :             : void z_smp_init(void);
      31                 :             : 
      32                 :             : /* Initialize the per-CPU state that only exists on SMP. */
      33                 :             : static inline void z_smp_cpu_init(struct _cpu *cpu)
      34                 :             : {
      35                 :             : #ifdef CONFIG_SCHED_IPI_SUPPORTED
      36                 :             :         sys_dlist_init(&cpu->ipi_workq);
      37                 :             : #else
      38                 :             :         ARG_UNUSED(cpu);
      39                 :             : #endif /* CONFIG_SCHED_IPI_SUPPORTED */
      40                 :             : }
      41                 :             : 
      42                 :             : /* Initialize the thread base fields that only exist on SMP. */
      43                 :             : static inline void z_smp_thread_base_init(struct _thread_base *base)
      44                 :             : {
      45                 :             :         base->is_idle = 0;
      46                 :             : 
      47                 :             :         /* Pretend that the thread was last executing on CPU0 to prevent
      48                 :             :          * out-of-bounds memory accesses to the _kernel.cpus[] array.
      49                 :             :          */
      50                 :             :         base->cpu = 0;
      51                 :             : }
      52                 :             : 
      53                 :             : static inline void z_smp_thread_mark_idle(struct k_thread *thread)
      54                 :             : {
      55                 :             :         thread->base.is_idle = 1U;
      56                 :             : }
      57                 :             : 
      58                 :             : /* Wait queue on which the thread suspending @p thread waits while
      59                 :             :  * @p thread runs on another CPU.
      60                 :             :  */
      61                 :             : static inline void z_smp_halt_queue_init(struct k_thread *thread)
      62                 :             : {
      63                 :             :         z_waitq_init(&thread->halt_queue);
      64                 :             : }
      65                 :             : 
      66                 :             : /* Wake the threads waiting on the halt queue. Scheduler spinlock held. */
      67                 :             : void z_smp_halt_queue_wake(struct k_thread *thread);
      68                 :             : 
      69                 :             : /* Return the CPU on which @p thread is currently running, or NULL when it
      70                 :             :  * is not running on another CPU.
      71                 :             :  */
      72                 :             : static inline struct _cpu *z_smp_thread_active_elsewhere(struct k_thread *thread)
      73                 :             : {
      74                 :             :         int thread_cpu_id = thread->base.cpu;
      75                 :             :         struct _cpu *thread_cpu;
      76                 :             : 
      77                 :             :         __ASSERT_NO_MSG((thread_cpu_id >= 0) && (thread_cpu_id < arch_num_cpus()));
      78                 :             : 
      79                 :             :         thread_cpu = &_kernel.cpus[thread_cpu_id];
      80                 :             :         if ((thread_cpu->current == thread) && (thread_cpu != _current_cpu)) {
      81                 :             :                 return thread_cpu;
      82                 :             :         }
      83                 :             : 
      84                 :             :         return NULL;
      85                 :             : }
      86                 :             : 
      87                 :             : /* Halt @p thread, which runs on @p cpu: flag it, poke the CPU and wait
      88                 :             :  * until the thread has halted itself. Releases the scheduler spinlock
      89                 :             :  * @p key before returning.
      90                 :             :  */
      91                 :             : void z_smp_halt_remote(struct k_thread *thread, struct _cpu *cpu, k_spinlock_key_t key,
      92                 :             :                        bool terminate);
      93                 :             : 
      94                 :             : /* Clear the halting bits (_THREAD_ABORTING and _THREAD_SUSPENDING). */
      95                 :             : static inline void z_smp_clear_halting(struct k_thread *thread)
      96                 :             : {
      97                 :             :         if (CONFIG_MP_MAX_NUM_CPUS > 1) {
      98                 :             :                 barrier_dmem_fence_full(); /* Other cpus spin on this locklessly! */
      99                 :             :                 thread->base.thread_state &= ~(_THREAD_ABORTING | _THREAD_SUSPENDING);
     100                 :             :         }
     101                 :             : }
     102                 :             : 
     103                 :             : /* Record whether _current may be switched out cooperatively. The CPU
     104                 :             :  * record keeps the flag until the next reschedule call or context switch.
     105                 :             :  * It doesn't need to be tracked per thread because if the thread gets
     106                 :             :  * preempted for whatever reason the scheduler will make the same decision
     107                 :             :  * anyway.
     108                 :             :  */
     109                 :             : static inline void z_smp_swap_ok_set(bool ok)
     110                 :             : {
     111                 :             :         _current_cpu->swap_ok = ok;
     112                 :             : }
     113                 :             : 
     114                 :             : /* In SMP, the irq_lock() is a spinlock which is implicitly released
     115                 :             :  * and reacquired on context switch to preserve the existing
     116                 :             :  * semantics.  This means that whenever we are about to return to a
     117                 :             :  * thread (via either z_swap() or interrupt/exception return!) we need
     118                 :             :  * to restore the lock state to whatever the thread's counter
     119                 :             :  * expects.
     120                 :             :  */
     121                 :             : void z_smp_release_global_lock(struct k_thread *thread);
     122                 :             : 
     123                 :             : /* Select the thread to switch to. Scheduler spinlock held. */
     124                 :             : struct k_thread *z_swap_next_thread(void);
     125                 :             : 
     126                 :             : /* Bind @p new_thread to this CPU before z_swap() switches to it. */
     127                 :             : static inline void z_smp_swap_prepare(struct k_thread *new_thread, bool release_global_lock)
     128                 :             : {
     129                 :             :         new_thread->base.cpu = arch_curr_cpu()->id;
     130                 :             : 
     131                 :             :         if (release_global_lock) {
     132                 :             :                 z_smp_release_global_lock(new_thread);
     133                 :             :         }
     134                 :             : }
     135                 :             : 
     136                 :             : /* Spin, with the scheduler lock held (!), on a thread that is known
     137                 :             :  * (!!) to have released the lock and be on a path where it will
     138                 :             :  * deterministically (!!!) reach arch_switch() in very small constant
     139                 :             :  * time.
     140                 :             :  *
     141                 :             :  * This exists to treat an unavoidable SMP race when threads swap --
     142                 :             :  * their thread record is in the queue (and visible to other CPUs)
     143                 :             :  * before arch_switch() finishes saving state.  We must spin for the
     144                 :             :  * switch handle before entering a new thread.  See docs on
     145                 :             :  * arch_switch().
     146                 :             :  *
     147                 :             :  * Stated differently: there's a chicken and egg bug with the question
     148                 :             :  * of "is a thread running or not?".  The thread needs to mark itself
     149                 :             :  * "not running" from its own context, but at that moment it obviously
     150                 :             :  * is still running until it reaches arch_switch()!  Locking can't
     151                 :             :  * treat this because the scheduler lock can't be released by the
     152                 :             :  * switched-to thread, which is going to (obviously) be running its
     153                 :             :  * own code and doesn't know it was switched out.
     154                 :             :  */
     155                 :             : static inline void z_sched_switch_spin(struct k_thread *thread)
     156                 :             : {
     157                 :             :         volatile void **shp = (void *)&thread->switch_handle;
     158                 :             : 
     159                 :             :         while (*shp == NULL) {
     160                 :             :                 arch_spin_relax();
     161                 :             :         }
     162                 :             :         /* Read barrier: don't allow any subsequent loads in the
     163                 :             :          * calling code to reorder before we saw switch_handle go
     164                 :             :          * non-null.
     165                 :             :          */
     166                 :             :         barrier_dmem_fence_full();
     167                 :             : }
     168                 :             : 
     169                 :             : /* Active threads must have a NULL switch handle, and it must be seen
     170                 :             :  * before the scheduler lock is released.
     171                 :             :  */
     172                 :             : static inline void z_smp_switch_handle_clear(struct k_thread *thread)
     173                 :             : {
     174                 :             :         thread->switch_handle = NULL;
     175                 :             :         barrier_dmem_fence_full(); /* write barrier */
     176                 :             : }
     177                 :             : 
     178                 :             : /* Put the outgoing @p old_thread back in the run queue once the switch
     179                 :             :  * away from it is certain, and send the pending IPIs.
     180                 :             :  */
     181                 :             : void z_smp_switch_requeue(struct k_thread *old_thread);
     182                 :             : 
     183                 :             : #else /* !CONFIG_SMP */
     184                 :             : 
     185                 :             : static inline void z_smp_arch_init(void)
     186                 :             : {
     187                 :             : }
     188                 :             : 
     189                 :             : static inline void z_smp_init(void)
     190                 :             : {
     191                 :             : }
     192                 :             : 
     193                 :             : static inline void z_smp_cpu_init(struct _cpu *cpu)
     194                 :             : {
     195                 :             :         ARG_UNUSED(cpu);
     196                 :             : }
     197                 :             : 
     198                 :             : static inline void z_smp_thread_base_init(struct _thread_base *base)
     199                 :             : {
     200                 :             :         ARG_UNUSED(base);
     201                 :             : }
     202                 :             : 
     203                 :             : static inline void z_smp_thread_mark_idle(struct k_thread *thread)
     204                 :             : {
     205                 :             :         ARG_UNUSED(thread);
     206                 :             : }
     207                 :             : 
     208                 :             : static inline void z_smp_halt_queue_init(struct k_thread *thread)
     209                 :             : {
     210                 :             :         ARG_UNUSED(thread);
     211                 :             : }
     212                 :             : 
     213                 :             : static inline void z_smp_halt_queue_wake(struct k_thread *thread)
     214                 :             : {
     215                 :             :         ARG_UNUSED(thread);
     216                 :             : }
     217                 :             : 
     218                 :          55 : static inline struct _cpu *z_smp_thread_active_elsewhere(struct k_thread *thread)
     219                 :             : {
     220                 :          55 :         ARG_UNUSED(thread);
     221                 :             : 
     222                 :          55 :         return NULL;
     223                 :             : }
     224                 :             : 
     225                 :             : static inline void z_smp_halt_remote(struct k_thread *thread, struct _cpu *cpu,
     226                 :             :                                      k_spinlock_key_t key, bool terminate)
     227                 :             : {
     228                 :             :         ARG_UNUSED(thread);
     229                 :             :         ARG_UNUSED(cpu);
     230                 :             :         ARG_UNUSED(key);
     231                 :             :         ARG_UNUSED(terminate);
     232                 :             : }
     233                 :             : 
     234                 :             : static inline void z_smp_clear_halting(struct k_thread *thread)
     235                 :             : {
     236                 :             :         ARG_UNUSED(thread);
     237                 :             : }
     238                 :             : 
     239                 :             : static inline void z_smp_swap_ok_set(bool ok)
     240                 :             : {
     241                 :             :         ARG_UNUSED(ok);
     242                 :             : }
     243                 :             : 
     244                 :             : static inline void z_smp_swap_prepare(struct k_thread *new_thread, bool release_global_lock)
     245                 :             : {
     246                 :             :         ARG_UNUSED(new_thread);
     247                 :             :         ARG_UNUSED(release_global_lock);
     248                 :             : }
     249                 :             : 
     250                 :             : static inline void z_sched_switch_spin(struct k_thread *thread)
     251                 :             : {
     252                 :             :         ARG_UNUSED(thread);
     253                 :             : }
     254                 :             : 
     255                 :             : static inline void z_smp_switch_handle_clear(struct k_thread *thread)
     256                 :             : {
     257                 :             :         ARG_UNUSED(thread);
     258                 :             : }
     259                 :             : 
     260                 :             : static inline void z_smp_switch_requeue(struct k_thread *old_thread)
     261                 :             : {
     262                 :             :         ARG_UNUSED(old_thread);
     263                 :             : }
     264                 :             : 
     265                 :             : #endif /* CONFIG_SMP */
     266                 :             : 
     267                 :             : #endif /* ZEPHYR_KERNEL_INCLUDE_KSMP_H_ */
        

Generated by: LCOV version 2.0-1