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.11 2004/08/24 16:09:08 bernie
23 * Revision 1.10 2004/08/24 16:07:01 bernie
24 * Use kputs()/kputchar() when possible.
26 * Revision 1.9 2004/08/24 14:26:57 bernie
27 * monitor_debug_stacks(): Conditionally compile on CONFIG_KERN_MONITOR.
29 * Revision 1.8 2004/08/14 19:37:57 rasky
30 * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
32 * Revision 1.7 2004/08/02 20:20:29 aleph
33 * Merge from project_ks
35 * Revision 1.6 2004/07/30 14:24:16 rasky
36 * Task switching con salvataggio perfetto stato di interrupt (SR)
37 * Kernel monitor per dump informazioni su stack dei processi
39 * Revision 1.5 2004/07/14 14:18:09 rasky
40 * Merge da SC: Rimosso timer dentro il task, che รจ uno spreco di memoria per troppi task
42 * Revision 1.4 2004/07/13 19:21:28 aleph
43 * Avoid warning for unused arg when compiled without some CONFIG_KERN_xx options
45 * Revision 1.3 2004/06/06 18:37:57 bernie
46 * Rename event macros to look like regular functions.
48 * Revision 1.2 2004/06/03 11:27:09 bernie
49 * Add dual-license information.
51 * Revision 1.1 2004/05/23 17:27:00 bernie
52 * Import kern/ subdirectory.
61 #include <drv/kdebug.h>
62 #include <arch_config.h> /* ARCH_EMUL */
64 #include <string.h> /* memset() */
66 /*! CPU dependent context switching routines
67 * \note This function *MUST* preserve also the status of the interrupts.
69 extern void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
70 extern int asm_switch_version(void);
73 * The scheduer tracks ready and waiting processes
74 * by enqueuing them in these lists. A pointer to the currently
75 * running process is stored in the CurrentProcess pointer.
77 * NOTE: these variables are protected by DI/EI locking
79 REGISTER Process *CurrentProcess;
80 REGISTER List ProcReadyList;
83 #if CONFIG_KERN_PREEMPTIVE
85 * The time sharing scheduler forces a task switch when
86 * the current process has consumed its quantum.
92 /* In Win32 we must emulate stack on the real process stack */
93 #if (ARCH & ARCH_EMUL)
94 extern List StackFreeList;
97 /* The main process (the one that executes main()) */
98 struct Process MainProcess;
101 #if CONFIG_KERN_MONITOR
104 static void monitor_init(void)
106 INITLIST(&MonitorProcs);
109 static void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
111 proc->monitor.name = name;
112 proc->monitor.stack_base = stack_base;
113 proc->monitor.stack_size = stack_size;
115 ADDTAIL(&MonitorProcs, &proc->monitor.link);
118 static void monitor_remove(Process* proc)
120 REMOVE(&proc->monitor.link);
123 #define MONITOR_NODE_TO_PROCESS(node) \
124 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
126 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
134 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
136 if (CPU_STACK_GROWS_UPWARD)
146 if (*cur != CONFIG_KERN_STACKFILLCODE)
149 if (CPU_STACK_GROWS_UPWARD)
155 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
159 #if CONFIG_KERN_MONITOR
161 void monitor_debug_stacks(void)
166 if (ISLISTEMPTY(&MonitorProcs))
168 kputs("No stacks registered in the monitor\n");
172 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
177 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
178 p->monitor.link.succ;
179 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
181 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
182 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);
186 #endif /* CONFIG_KERN_MONITOR */
191 static void proc_init_struct(Process* proc)
193 /* Avoid warning for unused argument */
196 #if CONFIG_KERN_SIGNALS
208 INITLIST(&ProcReadyList);
210 #if CONFIG_KERN_MONITOR
214 /* We "promote" the current context into a real process. The only thing we have
215 * to do is create a PCB and make it current. We don't need to setup the stack
216 * pointer because it will be written the first time we switch to another process.
218 proc_init_struct(&MainProcess);
219 CurrentProcess = &MainProcess;
221 /* Make sure the assembly routine is up-to-date with us */
222 ASSERT(asm_switch_version() == 1);
227 * Create a new process, starting at the provided entry point.
229 * \return Process structure of new created process
230 * if successful, NULL otherwise.
232 struct Process *proc_new_with_name(UNUSED(const char*, name), void (*entry)(void), IPTR data, size_t stacksize, cpustack_t *stack_base)
237 size_t proc_size_words = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
239 bool free_stack = false;
242 #if (ARCH & ARCH_EMUL)
243 /* Ignore stack provided by caller
244 * and use the large enough default instead
246 stack_base = (cpustack_t *)StackFreeList.head;
247 REMOVE((Node *)stack_base);
248 stacksize = DEF_STACKSIZE;
249 #elif CONFIG_KERN_HEAP
250 /* Did the caller provide a stack for us? */
253 /* Did the caller specify the desired stack size? */
255 stacksize = CONFIG_KERN_DEFSTACKSIZE + sizeof(Process);
257 /* Allocate stack dinamically */
258 if (!(stack_base = heap_alloc(stacksize)))
264 /* Stack must have been provided by the user */
269 #if CONFIG_KERN_MONITOR
270 /* Fill-in the stack with a special marker to help debugging */
271 memset(stack_base, CONFIG_KERN_STACKFILLCODE, stacksize / sizeof(cpustack_t));
274 /* Initialize the process control block */
275 if (CPU_STACK_GROWS_UPWARD)
277 proc = (Process*)stack_base;
278 proc->stack = stack_base + proc_size_words;
279 if (CPU_SP_ON_EMPTY_SLOT)
284 proc = (Process*)(stack_base + stacksize / sizeof(cpustack_t) - proc_size_words);
285 proc->stack = (cpustack_t*)proc;
286 if (CPU_SP_ON_EMPTY_SLOT)
290 proc_init_struct(proc);
291 proc->user_data = data;
294 proc->stack_base = stack_base;
295 proc->stack_size = stack_size;
297 proc->flags |= PF_FREESTACK;
300 /* Initialize process stack frame */
301 CPU_PUSH_CALL_CONTEXT(proc->stack, proc_exit);
302 CPU_PUSH_CALL_CONTEXT(proc->stack, entry);
304 /* Push a clean set of CPU registers for asm_switch_context() */
305 for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
306 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
308 /* Add to ready list */
309 DISABLE_IRQSAVE(flags);
311 ENABLE_IRQRESTORE(flags);
313 #if CONFIG_KERN_MONITOR
314 monitor_add(proc, name, stack_base, stacksize);
322 * System scheduler: pass CPU control to the next process in
325 * Saving and restoring the context on the stack is done
326 * by a CPU-dependent support routine which must usually be
327 * written in assembly.
329 void proc_schedule(void)
331 /* This function must not have any "auto" variables, otherwise
332 * the compiler might put them on the stack of the process
333 * being switched out.
335 static Process *old_process;
336 static cpuflags_t flags;
338 /* Remember old process to save its context later */
339 old_process = CurrentProcess;
341 /* Poll on the ready queue for the first ready process */
342 DISABLE_IRQSAVE(flags);
343 while (!(CurrentProcess = (struct Process*)REMHEAD(&ProcReadyList)))
346 * Make sure we physically reenable interrupts here, no matter what
347 * the current task status is. This is important because if we
348 * are idle-spinning, we must allow interrupts, otherwise no
349 * process will ever wake up.
351 * \todo If there was a way to code sig_wait so that it does not
352 * disable interrupts while waiting, there would not be any
359 ENABLE_IRQRESTORE(flags);
361 /* Optimization: don't switch contexts when the active
362 * process has not changed.
364 if (CurrentProcess != old_process)
366 static cpustack_t* dummy;
368 #if CONFIG_KERN_PREEMPTIVE
369 /* Reset quantum for this process */
370 Quantum = CONFIG_KERN_QUANTUM;
373 /* Save context of old process and switch to new process. If there is no
374 * old process, we save the old stack pointer into a dummy variable that
375 * we ignore. In fact, this happens only when the old process has just
377 * TODO: Instead of physically clearing the process at exit time, a zombie
378 * list should be created.
380 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
383 /* This RET resumes the execution on the new process */
388 * Terminate the current process
393 /* The following code is BROKEN.
394 * We are freeing our own stack before entering proc_schedule()
395 * BAJO: A correct fix would be to rearrange the scheduler with
396 * an additional parameter which frees the old stack/process
397 * after a context switch.
399 if (CurrentProcess->flags & PF_FREESTACK)
400 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
401 heap_free(CurrentProcess);
404 #if (ARCH & ARCH_EMUL)
406 /* Reinsert process stack in free list */
407 ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
408 - (DEF_STACKSIZE / sizeof(cpustack_t))));
410 /* NOTE: At this point the first two words of what used
411 * to be our stack contain a list node. From now on, we
412 * rely on the compiler not reading/writing the stack.
414 #endif /* ARCH_EMUL */
416 #if CONFIG_KERN_MONITOR
417 monitor_remove(CurrentProcess);
420 CurrentProcess = NULL;
427 * Co-operative context switch
429 void proc_switch(void)
431 /* Just like proc_schedule, this function must not have auto variables. */
432 static cpuflags_t flags;
434 DISABLE_IRQSAVE(flags);
435 SCHED_ENQUEUE(CurrentProcess);
436 ENABLE_IRQRESTORE(flags);
443 * Get the pointer to the current process
445 struct Process *proc_current(void)
447 return CurrentProcess;
451 * Get the pointer to the user data of the current process
453 IPTR proc_current_user_data(void)
455 return CurrentProcess->user_data;
458 #if 0 /* Simple testcase for the scheduler */
461 * Proc scheduling test subthread 1
463 static void NORETURN proc_test_thread1(void)
474 * Proc scheduling test subthread 2
476 static void NORETURN proc_test_thread2(void)
486 static cpustack_t proc_test_stack1[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
487 static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
490 * Proc scheduling test
492 void NORETURN proc_test(void)
494 proc_new(proc_test_thread1, NULL, sizeof(proc_test_stack1), proc_test_stack1);
495 proc_new(proc_test_thread2, NULL, sizeof(proc_test_stack2), proc_test_stack2);
496 kputs("Created tasks\n");
499 kdump(proc_test_stack1+sizeof(proc_test_stack1)-64, 64);
501 kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64);
505 kputs(">main task\n");