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.10 2004/08/24 16:07:01 bernie
21 * Use kputs()/kputchar() when possible.
23 * Revision 1.9 2004/08/24 14:26:57 bernie
24 * monitor_debug_stacks(): Conditionally compile on CONFIG_KERN_MONITOR.
26 * Revision 1.8 2004/08/14 19:37:57 rasky
27 * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
29 * Revision 1.7 2004/08/02 20:20:29 aleph
30 * Merge from project_ks
32 * Revision 1.6 2004/07/30 14:24:16 rasky
33 * Task switching con salvataggio perfetto stato di interrupt (SR)
34 * Kernel monitor per dump informazioni su stack dei processi
36 * Revision 1.5 2004/07/14 14:18:09 rasky
37 * Merge da SC: Rimosso timer dentro il task, che รจ uno spreco di memoria per troppi task
39 * Revision 1.4 2004/07/13 19:21:28 aleph
40 * Avoid warning for unused arg when compiled without some CONFIG_KERN_xx options
42 * Revision 1.3 2004/06/06 18:37:57 bernie
43 * Rename event macros to look like regular functions.
45 * Revision 1.2 2004/06/03 11:27:09 bernie
46 * Add dual-license information.
48 * Revision 1.1 2004/05/23 17:27:00 bernie
49 * Import kern/ subdirectory.
58 #include <drv/kdebug.h>
60 #include <string.h> /* memset() */
62 /*! CPU dependent context switching routines
63 * \note This function *MUST* preserve also the status of the interrupts.
65 extern void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
66 extern int asm_switch_version(void);
69 * The scheduer tracks ready and waiting processes
70 * by enqueuing them in these lists. A pointer to the currently
71 * running process is stored in the CurrentProcess pointer.
73 * NOTE: these variables are protected by DI/EI locking
75 REGISTER Process *CurrentProcess;
76 REGISTER List ProcReadyList;
79 #if CONFIG_KERN_PREEMPTIVE
81 * The time sharing scheduler forces a task switch when
82 * the current process has consumed its quantum.
88 /* In Win32 we must emulate stack on the real process stack */
89 #if (ARCH & ARCH_EMUL)
90 extern List StackFreeList;
93 /* The main process (the one that executes main()) */
94 struct Process MainProcess;
97 #if CONFIG_KERN_MONITOR
100 static void monitor_init(void)
102 INITLIST(&MonitorProcs);
105 static void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
107 proc->monitor.name = name;
108 proc->monitor.stack_base = stack_base;
109 proc->monitor.stack_size = stack_size;
111 ADDTAIL(&MonitorProcs, &proc->monitor.link);
114 static void monitor_remove(Process* proc)
116 REMOVE(&proc->monitor.link);
119 #define MONITOR_NODE_TO_PROCESS(node) \
120 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
122 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
130 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
132 if (CPU_STACK_GROWS_UPWARD)
142 if (*cur != CONFIG_KERN_STACKFILLCODE)
145 if (CPU_STACK_GROWS_UPWARD)
151 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
155 #if CONFIG_KERN_MONITOR
157 void monitor_debug_stacks(void)
162 if (ISLISTEMPTY(&MonitorProcs))
164 kputs("No stacks registered in the monitor\n");
168 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
173 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
174 p->monitor.link.succ;
175 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
177 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
178 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);
182 #endif /* CONFIG_KERN_MONITOR */
187 static void proc_init_struct(Process* proc)
189 /* Avoid warning for unused argument */
192 #if CONFIG_KERN_SIGNALS
204 INITLIST(&ProcReadyList);
206 #if CONFIG_KERN_MONITOR
210 /* We "promote" the current context into a real process. The only thing we have
211 * to do is create a PCB and make it current. We don't need to setup the stack
212 * pointer because it will be written the first time we switch to another process.
214 proc_init_struct(&MainProcess);
215 CurrentProcess = &MainProcess;
217 /* Make sure the assembly routine is up-to-date with us */
218 ASSERT(asm_switch_version() == 1);
223 * Create a new process, starting at the provided entry point.
225 * \return Process structure of new created process
226 * if successful, NULL otherwise.
228 struct Process *proc_new_with_name(UNUSED(const char*, name), void (*entry)(void), IPTR data, size_t stacksize, cpustack_t *stack_base)
233 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
235 bool free_stack = false;
238 #if (ARCH & ARCH_EMUL)
239 /* Ignore stack provided by caller
240 * and use the large enough default instead
242 stack_base = (cpustack_t *)StackFreeList.head;
243 REMOVE((Node *)stack_base);
244 stacksize = DEF_STACKSIZE;
245 #elif CONFIG_KERN_HEAP
246 /* Did the caller provide a stack for us? */
249 /* Did the caller specify the desired stack size? */
251 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
253 /* Allocate stack dinamically */
254 if (!(stack_base = heap_alloc(stacksize)))
260 /* Stack must have been provided by the user */
265 #if CONFIG_KERN_MONITOR
266 /* Fill-in the stack with a special marker to help debugging */
267 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
270 /* Initialize the process control block */
271 if (CPU_STACK_GROWS_UPWARD)
273 proc = (Process*)stack_base;
274 proc->stack = stack_base + proc_size_words;
275 if (CPU_SP_ON_EMPTY_SLOT)
280 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
281 proc->stack = (cpustack_t*)proc;
282 if (CPU_SP_ON_EMPTY_SLOT)
286 proc_init_struct(proc);
287 proc->user_data = data;
290 proc->stack_base = stack_base;
291 proc->stack_size = stack_size;
293 proc->flags |= PF_FREESTACK;
296 /* Initialize process stack frame */
297 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
298 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
300 /* Push a clean set of CPU registers for asm_switch_context() */
301 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
302 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
304 /* Add to ready list */
305 DISABLE_IRQSAVE(flags);
307 ENABLE_IRQRESTORE(flags);
309 #if CONFIG_KERN_MONITOR
310 monitor_add(proc, name, stack_base, stacksize);
318 * System scheduler: pass CPU control to the next process in
321 * Saving and restoring the context on the stack is done
322 * by a CPU-dependent support routine which must usually be
323 * written in assembly.
325 void proc_schedule(void)
327 /* This function must not have any "auto" variables, otherwise
328 * the compiler might put them on the stack of the process
329 * being switched out.
331 static Process *old_process;
332 static cpuflags_t flags;
334 /* Remember old process to save its context later */
335 old_process = CurrentProcess;
337 /* Poll on the ready queue for the first ready process */
338 DISABLE_IRQSAVE(flags);
339 while (!(CurrentProcess = (struct Process*)REMHEAD(&ProcReadyList)))
342 * Make sure we physically reenable interrupts here, no matter what
343 * the current task status is. This is important because if we
344 * are idle-spinning, we must allow interrupts, otherwise no
345 * process will ever wake up.
347 * \todo If there was a way to code sig_wait so that it does not
348 * disable interrupts while waiting, there would not be any
355 ENABLE_IRQRESTORE(flags);
357 /* Optimization: don't switch contexts when the active
358 * process has not changed.
360 if (CurrentProcess != old_process)
362 static cpustack_t* dummy;
364 #if CONFIG_KERN_PREEMPTIVE
365 /* Reset quantum for this process */
366 Quantum = CONFIG_KERN_QUANTUM;
369 /* Save context of old process and switch to new process. If there is no
370 * old process, we save the old stack pointer into a dummy variable that
371 * we ignore. In fact, this happens only when the old process has just
373 * TODO: Instead of physically clearing the process at exit time, a zombie
374 * list should be created.
376 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
379 /* This RET resumes the execution on the new process */
384 * Terminate the current process
389 /* The following code is BROKEN.
390 * We are freeing our own stack before entering proc_schedule()
391 * BAJO: A correct fix would be to rearrange the scheduler with
392 * an additional parameter which frees the old stack/process
393 * after a context switch.
395 if (CurrentProcess->flags & PF_FREESTACK)
396 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
397 heap_free(CurrentProcess);
400 #if (ARCH & ARCH_EMUL)
402 /* Reinsert process stack in free list */
403 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
404 - (DEF_STACKSIZE / sizeof(cpustack_t))));
406 /* NOTE: At this point the first two words of what used
407 * to be our stack contain a list node. From now on, we
408 * rely on the compiler not reading/writing the stack.
410 #endif /* ARCH_EMUL */
412 #if CONFIG_KERN_MONITOR
413 monitor_remove(CurrentProcess);
416 CurrentProcess = NULL;
423 * Co-operative context switch
425 void proc_switch(void)
427 /* Just like proc_schedule, this function must not have auto variables. */
428 static cpuflags_t flags;
430 DISABLE_IRQSAVE(flags);
431 SCHED_ENQUEUE(CurrentProcess);
432 ENABLE_IRQRESTORE(flags);
439 * Get the pointer to the current process
441 struct Process *proc_current(void)
443 return CurrentProcess;
447 * Get the pointer to the user data of the current process
449 IPTR proc_current_user_data(void)
451 return CurrentProcess->user_data;
454 #if 0 /* Simple testcase for the scheduler */
457 * Proc scheduling test subthread 1
459 static void NORETURN proc_test_thread1(void)
470 * Proc scheduling test subthread 2
472 static void NORETURN proc_test_thread2(void)
482 static cpustack_t proc_test_stack1[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
483 static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
486 * Proc scheduling test
488 void NORETURN proc_test(void)
490 proc_new(proc_test_thread1, NULL, sizeof(proc_test_stack1), proc_test_stack1);
491 proc_new(proc_test_thread2, NULL, sizeof(proc_test_stack2), proc_test_stack2);
492 kputs("Created tasks\n");
495 kdump(proc_test_stack1+sizeof(proc_test_stack1)-64, 64);
497 kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64);
501 kputs(">main task\n");