Increase minimum required stack to suite ARM7 needs.
[bertos.git] / bertos / kern / proc.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Process scheduler (public interface).
34  *
35  * \version $Id$
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38 #ifndef KERN_PROC_H
39 #define KERN_PROC_H
40
41 #include "cfg/cfg_kern.h"
42 #include <cfg/compiler.h>
43
44 #if CONFIG_KERN_PREEMPT
45         #include <cfg/debug.h> // ASSERT()
46 #endif
47
48 #include <cpu/types.h> // cpu_stack_t
49 #include <cpu/frame.h> // CPU_SAVED_REGS_CNT
50
51 /*
52  * Forward declaration. The definition of struct Process is private to the
53  * scheduler and hidden in proc_p.h.
54  */
55 struct Process;
56
57 void proc_init(void);
58 struct Process *proc_new_with_name(const char *name, void (*entry)(void), iptr_t data, size_t stacksize, cpu_stack_t *stack);
59
60 #if !CONFIG_KERN_MONITOR
61         #define proc_new(entry,data,size,stack) proc_new_with_name(NULL,(entry),(data),(size),(stack))
62 #else
63         #define proc_new(entry,data,size,stack) proc_new_with_name(#entry,(entry),(data),(size),(stack))
64 #endif
65
66 void proc_exit(void);
67 void proc_yield(void);
68 void proc_rename(struct Process *proc, const char *name);
69 const char *proc_name(struct Process *proc);
70 const char *proc_currentName(void);
71 iptr_t proc_currentUserData(void);
72
73 int proc_testSetup(void);
74 int proc_testRun(void);
75 int proc_testTearDown(void);
76
77 /**
78  * Return the context structure of the currently running process.
79  *
80  * The details of the Process structure are private to the scheduler.
81  * The address returned by this function is an opaque pointer that can
82  * be passed as an argument to other process-related functions.
83  */
84 INLINE struct Process *proc_current(void)
85 {
86         extern struct Process *CurrentProcess;
87         return CurrentProcess;
88 }
89
90 #if CONFIG_KERN_PRI
91         void proc_setPri(struct Process *proc, int pri);
92 #else
93         INLINE void proc_setPri(UNUSED_ARG(struct Process *,proc), UNUSED_ARG(int, pri))
94         {
95         }
96 #endif
97
98 /**
99  * Disable preemptive task switching.
100  *
101  * The scheduler maintains a global nesting counter.  Task switching is
102  * effectively re-enabled only when the number of calls to proc_permit()
103  * matches the number of calls to proc_forbid().
104  *
105  * \note Calling functions that could sleep while task switching is disabled
106  * is dangerous and unsupported.
107  *
108  * \note calling proc_forbid() from within an interrupt is illegal and
109  * meaningless.
110  *
111  * \note proc_permit() expands inline to 1-2 asm instructions, so it's a
112  * very efficient locking primitive in simple but performance-critical
113  * situations.  In all other cases, semaphores offer a more flexible and
114  * fine-grained locking primitive.
115  *
116  * \sa proc_permit()
117  */
118 INLINE void proc_forbid(void)
119 {
120         #if CONFIG_KERN_PREEMPT
121                 extern cpu_atomic_t _preempt_forbid_cnt;
122                 /*
123                  * We don't need to protect the counter against other processes.
124                  * The reason why is a bit subtle.
125                  *
126                  * If a process gets here, preempt_forbid_cnt can be either 0,
127                  * or != 0.  In the latter case, preemption is already disabled
128                  * and no concurrency issues can occur.
129                  *
130                  * In the former case, we could be preempted just after reading the
131                  * value 0 from memory, and a concurrent process might, in fact,
132                  * bump the value of preempt_forbid_cnt under our nose!
133                  *
134                  * BUT: if this ever happens, then we won't get another chance to
135                  * run until the other process calls proc_permit() to re-enable
136                  * preemption.  At this point, the value of preempt_forbid_cnt
137                  * must be back to 0, and thus what we had originally read from
138                  * memory happens to be valid.
139                  *
140                  * No matter how hard you think about it, and how complicated you
141                  * make your scenario, the above holds true as long as
142                  * "preempt_forbid_cnt != 0" means that no task switching is
143                  * possible.
144                  */
145                 ++_preempt_forbid_cnt;
146
147                 /*
148                  * Make sure _preempt_forbid_cnt is flushed to memory so the
149                  * preemption softirq will see the correct value from now on.
150                  */
151                 MEMORY_BARRIER;
152         #endif
153 }
154
155 /**
156  * Re-enable preemptive task switching.
157  *
158  * \sa proc_forbid()
159  */
160 INLINE void proc_permit(void)
161 {
162         #if CONFIG_KERN_PREEMPT
163
164                 /*
165                  * This is to ensure any global state changed by the process gets
166                  * flushed to memory before task switching is re-enabled.
167                  */
168                 MEMORY_BARRIER;
169                 extern cpu_atomic_t _preempt_forbid_cnt;
170                 /* No need to protect against interrupts here. */
171                 ASSERT(_preempt_forbid_cnt != 0);
172                 --_preempt_forbid_cnt;
173
174                 /*
175                  * This ensures _preempt_forbid_cnt is flushed to memory immediately
176                  * so the preemption interrupt sees the correct value.
177                  */
178                 MEMORY_BARRIER;
179
180         #endif
181 }
182
183 /**
184  * \return true if preemptive task switching is allowed.
185  * \note This accessor is needed because _preempt_forbid_cnt
186  *       must be absoultely private.
187  */
188 INLINE bool proc_allowed(void)
189 {
190         #if CONFIG_KERN_PREEMPT
191                 extern cpu_atomic_t _preempt_forbid_cnt;
192                 return (_preempt_forbid_cnt == 0);
193         #else
194                 return true;
195         #endif
196 }
197
198 /**
199  * Execute a block of \a CODE atomically with respect to task scheduling.
200  */
201 #define PROC_ATOMIC(CODE) \
202         do { \
203                 proc_forbid(); \
204                 CODE; \
205                 proc_permit(); \
206         } while(0)
207
208 #ifndef CONFIG_KERN_MINSTACKSIZE
209
210         #if (ARCH & ARCH_EMUL)
211                 /* We need a large stack because system libraries are bloated */
212                 #define CONFIG_KERN_MINSTACKSIZE  65536
213         #else
214                 /**
215                  * Default stack size for each thread, in bytes.
216                  *
217                  * The goal here is to allow a minimal task to save all of its
218                  * registers twice, plus push a maximum of 32 variables on the
219                  * stack.
220                  *
221                  * The actual size computed by the default formula is:
222                  *   AVR:    102
223                  *   i386:   156
224                  *   ARM:    164
225                  *   x86_64: 184
226                  *
227                  * Note that on most 16bit architectures, interrupts will also
228                  * run on the stack of the currently running process.  Nested
229                  * interrupts will greatly increases the amount of stack space
230                  * required per process.  Use irqmanager to minimize stack
231                  * usage.
232                  */
233                 #define CONFIG_KERN_MINSTACKSIZE  \
234                     (CPU_SAVED_REGS_CNT * 2 * sizeof(cpu_stack_t) \
235                     + 48 * sizeof(int))
236         #endif
237 #endif
238
239 /* Memory fill codes to help debugging */
240 #if CONFIG_KERN_MONITOR
241         #include <cpu/types.h>
242         #if (SIZEOF_CPUSTACK_T == 1)
243                 /* 8bit cpu_stack_t */
244                 #define CONFIG_KERN_STACKFILLCODE  0xA5
245                 #define CONFIG_KERN_MEMFILLCODE    0xDB
246         #elif (SIZEOF_CPUSTACK_T == 2)
247                 /* 16bit cpu_stack_t */
248                 #define CONFIG_KERN_STACKFILLCODE  0xA5A5
249                 #define CONFIG_KERN_MEMFILLCODE    0xDBDB
250         #elif (SIZEOF_CPUSTACK_T == 4)
251                 /* 32bit cpu_stack_t */
252                 #define CONFIG_KERN_STACKFILLCODE  0xA5A5A5A5UL
253                 #define CONFIG_KERN_MEMFILLCODE    0xDBDBDBDBUL
254         #elif (SIZEOF_CPUSTACK_T == 8)
255                 /* 64bit cpu_stack_t */
256                 #define CONFIG_KERN_STACKFILLCODE  0xA5A5A5A5A5A5A5A5ULL
257                 #define CONFIG_KERN_MEMFILLCODE    0xDBDBDBDBDBDBDBDBULL
258         #else
259                 #error No cpu_stack_t size supported!
260         #endif
261 #endif
262
263 #endif /* KERN_PROC_H */