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.4 2004/07/13 19:21:28 aleph
21 * Avoid warning for unused arg when compiled without some CONFIG_KERN_xx options
23 * Revision 1.3 2004/06/06 18:37:57 bernie
24 * Rename event macros to look like regular functions.
26 * Revision 1.2 2004/06/03 11:27:09 bernie
27 * Add dual-license information.
29 * Revision 1.1 2004/05/23 17:27:00 bernie
30 * Import kern/ subdirectory.
39 #include <drv/kdebug.h>
41 #include <string.h> /* memset() */
43 /* CPU dependent context switching routines */
44 extern void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
47 * The scheduer tracks ready and waiting processes
48 * by enqueuing them in these lists. A pointer to the currently
49 * running process is stored in the CurrentProcess pointer.
51 * NOTE: these variables are protected by DI/EI locking
53 REGISTER Process *CurrentProcess;
54 REGISTER List ProcReadyList;
56 #if CONFIG_KERN_PREEMPTIVE
58 * The time sharing scheduler forces a task switch when
59 * the current process has consumed its quantum.
65 /* In Win32 we must emulate stack on the real process stack */
66 #if (ARCH & ARCH_EMUL)
67 extern List StackFreeList;
70 /* The main process (the one that executes main()) */
71 struct Process MainProcess;
73 static void proc_init_struct(Process* proc)
75 /* Avoid warning for unused argument */
79 event_initSignal(&proc->proc_timer.expire, proc, SIG_SINGLE);
82 #if CONFIG_KERN_SIGNALS
93 INITLIST(&ProcReadyList);
95 /* We "promote" the current context into a real process. The only thing we have
96 to do is create a PCB and make it current. We don't need to setup the stack
97 pointer because it will be written the first time we switch to another process. */
98 proc_init_struct(&MainProcess);
99 CurrentProcess = &MainProcess;
104 * Create a new process, starting at the provided entry point.
106 * \return Process structure of new created process
107 * if successful, NULL otherwise.
109 Process *proc_new(void (*entry)(void), size_t stacksize, cpustack_t *stack_base)
113 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
115 bool free_stack = false;
118 #if (ARCH & ARCH_EMUL)
119 /* Ignore stack provided by caller
120 * and use the large enough default instead
122 stack_base = (cpustack_t *)StackFreeList.head;
123 REMOVE((Node *)stack_base);
124 stacksize = DEF_STACKSIZE;
125 #elif CONFIG_KERN_HEAP
126 /* Did the caller provide a stack for us? */
129 /* Did the caller specify the desired stack size? */
131 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
133 /* Allocate stack dinamically */
134 if (!(stack_base = heap_alloc(stacksize)))
140 /* Stack must have been provided by the user */
146 /* Fill-in the stack with a special marker to help debugging */
147 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
150 /* Initialize the process control block */
151 if (CPU_STACK_GROWS_UPWARD)
153 proc = (Process*)stack_base;
154 proc->stack = stack_base + proc_size_words;
155 if (CPU_SP_ON_EMPTY_SLOT)
160 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
161 proc->stack = (cpustack_t*)proc;
162 if (CPU_SP_ON_EMPTY_SLOT)
166 proc_init_struct(proc);
169 proc->stack_base = stack_base;
170 proc->stack_size = stack_size;
172 proc->flags |= PF_FREESTACK;
175 /* Initialize process stack frame */
176 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
177 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
179 /* Push a clean set of CPU registers for asm_switch_context() */
180 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
181 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
183 /* Add to ready list */
193 * System scheduler: pass CPU control to the next process in
196 * Saving and restoring the context on the stack is done
197 * by a CPU-dependent support routine which must usually be
198 * written in assembly.
200 void proc_schedule(void)
202 /* This function must not have any "auto" variables, otherwise
203 * the compiler might put them on the stack of the process
204 * being switched out.
206 static Process *old_process;
208 /* Remember old process to save its context later */
209 old_process = CurrentProcess;
210 CurrentProcess = NULL;
212 /* Poll on the ready queue for the first ready process
214 for(;;) /* forever */
216 /* Do CPU specific idle processing (ARGH, should be moved to the end of the loop!) */
220 if (!ISLISTEMPTY(&ProcReadyList))
222 /* Get process from ready list */
223 CurrentProcess = (Process *)ProcReadyList.head;
224 REMOVE((Node *)CurrentProcess);
231 /* Optimization: don't switch contexts when the active
232 * process has not changed.
234 if (CurrentProcess != old_process)
236 static cpustack_t* dummy;
238 #if CONFIG_KERN_PREEMPTIVE
239 /* Reset quantum for this process */
240 Quantum = CONFIG_KERN_QUANTUM;
243 /* Save context of old process and switch to new process. If there is no
244 * old process, we save the old stack pointer into a dummy variable that
245 * we ignore. In fact, this happens only when the old process has just
247 * TODO: Instead of physically clearing the process at exit time, a zombie
248 * list should be created.
250 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
253 /* This RET resumes the execution on the new process */
258 * Terminate the current process
263 /* The following code is BROKEN.
264 * We are freeing our own stack before entering proc_schedule()
265 * BAJO: A correct fix would be to rearrange the scheduler with
266 * an additional parameter which frees the old stack/process
267 * after a context switch.
269 if (CurrentProcess->flags & PF_FREESTACK)
270 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
271 heap_free(CurrentProcess);
274 #if (ARCH & ARCH_EMUL)
276 /* Reinsert process stack in free list */
277 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
278 - (DEF_STACKSIZE / sizeof(cpustack_t))));
280 /* NOTE: At this point the first two words of what used
281 * to be our stack contain a list node. From now on, we
282 * rely on the compiler not reading/writing the stack.
284 #endif /* ARCH_EMUL */
286 CurrentProcess = NULL;
293 * Co-operative context switch
295 void proc_switch(void)
298 SCHED_ENQUEUE(CurrentProcess);
305 * Get the pointer to the current process
307 struct Process *proc_current(void)
309 return CurrentProcess;
313 #if 0 /* Simple testcase for the scheduler */
316 * Proc scheduling test subthread 1
318 static void NORETURN proc_test_thread1(void)
329 * Proc scheduling test subthread 2
331 static void NORETURN proc_test_thread2(void)
341 static cpustack_t proc_test_stack1[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
342 static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
345 * Proc scheduling test
347 void NORETURN proc_test(void)
349 proc_new(proc_test_thread1, sizeof(proc_test_stack1), proc_test_stack1);
350 proc_new(proc_test_thread2, sizeof(proc_test_stack2), proc_test_stack2);
351 kputs("Created tasks\n");
354 kdump(proc_test_stack1+sizeof(proc_test_stack1)-64, 64);
356 kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64);
360 kputs(">main task\n");