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.3 2004/06/06 18:37:57 bernie
21 * Rename event macros to look like regular functions.
23 * Revision 1.2 2004/06/03 11:27:09 bernie
24 * Add dual-license information.
26 * Revision 1.1 2004/05/23 17:27:00 bernie
27 * Import kern/ subdirectory.
36 #include <drv/kdebug.h>
38 #include <string.h> /* memset() */
40 /* CPU dependent context switching routines */
41 extern void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
44 * The scheduer tracks ready and waiting processes
45 * by enqueuing them in these lists. A pointer to the currently
46 * running process is stored in the CurrentProcess pointer.
48 * NOTE: these variables are protected by DI/EI locking
50 REGISTER Process *CurrentProcess;
51 REGISTER List ProcReadyList;
53 #if CONFIG_KERN_PREEMPTIVE
55 * The time sharing scheduler forces a task switch when
56 * the current process has consumed its quantum.
62 /* In Win32 we must emulate stack on the real process stack */
63 #if (ARCH & ARCH_EMUL)
64 extern List StackFreeList;
67 /* The main process (the one that executes main()) */
68 struct Process MainProcess;
70 static void proc_init_struct(Process* proc)
73 event_initSignal(&proc->proc_timer.expire, proc, SIG_SINGLE);
76 #if CONFIG_KERN_SIGNALS
87 INITLIST(&ProcReadyList);
89 /* We "promote" the current context into a real process. The only thing we have
90 to do is create a PCB and make it current. We don't need to setup the stack
91 pointer because it will be written the first time we switch to another process. */
92 proc_init_struct(&MainProcess);
93 CurrentProcess = &MainProcess;
98 * Create a new process, starting at the provided entry point.
100 * \return Process structure of new created process
101 * if successful, NULL otherwise.
103 Process *proc_new(void (*entry)(void), size_t stacksize, cpustack_t *stack_base)
107 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
109 bool free_stack = false;
112 #if (ARCH & ARCH_EMUL)
113 /* Ignore stack provided by caller
114 * and use the large enough default instead
116 stack_base = (cpustack_t *)StackFreeList.head;
117 REMOVE((Node *)stack_base);
118 stacksize = DEF_STACKSIZE;
119 #elif CONFIG_KERN_HEAP
120 /* Did the caller provide a stack for us? */
123 /* Did the caller specify the desired stack size? */
125 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
127 /* Allocate stack dinamically */
128 if (!(stack_base = heap_alloc(stacksize)))
134 /* Stack must have been provided by the user */
140 /* Fill-in the stack with a special marker to help debugging */
141 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
144 /* Initialize the process control block */
145 if (CPU_STACK_GROWS_UPWARD)
147 proc = (Process*)stack_base;
148 proc->stack = stack_base + proc_size_words;
149 if (CPU_SP_ON_EMPTY_SLOT)
154 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
155 proc->stack = (cpustack_t*)proc;
156 if (CPU_SP_ON_EMPTY_SLOT)
160 proc_init_struct(proc);
163 proc->stack_base = stack_base;
164 proc->stack_size = stack_size;
166 proc->flags |= PF_FREESTACK;
169 /* Initialize process stack frame */
170 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
171 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
173 /* Push a clean set of CPU registers for asm_switch_context() */
174 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
175 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
177 /* Add to ready list */
187 * System scheduler: pass CPU control to the next process in
190 * Saving and restoring the context on the stack is done
191 * by a CPU-dependent support routine which must usually be
192 * written in assembly.
194 void proc_schedule(void)
196 /* This function must not have any "auto" variables, otherwise
197 * the compiler might put them on the stack of the process
198 * being switched out.
200 static Process *old_process;
202 /* Remember old process to save its context later */
203 old_process = CurrentProcess;
204 CurrentProcess = NULL;
206 /* Poll on the ready queue for the first ready process
208 for(;;) /* forever */
210 /* Do CPU specific idle processing (ARGH, should be moved to the end of the loop!) */
214 if (!ISLISTEMPTY(&ProcReadyList))
216 /* Get process from ready list */
217 CurrentProcess = (Process *)ProcReadyList.head;
218 REMOVE((Node *)CurrentProcess);
225 /* Optimization: don't switch contexts when the active
226 * process has not changed.
228 if (CurrentProcess != old_process)
230 static cpustack_t* dummy;
232 #if CONFIG_KERN_PREEMPTIVE
233 /* Reset quantum for this process */
234 Quantum = CONFIG_KERN_QUANTUM;
237 /* Save context of old process and switch to new process. If there is no
238 * old process, we save the old stack pointer into a dummy variable that
239 * we ignore. In fact, this happens only when the old process has just
241 * TODO: Instead of physically clearing the process at exit time, a zombie
242 * list should be created.
244 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
247 /* This RET resumes the execution on the new process */
252 * Terminate the current process
257 /* The following code is BROKEN.
258 * We are freeing our own stack before entering proc_schedule()
259 * BAJO: A correct fix would be to rearrange the scheduler with
260 * an additional parameter which frees the old stack/process
261 * after a context switch.
263 if (CurrentProcess->flags & PF_FREESTACK)
264 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
265 heap_free(CurrentProcess);
268 #if (ARCH & ARCH_EMUL)
270 /* Reinsert process stack in free list */
271 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
272 - (DEF_STACKSIZE / sizeof(cpustack_t))));
274 /* NOTE: At this point the first two words of what used
275 * to be our stack contain a list node. From now on, we
276 * rely on the compiler not reading/writing the stack.
278 #endif /* ARCH_EMUL */
280 CurrentProcess = NULL;
287 * Co-operative context switch
289 void proc_switch(void)
292 SCHED_ENQUEUE(CurrentProcess);
299 * Get the pointer to the current process
301 struct Process *proc_current(void)
303 return CurrentProcess;
307 #if 0 /* Simple testcase for the scheduler */
310 * Proc scheduling test subthread 1
312 static void NORETURN proc_test_thread1(void)
323 * Proc scheduling test subthread 2
325 static void NORETURN proc_test_thread2(void)
335 static cpustack_t proc_test_stack1[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
336 static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
339 * Proc scheduling test
341 void NORETURN proc_test(void)
343 proc_new(proc_test_thread1, sizeof(proc_test_stack1), proc_test_stack1);
344 proc_new(proc_test_thread2, sizeof(proc_test_stack2), proc_test_stack2);
345 kputs("Created tasks\n");
348 kdump(proc_test_stack1+sizeof(proc_test_stack1)-64, 64);
350 kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64);
354 kputs(">main task\n");