4 * Copyright 2001,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999,2000,2001 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
9 * \brief Simple realtime multitasking scheduler.
10 * Context switching is only done cooperatively.
14 * \author Bernardo Innocenti <bernie@develer.com>
15 * \author Stefano Fedrigo <aleph@develer.com>
20 *#* Revision 1.16 2004/10/03 20:39:28 bernie
21 *#* Import changes from sc/firmware.
23 *#* Revision 1.15 2004/09/20 03:29:39 bernie
26 *#* Revision 1.14 2004/09/14 21:06:44 bernie
27 *#* Use debug.h instead of kdebug.h.
29 *#* Revision 1.13 2004/08/29 21:58:53 bernie
30 *#* Include macros.h explicityl.
32 *#* Revision 1.11 2004/08/24 16:09:08 bernie
33 *#* Add missing header.
35 *#* Revision 1.10 2004/08/24 16:07:01 bernie
36 *#* Use kputs()/kputchar() when possible.
38 *#* Revision 1.9 2004/08/24 14:26:57 bernie
39 *#* monitor_debug_stacks(): Conditionally compile on CONFIG_KERN_MONITOR.
41 *#* Revision 1.8 2004/08/14 19:37:57 rasky
42 *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
44 *#* Revision 1.7 2004/08/02 20:20:29 aleph
45 *#* Merge from project_ks
47 *#* Revision 1.6 2004/07/30 14:24:16 rasky
48 *#* Task switching con salvataggio perfetto stato di interrupt (SR)
49 *#* Kernel monitor per dump informazioni su stack dei processi
51 *#* Revision 1.5 2004/07/14 14:18:09 rasky
52 *#* Merge da SC: Rimosso timer dentro il task, che รจ uno spreco di memoria per troppi task
54 *#* Revision 1.4 2004/07/13 19:21:28 aleph
55 *#* Avoid warning for unused arg when compiled without some CONFIG_KERN_xx options
57 *#* Revision 1.3 2004/06/06 18:37:57 bernie
58 *#* Rename event macros to look like regular functions.
60 *#* Revision 1.2 2004/06/03 11:27:09 bernie
61 *#* Add dual-license information.
63 *#* Revision 1.1 2004/05/23 17:27:00 bernie
64 *#* Import kern/ subdirectory.
74 #include <arch_config.h> /* ARCH_EMUL */
75 #include <macros.h> /* ABS() */
77 #include <string.h> /* memset() */
80 * CPU dependent context switching routines.
82 * \note This function *MUST* preserve also the status of the interrupts.
84 EXTERN_C void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
85 EXTERN_C int asm_switch_version(void);
88 * The scheduer tracks ready and waiting processes
89 * by enqueuing them in these lists. A pointer to the currently
90 * running process is stored in the CurrentProcess pointer.
92 * NOTE: these variables are protected by DI/EI locking
94 REGISTER Process *CurrentProcess;
95 REGISTER List ProcReadyList;
98 #if CONFIG_KERN_PREEMPTIVE
100 * The time sharing scheduler forces a task switch when
101 * the current process has consumed its quantum.
107 /* In Win32 we must emulate stack on the real process stack */
108 #if (ARCH & ARCH_EMUL)
109 extern List StackFreeList;
112 /* The main process (the one that executes main()) */
113 struct Process MainProcess;
116 static void proc_init_struct(Process* proc)
118 /* Avoid warning for unused argument */
121 #if CONFIG_KERN_SIGNALS
133 INITLIST(&ProcReadyList);
135 #if CONFIG_KERN_MONITOR
139 /* We "promote" the current context into a real process. The only thing we have
140 * to do is create a PCB and make it current. We don't need to setup the stack
141 * pointer because it will be written the first time we switch to another process.
143 proc_init_struct(&MainProcess);
144 CurrentProcess = &MainProcess;
146 /* Make sure the assembly routine is up-to-date with us */
147 ASSERT(asm_switch_version() == 1);
152 * Create a new process, starting at the provided entry point.
154 * \return Process structure of new created process
155 * if successful, NULL otherwise.
157 struct Process *proc_new_with_name(UNUSED(const char*, name), void (*entry)(void), IPTR data, size_t stacksize, cpustack_t *stack_base)
162 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
164 bool free_stack = false;
167 #if (ARCH & ARCH_EMUL)
168 /* Ignore stack provided by caller
169 * and use the large enough default instead
171 stack_base = (cpustack_t *)StackFreeList.head;
172 REMOVE((Node *)stack_base);
173 stacksize = DEF_STACKSIZE;
174 #elif CONFIG_KERN_HEAP
175 /* Did the caller provide a stack for us? */
178 /* Did the caller specify the desired stack size? */
180 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
182 /* Allocate stack dinamically */
183 if (!(stack_base = heap_alloc(stacksize)))
189 /* Stack must have been provided by the user */
194 #if CONFIG_KERN_MONITOR
195 /* Fill-in the stack with a special marker to help debugging */
196 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
199 /* Initialize the process control block */
200 if (CPU_STACK_GROWS_UPWARD)
202 proc = (Process*)stack_base;
203 proc->stack = stack_base + proc_size_words;
204 if (CPU_SP_ON_EMPTY_SLOT)
209 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
210 proc->stack = (cpustack_t*)proc;
211 if (CPU_SP_ON_EMPTY_SLOT)
215 proc_init_struct(proc);
216 proc->user_data = data;
219 proc->stack_base = stack_base;
220 proc->stack_size = stack_size;
222 proc->flags |= PF_FREESTACK;
225 /* Initialize process stack frame */
226 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
227 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
229 /* Push a clean set of CPU registers for asm_switch_context() */
230 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
231 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
233 /* Add to ready list */
234 DISABLE_IRQSAVE(flags);
236 ENABLE_IRQRESTORE(flags);
238 #if CONFIG_KERN_MONITOR
239 monitor_add(proc, name, stack_base, stacksize);
247 * System scheduler: pass CPU control to the next process in
250 * Saving and restoring the context on the stack is done
251 * by a CPU-dependent support routine which must usually be
252 * written in assembly.
254 void proc_schedule(void)
256 /* This function must not have any "auto" variables, otherwise
257 * the compiler might put them on the stack of the process
258 * being switched out.
260 static Process *old_process;
261 static cpuflags_t flags;
263 /* Remember old process to save its context later */
264 old_process = CurrentProcess;
266 /* Poll on the ready queue for the first ready process */
267 DISABLE_IRQSAVE(flags);
268 while (!(CurrentProcess = (struct Process*)REMHEAD(&ProcReadyList)))
271 * Make sure we physically reenable interrupts here, no matter what
272 * the current task status is. This is important because if we
273 * are idle-spinning, we must allow interrupts, otherwise no
274 * process will ever wake up.
276 * \todo If there was a way to code sig_wait so that it does not
277 * disable interrupts while waiting, there would not be any
284 ENABLE_IRQRESTORE(flags);
286 /* Optimization: don't switch contexts when the active
287 * process has not changed.
289 if (CurrentProcess != old_process)
291 static cpustack_t* dummy;
293 #if CONFIG_KERN_PREEMPTIVE
294 /* Reset quantum for this process */
295 Quantum = CONFIG_KERN_QUANTUM;
298 /* Save context of old process and switch to new process. If there is no
299 * old process, we save the old stack pointer into a dummy variable that
300 * we ignore. In fact, this happens only when the old process has just
302 * TODO: Instead of physically clearing the process at exit time, a zombie
303 * list should be created.
305 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
308 /* This RET resumes the execution on the new process */
313 * Terminate the current process
318 /* The following code is BROKEN.
319 * We are freeing our own stack before entering proc_schedule()
320 * BAJO: A correct fix would be to rearrange the scheduler with
321 * an additional parameter which frees the old stack/process
322 * after a context switch.
324 if (CurrentProcess->flags & PF_FREESTACK)
325 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
326 heap_free(CurrentProcess);
329 #if (ARCH & ARCH_EMUL)
331 /* Reinsert process stack in free list */
332 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
333 - (DEF_STACKSIZE / sizeof(cpustack_t))));
335 /* NOTE: At this point the first two words of what used
336 * to be our stack contain a list node. From now on, we
337 * rely on the compiler not reading/writing the stack.
339 #endif /* ARCH_EMUL */
341 #if CONFIG_KERN_MONITOR
342 monitor_remove(CurrentProcess);
345 CurrentProcess = NULL;
352 * Co-operative context switch
354 void proc_switch(void)
356 /* Just like proc_schedule, this function must not have auto variables. */
357 static cpuflags_t flags;
359 DISABLE_IRQSAVE(flags);
360 SCHED_ENQUEUE(CurrentProcess);
361 ENABLE_IRQRESTORE(flags);
368 * Get the pointer to the current process
370 struct Process *proc_current(void)
372 return CurrentProcess;
376 * Get the pointer to the user data of the current process
378 IPTR proc_current_user_data(void)
380 return CurrentProcess->user_data;
383 #if 0 /* Simple testcase for the scheduler */
385 #include <drv/timer.h>
388 * Proc scheduling test subthread 1
390 static void NORETURN proc_test_thread1(void)
401 * Proc scheduling test subthread 2
403 static void NORETURN proc_test_thread2(void)
413 static cpustack_t proc_test_stack1[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
414 static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
417 * Proc scheduling test
419 void NORETURN proc_test(void)
421 proc_new(proc_test_thread1, NULL, sizeof(proc_test_stack1), proc_test_stack1);
422 proc_new(proc_test_thread2, NULL, sizeof(proc_test_stack2), proc_test_stack2);
423 kputs("Created tasks\n");
426 kdump(proc_test_stack1+sizeof(proc_test_stack1)-64, 64);
428 kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64);
432 kputs(">main task\n");