4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2001,2004 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999,2000,2001 Bernie Innocenti <bernie@codewiz.org>
34 * \brief Simple realtime multitasking scheduler.
35 * Context switching is only done cooperatively.
39 * \author Bernie Innocenti <bernie@codewiz.org>
40 * \author Stefano Fedrigo <aleph@develer.com>
47 #include "cfg/cfg_arch.h" /* ARCH_EMUL */
48 #include <cfg/debug.h>
49 #include <cfg/module.h>
50 #include <cfg/macros.h> /* ABS() */
53 #include <cpu/types.h>
56 #include <mware/event.h>
58 #include <string.h> /* memset() */
61 * CPU dependent context switching routines.
63 * \note This function *MUST* preserve also the status of the interrupts.
65 EXTERN_C void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
66 EXTERN_C int asm_switch_version(void);
69 * The scheduer tracks ready and waiting processes
70 * by enqueuing them in these lists. A pointer to the currently
71 * running process is stored in the CurrentProcess pointer.
73 * NOTE: these variables are protected by DI/EI locking
75 REGISTER Process *CurrentProcess;
76 REGISTER List ProcReadyList;
79 #if CONFIG_KERN_PREEMPTIVE
81 * The time sharing scheduler forces a task switch when
82 * the current process has consumed its quantum.
88 /* In Win32 we must emulate stack on the real process stack */
89 #if (ARCH & ARCH_EMUL)
90 extern List StackFreeList;
93 /** The main process (the one that executes main()). */
94 struct Process MainProcess;
97 static void proc_init_struct(Process *proc)
99 /* Avoid warning for unused argument. */
102 #if CONFIG_KERN_SIGNALS
106 #if CONFIG_KERN_PREEMPTIVE
107 proc->forbid_cnt = 0;
119 LIST_INIT(&ProcReadyList);
121 #if CONFIG_KERN_MONITOR
125 /* We "promote" the current context into a real process. The only thing we have
126 * to do is create a PCB and make it current. We don't need to setup the stack
127 * pointer because it will be written the first time we switch to another process.
129 proc_init_struct(&MainProcess);
130 CurrentProcess = &MainProcess;
132 /* Make sure the assembly routine is up-to-date with us */
133 ASSERT(asm_switch_version() == 1);
139 * Create a new process, starting at the provided entry point.
141 * \return Process structure of new created process
142 * if successful, NULL otherwise.
144 struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(void), iptr_t data, size_t stack_size, cpustack_t *stack_base)
148 const size_t PROC_SIZE_WORDS = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
150 bool free_stack = false;
153 #if (ARCH & ARCH_EMUL)
154 /* Ignore stack provided by caller and use the large enough default instead. */
155 stack_base = (cpustack_t *)list_remHead(&StackFreeList);
157 stack_size = CONFIG_PROC_DEFSTACKSIZE;
158 #elif CONFIG_KERN_HEAP
159 /* Did the caller provide a stack for us? */
162 /* Did the caller specify the desired stack size? */
164 stack_size = CONFIG_PROC_DEFSTACKSIZE + sizeof(Process);
166 /* Allocate stack dinamically */
167 if (!(stack_base = heap_alloc(stack_size)))
173 /* Stack must have been provided by the user */
178 #if CONFIG_KERN_MONITOR
179 /* Fill-in the stack with a special marker to help debugging */
180 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stack_size / sizeof(cpustack_t));
183 /* Initialize the process control block */
184 if (CPU_STACK_GROWS_UPWARD)
186 proc = (Process*)stack_base;
187 proc->stack = stack_base + PROC_SIZE_WORDS;
188 if (CPU_SP_ON_EMPTY_SLOT)
193 proc = (Process*)(stack_base + stack_size / sizeof(cpustack_t) - PROC_SIZE_WORDS);
194 proc->stack = (cpustack_t*)proc;
195 if (CPU_SP_ON_EMPTY_SLOT)
199 proc_init_struct(proc);
200 proc->user_data = data;
202 #if CONFIG_KERN_HEAP | CONFIG_KERN_MONITOR | (ARCH & ARCH_EMUL)
203 proc->stack_base = stack_base;
204 proc->stack_size = stack_size;
207 proc->flags |= PF_FREESTACK;
211 /* Initialize process stack frame */
212 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
213 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
215 /* Push a clean set of CPU registers for asm_switch_context() */
216 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
217 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
219 /* Add to ready list */
220 ATOMIC(SCHED_ENQUEUE(proc));
222 #if CONFIG_KERN_MONITOR
223 monitor_add(proc, name);
229 /** Rename a process */
230 void proc_rename(struct Process *proc, const char *name)
232 #if CONFIG_KERN_MONITOR
233 monitor_rename(proc, name);
235 (void)proc; (void)name;
241 * System scheduler: pass CPU control to the next process in
244 * Saving and restoring the context on the stack is done
245 * by a CPU-dependent support routine which must usually be
246 * written in assembly.
248 void proc_schedule(void)
250 struct Process *old_process;
253 /* Remember old process to save its context later */
254 old_process = CurrentProcess;
257 /* Scheduling in interrupts is a nono. */
258 ASSERT(!IRQ_RUNNING());
261 /* Poll on the ready queue for the first ready process */
262 IRQ_SAVE_DISABLE(flags);
263 while (!(CurrentProcess = (struct Process *)list_remHead(&ProcReadyList)))
266 * Make sure we physically reenable interrupts here, no matter what
267 * the current task status is. This is important because if we
268 * are idle-spinning, we must allow interrupts, otherwise no
269 * process will ever wake up.
271 * During idle-spinning, an interrupt can occur and it may
272 * modify \p ProcReadyList. To ensure that compiler reload this
273 * variable every while cycle we call CPU_MEMORY_BARRIER.
274 * The memory barrier ensure that all variables used in this context
276 * \todo If there was a way to write sig_wait() so that it does not
277 * disable interrupts while waiting, there would not be any
288 * Optimization: don't switch contexts when the active
289 * process has not changed.
291 if (CurrentProcess != old_process)
295 #if CONFIG_KERN_PREEMPTIVE
296 /* Reset quantum for this process */
297 Quantum = CONFIG_KERN_QUANTUM;
300 /* Save context of old process and switch to new process. If there is no
301 * old process, we save the old stack pointer into a dummy variable that
302 * we ignore. In fact, this happens only when the old process has just
304 * TODO: Instead of physically clearing the process at exit time, a zombie
305 * list should be created.
307 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
310 /* This RET resumes the execution on the new process */
315 * Terminate the current process
319 #if CONFIG_KERN_MONITOR
320 monitor_remove(CurrentProcess);
325 * The following code is BROKEN.
326 * We are freeing our own stack before entering proc_schedule()
327 * BAJO: A correct fix would be to rearrange the scheduler with
328 * an additional parameter which frees the old stack/process
329 * after a context switch.
331 if (CurrentProcess->flags & PF_FREESTACK)
332 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
333 heap_free(CurrentProcess);
336 #if (ARCH & ARCH_EMUL)
337 #warning This is wrong
338 /* Reinsert process stack in free list */
339 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
340 - (CONFIG_PROC_DEFSTACKSIZE / sizeof(cpustack_t))));
343 * NOTE: At this point the first two words of what used
344 * to be our stack contain a list node. From now on, we
345 * rely on the compiler not reading/writing the stack.
347 #endif /* ARCH_EMUL */
349 CurrentProcess = NULL;
356 * Co-operative context switch
358 void proc_switch(void)
362 IRQ_SAVE_DISABLE(flags);
363 SCHED_ENQUEUE(CurrentProcess);
371 * Get the pointer to the current process
373 struct Process *proc_current(void)
375 return CurrentProcess;
379 * Get the pointer to the user data of the current process
381 iptr_t proc_current_user_data(void)
383 return CurrentProcess->user_data;
387 #if CONFIG_KERN_PREEMPTIVE
390 * Disable preemptive task switching.
392 * The scheduler maintains a per-process nesting counter. Task switching is
393 * effectively re-enabled only when the number of calls to proc_permit()
394 * matches the number of calls to proc_forbid().
396 * Calling functions that could sleep while task switching is disabled
397 * is dangerous, although supported. Preemptive task switching is
398 * resumed while the process is sleeping and disabled again as soon as
403 void proc_forbid(void)
405 /* No need to protect against interrupts here. */
406 ++CurrentProcess->forbid_cnt;
410 * Re-enable preemptive task switching.
414 void proc_permit(void)
416 /* No need to protect against interrupts here. */
417 --CurrentProcess->forbid_cnt;
420 #endif /* CONFIG_KERN_PREEMPTIVE */