4 * Copyright 2001,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999,2000,2001 Bernardo Innocenti <bernie@develer.com>
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.1 2004/05/23 17:27:00 bernie
21 * Import kern/ subdirectory.
30 #include <drv/kdebug.h>
32 #include <string.h> /* memset() */
34 /* CPU dependent context switching routines */
35 extern void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
38 * The scheduer tracks ready and waiting processes
39 * by enqueuing them in these lists. A pointer to the currently
40 * running process is stored in the CurrentProcess pointer.
42 * NOTE: these variables are protected by DI/EI locking
44 REGISTER Process *CurrentProcess;
45 REGISTER List ProcReadyList;
47 #if CONFIG_KERN_PREEMPTIVE
49 * The time sharing scheduler forces a task switch when
50 * the current process has consumed its quantum.
56 /* In Win32 we must emulate stack on the real process stack */
57 #if (ARCH & ARCH_EMUL)
58 extern List StackFreeList;
61 /* The main process (the one that executes main()) */
62 struct Process MainProcess;
64 static void proc_init_struct(Process* proc)
67 INITEVENT_SIG(&proc->proc_timer.expire, proc, SIG_SINGLE);
70 #if CONFIG_KERN_SIGNALS
81 INITLIST(&ProcReadyList);
83 /* We "promote" the current context into a real process. The only thing we have
84 to do is create a PCB and make it current. We don't need to setup the stack
85 pointer because it will be written the first time we switch to another process. */
86 proc_init_struct(&MainProcess);
87 CurrentProcess = &MainProcess;
92 * Create a new process, starting at the provided entry point.
94 * \return Process structure of new created process
95 * if successful, NULL otherwise.
97 Process *proc_new(void (*entry)(void), size_t stacksize, cpustack_t *stack_base)
101 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
103 bool free_stack = false;
106 #if (ARCH & ARCH_EMUL)
107 /* Ignore stack provided by caller
108 * and use the large enough default instead
110 stack_base = (cpustack_t *)StackFreeList.head;
111 REMOVE((Node *)stack_base);
112 stacksize = DEF_STACKSIZE;
113 #elif CONFIG_KERN_HEAP
114 /* Did the caller provide a stack for us? */
117 /* Did the caller specify the desired stack size? */
119 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
121 /* Allocate stack dinamically */
122 if (!(stack_base = heap_alloc(stacksize)))
128 /* Stack must have been provided by the user */
134 /* Fill-in the stack with a special marker to help debugging */
135 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
138 /* Initialize the process control block */
139 if (CPU_STACK_GROWS_UPWARD)
141 proc = (Process*)stack_base;
142 proc->stack = stack_base + proc_size_words;
143 if (CPU_SP_ON_EMPTY_SLOT)
148 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
149 proc->stack = (cpustack_t*)proc;
150 if (CPU_SP_ON_EMPTY_SLOT)
154 proc_init_struct(proc);
157 proc->stack_base = stack_base;
158 proc->stack_size = stack_size;
160 proc->flags |= PF_FREESTACK;
163 /* Initialize process stack frame */
164 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
165 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
167 /* Push a clean set of CPU registers for asm_switch_context() */
168 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
169 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
171 /* Add to ready list */
181 * System scheduler: pass CPU control to the next process in
184 * Saving and restoring the context on the stack is done
185 * by a CPU-dependent support routine which must usually be
186 * written in assembly.
188 void proc_schedule(void)
190 /* This function must not have any "auto" variables, otherwise
191 * the compiler might put them on the stack of the process
192 * being switched out.
194 static Process *old_process;
196 /* Remember old process to save its context later */
197 old_process = CurrentProcess;
198 CurrentProcess = NULL;
200 /* Poll on the ready queue for the first ready process
202 for(;;) /* forever */
204 /* Do CPU specific idle processing (ARGH, should be moved to the end of the loop!) */
208 if (!ISLISTEMPTY(&ProcReadyList))
210 /* Get process from ready list */
211 CurrentProcess = (Process *)ProcReadyList.head;
212 REMOVE((Node *)CurrentProcess);
219 /* Optimization: don't switch contexts when the active
220 * process has not changed.
222 if (CurrentProcess != old_process)
224 static cpustack_t* dummy;
226 #if CONFIG_KERN_PREEMPTIVE
227 /* Reset quantum for this process */
228 Quantum = CONFIG_KERN_QUANTUM;
231 /* Save context of old process and switch to new process. If there is no
232 * old process, we save the old stack pointer into a dummy variable that
233 * we ignore. In fact, this happens only when the old process has just
235 * TODO: Instead of physically clearing the process at exit time, a zombie
236 * list should be created.
238 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
241 /* This RET resumes the execution on the new process */
246 * Terminate the current process
251 /* The following code is BROKEN.
252 * We are freeing our own stack before entering proc_schedule()
253 * BAJO: A correct fix would be to rearrange the scheduler with
254 * an additional parameter which frees the old stack/process
255 * after a context switch.
257 if (CurrentProcess->flags & PF_FREESTACK)
258 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
259 heap_free(CurrentProcess);
262 #if (ARCH & ARCH_EMUL)
264 /* Reinsert process stack in free list */
265 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
266 - (DEF_STACKSIZE / sizeof(cpustack_t))));
268 /* NOTE: At this point the first two words of what used
269 * to be our stack contain a list node. From now on, we
270 * rely on the compiler not reading/writing the stack.
272 #endif /* ARCH_EMUL */
274 CurrentProcess = NULL;
281 * Co-operative context switch
283 void proc_switch(void)
286 SCHED_ENQUEUE(CurrentProcess);
293 * Get the pointer to the current process
295 struct Process *proc_current(void)
297 return CurrentProcess;
301 #if 0 /* Simple testcase for the scheduler */
304 * Proc scheduling test subthread 1
306 static void NORETURN proc_test_thread1(void)
317 * Proc scheduling test subthread 2
319 static void NORETURN proc_test_thread2(void)
329 static cpustack_t proc_test_stack1[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
330 static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
333 * Proc scheduling test
335 void NORETURN proc_test(void)
337 proc_new(proc_test_thread1, sizeof(proc_test_stack1), proc_test_stack1);
338 proc_new(proc_test_thread2, sizeof(proc_test_stack2), proc_test_stack2);
339 kputs("Created tasks\n");
342 kdump(proc_test_stack1+sizeof(proc_test_stack1)-64, 64);
344 kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64);
348 kputs(">main task\n");