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.13 2004/08/29 21:58:53 bernie
21 *#* Include macros.h explicityl.
23 *#* Revision 1.11 2004/08/24 16:09:08 bernie
24 *#* Add missing header.
26 *#* Revision 1.10 2004/08/24 16:07:01 bernie
27 *#* Use kputs()/kputchar() when possible.
29 *#* Revision 1.9 2004/08/24 14:26:57 bernie
30 *#* monitor_debug_stacks(): Conditionally compile on CONFIG_KERN_MONITOR.
32 *#* Revision 1.8 2004/08/14 19:37:57 rasky
33 *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
35 *#* Revision 1.7 2004/08/02 20:20:29 aleph
36 *#* Merge from project_ks
38 *#* Revision 1.6 2004/07/30 14:24:16 rasky
39 *#* Task switching con salvataggio perfetto stato di interrupt (SR)
40 *#* Kernel monitor per dump informazioni su stack dei processi
42 *#* Revision 1.5 2004/07/14 14:18:09 rasky
43 *#* Merge da SC: Rimosso timer dentro il task, che รจ uno spreco di memoria per troppi task
45 *#* Revision 1.4 2004/07/13 19:21:28 aleph
46 *#* Avoid warning for unused arg when compiled without some CONFIG_KERN_xx options
48 *#* Revision 1.3 2004/06/06 18:37:57 bernie
49 *#* Rename event macros to look like regular functions.
51 *#* Revision 1.2 2004/06/03 11:27:09 bernie
52 *#* Add dual-license information.
54 *#* Revision 1.1 2004/05/23 17:27:00 bernie
55 *#* Import kern/ subdirectory.
64 #include <drv/kdebug.h>
65 #include <arch_config.h> /* ARCH_EMUL */
66 #include <macros.h> /* ABS() */
68 #include <string.h> /* memset() */
70 /*! CPU dependent context switching routines
71 * \note This function *MUST* preserve also the status of the interrupts.
73 extern void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
74 extern int asm_switch_version(void);
77 * The scheduer tracks ready and waiting processes
78 * by enqueuing them in these lists. A pointer to the currently
79 * running process is stored in the CurrentProcess pointer.
81 * NOTE: these variables are protected by DI/EI locking
83 REGISTER Process *CurrentProcess;
84 REGISTER List ProcReadyList;
87 #if CONFIG_KERN_PREEMPTIVE
89 * The time sharing scheduler forces a task switch when
90 * the current process has consumed its quantum.
96 /* In Win32 we must emulate stack on the real process stack */
97 #if (ARCH & ARCH_EMUL)
98 extern List StackFreeList;
101 /* The main process (the one that executes main()) */
102 struct Process MainProcess;
105 #if CONFIG_KERN_MONITOR
108 static void monitor_init(void)
110 INITLIST(&MonitorProcs);
113 static void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
115 proc->monitor.name = name;
116 proc->monitor.stack_base = stack_base;
117 proc->monitor.stack_size = stack_size;
119 ADDTAIL(&MonitorProcs, &proc->monitor.link);
122 static void monitor_remove(Process* proc)
124 REMOVE(&proc->monitor.link);
127 #define MONITOR_NODE_TO_PROCESS(node) \
128 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
130 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
138 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
140 if (CPU_STACK_GROWS_UPWARD)
150 if (*cur != CONFIG_KERN_STACKFILLCODE)
153 if (CPU_STACK_GROWS_UPWARD)
159 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
163 #if CONFIG_KERN_MONITOR
165 void monitor_debug_stacks(void)
170 if (ISLISTEMPTY(&MonitorProcs))
172 kputs("No stacks registered in the monitor\n");
176 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
181 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
182 p->monitor.link.succ;
183 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
185 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
186 kprintf("%-24s %04x %04x %4x %4x\n", p->monitor.name, (uint16_t)p, (uint16_t)p->monitor.stack_base, (uint16_t)p->monitor.stack_size, (uint16_t)free);
190 #endif /* CONFIG_KERN_MONITOR */
195 static void proc_init_struct(Process* proc)
197 /* Avoid warning for unused argument */
200 #if CONFIG_KERN_SIGNALS
212 INITLIST(&ProcReadyList);
214 #if CONFIG_KERN_MONITOR
218 /* We "promote" the current context into a real process. The only thing we have
219 * to do is create a PCB and make it current. We don't need to setup the stack
220 * pointer because it will be written the first time we switch to another process.
222 proc_init_struct(&MainProcess);
223 CurrentProcess = &MainProcess;
225 /* Make sure the assembly routine is up-to-date with us */
226 ASSERT(asm_switch_version() == 1);
231 * Create a new process, starting at the provided entry point.
233 * \return Process structure of new created process
234 * if successful, NULL otherwise.
236 struct Process *proc_new_with_name(UNUSED(const char*, name), void (*entry)(void), IPTR data, size_t stacksize, cpustack_t *stack_base)
241 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
243 bool free_stack = false;
246 #if (ARCH & ARCH_EMUL)
247 /* Ignore stack provided by caller
248 * and use the large enough default instead
250 stack_base = (cpustack_t *)StackFreeList.head;
251 REMOVE((Node *)stack_base);
252 stacksize = DEF_STACKSIZE;
253 #elif CONFIG_KERN_HEAP
254 /* Did the caller provide a stack for us? */
257 /* Did the caller specify the desired stack size? */
259 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
261 /* Allocate stack dinamically */
262 if (!(stack_base = heap_alloc(stacksize)))
268 /* Stack must have been provided by the user */
273 #if CONFIG_KERN_MONITOR
274 /* Fill-in the stack with a special marker to help debugging */
275 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
278 /* Initialize the process control block */
279 if (CPU_STACK_GROWS_UPWARD)
281 proc = (Process*)stack_base;
282 proc->stack = stack_base + proc_size_words;
283 if (CPU_SP_ON_EMPTY_SLOT)
288 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
289 proc->stack = (cpustack_t*)proc;
290 if (CPU_SP_ON_EMPTY_SLOT)
294 proc_init_struct(proc);
295 proc->user_data = data;
298 proc->stack_base = stack_base;
299 proc->stack_size = stack_size;
301 proc->flags |= PF_FREESTACK;
304 /* Initialize process stack frame */
305 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
306 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
308 /* Push a clean set of CPU registers for asm_switch_context() */
309 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
310 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
312 /* Add to ready list */
313 DISABLE_IRQSAVE(flags);
315 ENABLE_IRQRESTORE(flags);
317 #if CONFIG_KERN_MONITOR
318 monitor_add(proc, name, stack_base, stacksize);
326 * System scheduler: pass CPU control to the next process in
329 * Saving and restoring the context on the stack is done
330 * by a CPU-dependent support routine which must usually be
331 * written in assembly.
333 void proc_schedule(void)
335 /* This function must not have any "auto" variables, otherwise
336 * the compiler might put them on the stack of the process
337 * being switched out.
339 static Process *old_process;
340 static cpuflags_t flags;
342 /* Remember old process to save its context later */
343 old_process = CurrentProcess;
345 /* Poll on the ready queue for the first ready process */
346 DISABLE_IRQSAVE(flags);
347 while (!(CurrentProcess = (struct Process*)REMHEAD(&ProcReadyList)))
350 * Make sure we physically reenable interrupts here, no matter what
351 * the current task status is. This is important because if we
352 * are idle-spinning, we must allow interrupts, otherwise no
353 * process will ever wake up.
355 * \todo If there was a way to code sig_wait so that it does not
356 * disable interrupts while waiting, there would not be any
363 ENABLE_IRQRESTORE(flags);
365 /* Optimization: don't switch contexts when the active
366 * process has not changed.
368 if (CurrentProcess != old_process)
370 static cpustack_t* dummy;
372 #if CONFIG_KERN_PREEMPTIVE
373 /* Reset quantum for this process */
374 Quantum = CONFIG_KERN_QUANTUM;
377 /* Save context of old process and switch to new process. If there is no
378 * old process, we save the old stack pointer into a dummy variable that
379 * we ignore. In fact, this happens only when the old process has just
381 * TODO: Instead of physically clearing the process at exit time, a zombie
382 * list should be created.
384 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
387 /* This RET resumes the execution on the new process */
392 * Terminate the current process
397 /* The following code is BROKEN.
398 * We are freeing our own stack before entering proc_schedule()
399 * BAJO: A correct fix would be to rearrange the scheduler with
400 * an additional parameter which frees the old stack/process
401 * after a context switch.
403 if (CurrentProcess->flags & PF_FREESTACK)
404 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
405 heap_free(CurrentProcess);
408 #if (ARCH & ARCH_EMUL)
410 /* Reinsert process stack in free list */
411 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
412 - (DEF_STACKSIZE / sizeof(cpustack_t))));
414 /* NOTE: At this point the first two words of what used
415 * to be our stack contain a list node. From now on, we
416 * rely on the compiler not reading/writing the stack.
418 #endif /* ARCH_EMUL */
420 #if CONFIG_KERN_MONITOR
421 monitor_remove(CurrentProcess);
424 CurrentProcess = NULL;
431 * Co-operative context switch
433 void proc_switch(void)
435 /* Just like proc_schedule, this function must not have auto variables. */
436 static cpuflags_t flags;
438 DISABLE_IRQSAVE(flags);
439 SCHED_ENQUEUE(CurrentProcess);
440 ENABLE_IRQRESTORE(flags);
447 * Get the pointer to the current process
449 struct Process *proc_current(void)
451 return CurrentProcess;
455 * Get the pointer to the user data of the current process
457 IPTR proc_current_user_data(void)
459 return CurrentProcess->user_data;
462 #if 0 /* Simple testcase for the scheduler */
465 * Proc scheduling test subthread 1
467 static void NORETURN proc_test_thread1(void)
478 * Proc scheduling test subthread 2
480 static void NORETURN proc_test_thread2(void)
490 static cpustack_t proc_test_stack1[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
491 static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
494 * Proc scheduling test
496 void NORETURN proc_test(void)
498 proc_new(proc_test_thread1, NULL, sizeof(proc_test_stack1), proc_test_stack1);
499 proc_new(proc_test_thread2, NULL, sizeof(proc_test_stack2), proc_test_stack2);
500 kputs("Created tasks\n");
503 kdump(proc_test_stack1+sizeof(proc_test_stack1)-64, 64);
505 kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64);
509 kputs(">main task\n");