Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018 Intel Corporation
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : : #ifndef ZEPHYR_KERNEL_INCLUDE_KSWAP_H_
7 : : #define ZEPHYR_KERNEL_INCLUDE_KSWAP_H_
8 : :
9 : : #include <kspinlock.h>
10 : : #include <ksched.h>
11 : : #include <ksmp.h>
12 : : #include <run_q.h>
13 : : #include <zephyr/spinlock.h>
14 : : #include <zephyr/sys/barrier.h>
15 : : #include <kernel_arch_func.h>
16 : : #include <timeslicing.h>
17 : : #include <usage.h>
18 : :
19 : : #ifdef CONFIG_STACK_SENTINEL
20 : : extern void z_check_stack_sentinel(void);
21 : : #else
22 : : #define z_check_stack_sentinel() /**/
23 : : #endif /* CONFIG_STACK_SENTINEL */
24 : :
25 : : /* context switching and scheduling-related routines */
26 : : #ifdef CONFIG_USE_SWITCH
27 : :
28 : : /* New style context switching. arch_switch() is a lower level
29 : : * primitive that doesn't know about the scheduler or return value.
30 : : * Needed for SMP, where the scheduler requires spinlocking that we
31 : : * don't want to have to do in per-architecture assembly.
32 : : *
33 : : * Note that is_spinlock is a compile-time construct which will be
34 : : * optimized out when this function is expanded.
35 : : */
36 : : static ALWAYS_INLINE unsigned int do_swap(unsigned int key,
37 : : struct k_spinlock *lock,
38 : : bool is_spinlock)
39 : : {
40 : : struct k_thread *new_thread, *old_thread;
41 : :
42 : : #ifdef CONFIG_SPIN_VALIDATE
43 : : z_assert_can_swap(key, lock);
44 : : #endif /* CONFIG_SPIN_VALIDATE */
45 : :
46 : : old_thread = _current;
47 : :
48 : : z_check_stack_sentinel();
49 : :
50 : : old_thread->swap_retval = -EAGAIN;
51 : :
52 : : /* We always take the scheduler spinlock if we don't already
53 : : * have it. We "release" other spinlocks here. But we never
54 : : * drop the interrupt lock.
55 : : */
56 : : if (is_spinlock && (lock != NULL) && !z_is_sched_spinlock(lock)) {
57 : : k_spin_release(lock);
58 : : }
59 : : if (IS_ENABLED(CONFIG_SMP) || IS_ENABLED(CONFIG_SPIN_VALIDATE)) {
60 : : /* Taking a nested uniprocessor lock in void context is a noop */
61 : : if (!is_spinlock || !z_is_sched_spinlock(lock)) {
62 : : (void)z_sched_spinlock_lock();
63 : : }
64 : : }
65 : :
66 : : #ifdef CONFIG_SMP
67 : : new_thread = z_swap_next_thread();
68 : : #else
69 : : new_thread = _kernel.ready_q.cache;
70 : : #endif
71 : :
72 : : if (new_thread != old_thread) {
73 : : z_sched_usage_switch(new_thread);
74 : : z_smp_swap_prepare(new_thread, !is_spinlock);
75 : : z_thread_mark_switched_out();
76 : : z_sched_switch_spin(new_thread);
77 : : z_current_thread_set(new_thread);
78 : :
79 : : #if defined(CONFIG_TIMESLICING) && defined(CONFIG_SMP)
80 : : /* Uniprocessor builds start it in update_cache() */
81 : : z_time_slice_reset(new_thread);
82 : : #endif /* CONFIG_TIMESLICING && CONFIG_SMP */
83 : :
84 : : z_sched_spinlock_transfer_owner(new_thread);
85 : :
86 : : arch_cohere_stacks(old_thread, NULL, new_thread);
87 : : z_smp_switch_requeue(old_thread);
88 : :
89 : : void *newsh = new_thread->switch_handle;
90 : :
91 : : z_smp_switch_handle_clear(new_thread);
92 : : z_sched_spinlock_release();
93 : : arch_switch(newsh, &old_thread->switch_handle);
94 : : } else {
95 : : z_sched_spinlock_release();
96 : : }
97 : :
98 : : if (is_spinlock) {
99 : : arch_irq_unlock(key);
100 : : } else {
101 : : irq_unlock(key);
102 : : }
103 : :
104 : : return _current->swap_retval;
105 : : }
106 : :
107 : : static inline int z_swap_irqlock(unsigned int key)
108 : : {
109 : : return do_swap(key, NULL, false);
110 : : }
111 : :
112 : : static inline int z_swap(struct k_spinlock *lock, k_spinlock_key_t key)
113 : : {
114 : : return do_swap(key.key, lock, true);
115 : : }
116 : :
117 : : static inline void z_swap_unlocked(void)
118 : : {
119 : : (void) do_swap(arch_irq_lock(), NULL, true);
120 : : }
121 : :
122 : : #else /* !CONFIG_USE_SWITCH */
123 : :
124 : 114 : static inline int z_swap_irqlock(unsigned int key)
125 : : {
126 : 114 : int ret;
127 : 114 : z_check_stack_sentinel();
128 : :
129 : : #ifdef CONFIG_SPIN_VALIDATE
130 : : z_assert_can_swap(key, NULL);
131 : : #endif /* CONFIG_SPIN_VALIDATE */
132 : :
133 : 114 : ret = arch_swap(key);
134 : 60 : return ret;
135 : : }
136 : :
137 : : /* If !USE_SWITCH, then spinlocks are guaranteed degenerate as we
138 : : * can't be in SMP. The k_spin_release() call is just for validation
139 : : * handling.
140 : : */
141 : 114 : static ALWAYS_INLINE int z_swap(struct k_spinlock *lock, k_spinlock_key_t key)
142 : : {
143 : 114 : k_spin_release(lock);
144 : 114 : return z_swap_irqlock(key.key);
145 : : }
146 : :
147 : : static inline void z_swap_unlocked(void)
148 : : {
149 : : (void) z_swap_irqlock(arch_irq_lock());
150 : : }
151 : :
152 : : #endif /* !CONFIG_USE_SWITCH */
153 : :
154 : : /**
155 : : * @brief - Like z_swap() but it is known that the scheduler's spinlock is already held.
156 : : */
157 : 64 : static inline int z_swap_locked(k_spinlock_key_t key)
158 : : {
159 : 64 : return z_swap(&_sched_spinlock, key);
160 : : }
161 : :
162 : : /**
163 : : * Set up a "dummy" thread, used at early initialization to launch the
164 : : * first thread on a CPU.
165 : : *
166 : : * Needs to set enough fields such that the context switching code can
167 : : * use it to properly store state, which will just be discarded.
168 : : *
169 : : * The memory of the dummy thread can be completely uninitialized.
170 : : */
171 : : void z_dummy_thread_init(struct k_thread *dummy_thread);
172 : :
173 : : #endif /* ZEPHYR_KERNEL_INCLUDE_KSWAP_H_ */
|