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 Bernardo Innocenti <bernie@develer.com>
34 * \brief Simple realtime multitasking scheduler.
35 * Context switching is only done cooperatively.
39 * \author Bernardo Innocenti <bernie@develer.com>
40 * \author Stefano Fedrigo <aleph@develer.com>
47 #include <mware/event.h>
49 #include <cpu/types.h>
51 #include <cfg/debug.h>
52 #include <cfg/arch_config.h> /* ARCH_EMUL */
53 #include <cfg/macros.h> /* ABS() */
55 #include <string.h> /* memset() */
58 * CPU dependent context switching routines.
60 * \note This function *MUST* preserve also the status of the interrupts.
62 EXTERN_C void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
63 EXTERN_C int asm_switch_version(void);
66 * The scheduer tracks ready and waiting processes
67 * by enqueuing them in these lists. A pointer to the currently
68 * running process is stored in the CurrentProcess pointer.
70 * NOTE: these variables are protected by DI/EI locking
72 REGISTER Process *CurrentProcess;
73 REGISTER List ProcReadyList;
76 #if CONFIG_KERN_PREEMPTIVE
78 * The time sharing scheduler forces a task switch when
79 * the current process has consumed its quantum.
85 /* In Win32 we must emulate stack on the real process stack */
86 #if (ARCH & ARCH_EMUL)
87 extern List StackFreeList;
90 /** The main process (the one that executes main()). */
91 struct Process MainProcess;
94 static void proc_init_struct(Process *proc)
96 /* Avoid warning for unused argument. */
99 #if CONFIG_KERN_SIGNALS
103 #if CONFIG_KERN_PREEMPTIVE
104 proc->forbid_cnt = 0;
115 LIST_INIT(&ProcReadyList);
117 #if CONFIG_KERN_MONITOR
121 /* We "promote" the current context into a real process. The only thing we have
122 * to do is create a PCB and make it current. We don't need to setup the stack
123 * pointer because it will be written the first time we switch to another process.
125 proc_init_struct(&MainProcess);
126 CurrentProcess = &MainProcess;
128 /* Make sure the assembly routine is up-to-date with us */
129 ASSERT(asm_switch_version() == 1);
134 * Create a new process, starting at the provided entry point.
136 * \return Process structure of new created process
137 * if successful, NULL otherwise.
139 struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(void), iptr_t data, size_t stacksize, cpustack_t *stack_base)
143 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
145 bool free_stack = false;
148 #if (ARCH & ARCH_EMUL)
149 /* Ignore stack provided by caller and use the large enough default instead. */
150 stack_base = (cpustack_t *)LIST_HEAD(&StackFreeList);
151 REMOVE(LIST_HEAD(&StackFreeList));
152 stacksize = CONFIG_KERN_DEFSTACKSIZE;
153 #elif CONFIG_KERN_HEAP
154 /* Did the caller provide a stack for us? */
157 /* Did the caller specify the desired stack size? */
159 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
161 /* Allocate stack dinamically */
162 if (!(stack_base = heap_alloc(stacksize)))
168 /* Stack must have been provided by the user */
173 #if CONFIG_KERN_MONITOR
174 /* Fill-in the stack with a special marker to help debugging */
175 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
178 /* Initialize the process control block */
179 if (CPU_STACK_GROWS_UPWARD)
181 proc = (Process*)stack_base;
182 proc->stack = stack_base + proc_size_words;
183 if (CPU_SP_ON_EMPTY_SLOT)
188 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
189 proc->stack = (cpustack_t*)proc;
190 if (CPU_SP_ON_EMPTY_SLOT)
194 proc_init_struct(proc);
195 proc->user_data = data;
198 proc->stack_base = stack_base;
199 proc->stack_size = stack_size;
201 proc->flags |= PF_FREESTACK;
204 /* Initialize process stack frame */
205 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
206 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
208 /* Push a clean set of CPU registers for asm_switch_context() */
209 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
210 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
212 /* Add to ready list */
213 ATOMIC(SCHED_ENQUEUE(proc));
215 #if CONFIG_KERN_MONITOR
216 monitor_add(proc, name, stack_base, stacksize);
222 /** Rename a process */
223 void proc_rename(struct Process *proc, const char *name)
225 #if CONFIG_KERN_MONITOR
226 monitor_rename(proc, name);
228 (void)proc; (void)name;
234 * System scheduler: pass CPU control to the next process in
237 * Saving and restoring the context on the stack is done
238 * by a CPU-dependent support routine which must usually be
239 * written in assembly.
241 void proc_schedule(void)
243 /* This function must not have any "auto" variables, otherwise
244 * the compiler might put them on the stack of the process
245 * being switched out.
247 static struct Process *old_process;
248 static cpuflags_t flags;
250 /* Remember old process to save its context later */
251 old_process = CurrentProcess;
254 /* Scheduling in interrupts is a nono. */
255 ASSERT(!IRQ_RUNNING());
258 /* Poll on the ready queue for the first ready process */
259 IRQ_SAVE_DISABLE(flags);
260 while (!(CurrentProcess = (struct Process *)list_remHead(&ProcReadyList)))
263 * Make sure we physically reenable interrupts here, no matter what
264 * the current task status is. This is important because if we
265 * are idle-spinning, we must allow interrupts, otherwise no
266 * process will ever wake up.
268 * \todo If there was a way to write sig_wait() so that it does not
269 * disable interrupts while waiting, there would not be any
279 * Optimization: don't switch contexts when the active
280 * process has not changed.
282 if (CurrentProcess != old_process)
284 static cpustack_t *dummy;
286 #if CONFIG_KERN_PREEMPTIVE
287 /* Reset quantum for this process */
288 Quantum = CONFIG_KERN_QUANTUM;
291 /* Save context of old process and switch to new process. If there is no
292 * old process, we save the old stack pointer into a dummy variable that
293 * we ignore. In fact, this happens only when the old process has just
295 * TODO: Instead of physically clearing the process at exit time, a zombie
296 * list should be created.
298 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
301 /* This RET resumes the execution on the new process */
306 * Terminate the current process
310 #if CONFIG_KERN_MONITOR
311 monitor_remove(CurrentProcess);
316 * The following code is BROKEN.
317 * We are freeing our own stack before entering proc_schedule()
318 * BAJO: A correct fix would be to rearrange the scheduler with
319 * an additional parameter which frees the old stack/process
320 * after a context switch.
322 if (CurrentProcess->flags & PF_FREESTACK)
323 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
324 heap_free(CurrentProcess);
327 #if (ARCH & ARCH_EMUL)
328 #warning This is wrong
329 /* Reinsert process stack in free list */
330 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
331 - (CONFIG_KERN_DEFSTACKSIZE / sizeof(cpustack_t))));
334 * NOTE: At this point the first two words of what used
335 * to be our stack contain a list node. From now on, we
336 * rely on the compiler not reading/writing the stack.
338 #endif /* ARCH_EMUL */
340 CurrentProcess = NULL;
347 * Co-operative context switch
349 void proc_switch(void)
351 /* Just like proc_schedule, this function must not have auto variables. */
352 static cpuflags_t flags;
354 IRQ_SAVE_DISABLE(flags);
355 SCHED_ENQUEUE(CurrentProcess);
363 * Get the pointer to the current process
365 struct Process *proc_current(void)
367 return CurrentProcess;
371 * Get the pointer to the user data of the current process
373 iptr_t proc_current_user_data(void)
375 return CurrentProcess->user_data;
379 #if CONFIG_KERN_PREEMPTIVE
382 * Disable preemptive task switching.
384 * The scheduler maintains a per-process nesting counter. Task switching is
385 * effectively re-enabled only when the number of calls to proc_permit()
386 * matches the number of calls to proc_forbid().
388 * Calling functions that could sleep while task switching is disabled
389 * is dangerous, although supported. Preemptive task switching is
390 * resumed while the process is sleeping and disabled again as soon as
395 void proc_forbid(void)
397 /* No need to protect against interrupts here. */
398 ++CurrentProcess->forbid_cnt;
402 * Re-enable preemptive task switching.
406 void proc_permit(void)
408 /* No need to protect against interrupts here. */
409 --CurrentProcess->forbid_cnt;
412 #endif /* CONFIG_KERN_PREEMPTIVE */