Parametric scheduler approach.
[bertos.git] / bertos / kern / proc.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Simple cooperative multitasking scheduler.
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  * \author Stefano Fedrigo <aleph@develer.com>
37  */
38
39 #include "proc_p.h"
40 #include "proc.h"
41
42 #include "cfg/cfg_proc.h"
43 #define LOG_LEVEL KERN_LOG_LEVEL
44 #define LOG_FORMAT KERN_LOG_FORMAT
45 #include <cfg/log.h>
46
47 #include "cfg/cfg_monitor.h"
48 #include <cfg/macros.h>    // ROUND_UP2
49 #include <cfg/module.h>
50 #include <cfg/depend.h>    // CONFIG_DEPEND()
51
52 #include <cpu/irq.h>
53 #include <cpu/types.h>
54 #include <cpu/attr.h>
55 #include <cpu/frame.h>
56
57 #if CONFIG_KERN_HEAP
58         #include <struct/heap.h>
59 #endif
60
61 #include <string.h>           /* memset() */
62
63 #define PROC_SIZE_WORDS (ROUND_UP2(sizeof(Process), sizeof(cpu_stack_t)) / sizeof(cpu_stack_t))
64
65 /*
66  * The scheduer tracks ready processes by enqueuing them in the
67  * ready list.
68  *
69  * \note Access to the list must occur while interrupts are disabled.
70  */
71 REGISTER List proc_ready_list;
72
73 /*
74  * Holds a pointer to the TCB of the currently running process.
75  *
76  * \note User applications should use proc_current() to retrieve this value.
77  */
78 REGISTER Process *current_process;
79
80 /** The main process (the one that executes main()). */
81 static struct Process main_process;
82
83 #if CONFIG_KERN_HEAP
84
85 /**
86  * Local heap dedicated to allocate the memory used by the processes.
87  */
88 static HEAP_DEFINE_BUF(heap_buf, CONFIG_KERN_HEAP_SIZE);
89 static Heap proc_heap;
90
91 /*
92  * Keep track of zombie processes (processes that are exiting and need to
93  * release some resources).
94  *
95  * \note Access to the list must occur while kernel preemption is disabled.
96  */
97 static List zombie_list;
98
99 #endif /* CONFIG_KERN_HEAP */
100
101 static void proc_initStruct(Process *proc)
102 {
103         /* Avoid warning for unused argument. */
104         (void)proc;
105
106 #if CONFIG_KERN_SIGNALS
107         proc->sig_recv = 0;
108         proc->sig_wait = 0;
109 #endif
110
111 #if CONFIG_KERN_HEAP
112         proc->flags = 0;
113 #endif
114
115 #if CONFIG_KERN_PRI
116         proc->link.pri = 0;
117 #endif
118 }
119
120 MOD_DEFINE(proc);
121
122 void proc_init(void)
123 {
124         LIST_INIT(&proc_ready_list);
125
126 #if CONFIG_KERN_HEAP
127         LIST_INIT(&zombie_list);
128         heap_init(&proc_heap, heap_buf, sizeof(heap_buf));
129 #endif
130         /*
131          * We "promote" the current context into a real process. The only thing we have
132          * to do is create a PCB and make it current. We don't need to setup the stack
133          * pointer because it will be written the first time we switch to another process.
134          */
135         proc_initStruct(&main_process);
136         current_process = &main_process;
137
138 #if CONFIG_KERN_MONITOR
139         monitor_init();
140         monitor_add(current_process, "main");
141 #endif
142         proc_schedInit();
143
144         MOD_INIT(proc);
145 }
146
147
148 #if CONFIG_KERN_HEAP
149
150 /**
151  * Free all the resources of all zombie processes previously added to the zombie
152  * list.
153  */
154 static void proc_freeZombies(void)
155 {
156         Process *proc;
157
158         while (1)
159         {
160                 PROC_ATOMIC(proc = (Process *)list_remHead(&zombie_list));
161                 if (proc == NULL)
162                         return;
163
164                 if (proc->flags & PF_FREESTACK)
165                 {
166                         PROC_ATOMIC(heap_freemem(&proc_heap, proc->stack_base,
167                                 proc->stack_size + PROC_SIZE_WORDS * sizeof(cpu_stack_t)));
168                 }
169         }
170 }
171
172 /**
173  * Enqueue a process in the zombie list.
174  */
175 static void proc_addZombie(Process *proc)
176 {
177         Node *node;
178 #if CONFIG_KERN_PREEMPT
179         ASSERT(!proc_preemptAllowed());
180 #endif
181
182 #if CONFIG_KERN_PRI
183         node = &(proc)->link.link;
184 #else
185         node = &(proc)->link;
186 #endif
187         LIST_ASSERT_VALID(&zombie_list);
188         ADDTAIL(&zombie_list, node);
189 }
190
191 #endif /* CONFIG_KERN_HEAP */
192
193 /**
194  * Create a new process, starting at the provided entry point.
195  *
196  *
197  * \note The function
198  * \code
199  * proc_new(entry, data, stacksize, stack)
200  * \endcode
201  * is a more convenient way to create a process, as you don't have to specify
202  * the name.
203  *
204  * \return Process structure of new created process
205  *         if successful, NULL otherwise.
206  */
207 struct Process *proc_new_with_name(UNUSED_ARG(const char *, name), void (*entry)(void), iptr_t data, size_t stack_size, cpu_stack_t *stack_base)
208 {
209         Process *proc;
210         LOG_INFO("name=%s", name);
211 #if CONFIG_KERN_HEAP
212         bool free_stack = false;
213
214         /*
215          * Free up resources of a zombie process.
216          *
217          * We're implementing a kind of lazy garbage collector here for
218          * efficiency reasons: we can avoid to introduce overhead into another
219          * kernel task dedicated to free up resources (e.g., idle) and we're
220          * not introducing any overhead into the scheduler after a context
221          * switch (that would be *very* bad, because the scheduler runs with
222          * IRQ disabled).
223          *
224          * In this way we are able to release the memory of the zombie tasks
225          * without disabling IRQs and without introducing any significant
226          * overhead in any other kernel task.
227          */
228         proc_freeZombies();
229
230         /* Did the caller provide a stack for us? */
231         if (!stack_base)
232         {
233                 /* Did the caller specify the desired stack size? */
234                 if (!stack_size)
235                         stack_size = KERN_MINSTACKSIZE;
236
237                 /* Allocate stack dinamically */
238                 PROC_ATOMIC(stack_base =
239                         (cpu_stack_t *)heap_allocmem(&proc_heap, stack_size));
240                 if (stack_base == NULL)
241                         return NULL;
242
243                 free_stack = true;
244         }
245
246 #else // CONFIG_KERN_HEAP
247
248         /* Stack must have been provided by the user */
249         ASSERT_VALID_PTR(stack_base);
250         ASSERT(stack_size);
251
252 #endif // CONFIG_KERN_HEAP
253
254 #if CONFIG_KERN_MONITOR
255         /*
256          * Fill-in the stack with a special marker to help debugging.
257          * On 64bit platforms, CONFIG_KERN_STACKFILLCODE is larger
258          * than an int, so the (int) cast is required to silence the
259          * warning for truncating its size.
260          */
261         memset(stack_base, (int)CONFIG_KERN_STACKFILLCODE, stack_size);
262 #endif
263
264         /* Initialize the process control block */
265         if (CPU_STACK_GROWS_UPWARD)
266         {
267                 proc = (Process *)stack_base;
268                 proc->stack = stack_base + PROC_SIZE_WORDS;
269                 // On some architecture stack should be aligned, so we do it.
270                 proc->stack = (cpu_stack_t *)((uintptr_t)proc->stack + (sizeof(cpu_aligned_stack_t) - ((uintptr_t)proc->stack % sizeof(cpu_aligned_stack_t))));
271                 if (CPU_SP_ON_EMPTY_SLOT)
272                         proc->stack++;
273         }
274         else
275         {
276                 proc = (Process *)(stack_base + stack_size / sizeof(cpu_stack_t) - PROC_SIZE_WORDS);
277                 // On some architecture stack should be aligned, so we do it.
278                 proc->stack = (cpu_stack_t *)((uintptr_t)proc - ((uintptr_t)proc % sizeof(cpu_aligned_stack_t)));
279                 if (CPU_SP_ON_EMPTY_SLOT)
280                         proc->stack--;
281         }
282         /* Ensure stack is aligned */
283         ASSERT((uintptr_t)proc->stack % sizeof(cpu_aligned_stack_t) == 0);
284
285         stack_size -= PROC_SIZE_WORDS * sizeof(cpu_stack_t);
286         proc_initStruct(proc);
287         proc->user_data = data;
288
289 #if CONFIG_KERN_HEAP | CONFIG_KERN_MONITOR
290         proc->stack_base = stack_base;
291         proc->stack_size = stack_size;
292         #if CONFIG_KERN_HEAP
293         if (free_stack)
294                 proc->flags |= PF_FREESTACK;
295         #endif
296 #endif
297         proc->user_entry = entry;
298         CPU_CREATE_NEW_STACK(proc->stack);
299
300 #if CONFIG_KERN_MONITOR
301         monitor_add(proc, name);
302 #endif
303
304         /* Add to ready list */
305         ATOMIC(SCHED_ENQUEUE(proc));
306
307         return proc;
308 }
309
310 /**
311  * Return the name of the specified process.
312  *
313  * NULL is a legal argument and will return the name "<NULL>".
314  */
315 const char *proc_name(struct Process *proc)
316 {
317 #if CONFIG_KERN_MONITOR
318         return proc ? proc->monitor.name : "<NULL>";
319 #else
320         (void)proc;
321         return "---";
322 #endif
323 }
324
325 /// Return the name of the currently running process
326 const char *proc_currentName(void)
327 {
328         return proc_name(proc_current());
329 }
330
331 /// Rename a process
332 void proc_rename(struct Process *proc, const char *name)
333 {
334 #if CONFIG_KERN_MONITOR
335         monitor_rename(proc, name);
336 #else
337         (void)proc; (void)name;
338 #endif
339 }
340
341
342 #if CONFIG_KERN_PRI
343 /**
344  * Change the scheduling priority of a process.
345  *
346  * Process piorities are signed ints, whereas a larger integer value means
347  * higher scheduling priority.  The default priority for new processes is 0.
348  * The idle process runs with the lowest possible priority: INT_MIN.
349  *
350  * A process with a higher priority always preempts lower priority processes.
351  * Processes of equal priority share the CPU time according to a simple
352  * round-robin policy.
353  *
354  * As a general rule to maximize responsiveness, compute-bound processes
355  * should be assigned negative priorities and tight, interactive processes
356  * should be assigned positive priorities.
357  *
358  * To avoid interfering with system background activities such as input
359  * processing, application processes should remain within the range -10
360  * and +10.
361  */
362 void proc_setPri(struct Process *proc, int pri)
363 {
364         if (proc->link.pri == pri)
365                 return;
366
367         proc->link.pri = pri;
368
369         if (proc != current_process)
370                 ATOMIC(sched_reenqueue(proc));
371 }
372 #endif // CONFIG_KERN_PRI
373
374 INLINE void proc_run(void)
375 {
376         void (*entry)(void) = current_process->user_entry;
377
378         LOG_INFO("New process starting at %p", entry);
379         entry();
380 }
381
382 /**
383  * Entry point for all the processes.
384  */
385 void proc_entry(void)
386 {
387         /*
388          * Return from a context switch assumes interrupts are disabled, so
389          * we need to explicitly re-enable them as soon as possible.
390          */
391         IRQ_ENABLE;
392         /* Call the actual process's entry point */
393         proc_run();
394         proc_exit();
395 }
396
397 /**
398  * Terminate the current process
399  */
400 void proc_exit(void)
401 {
402         LOG_INFO("%p:%s", current_process, proc_currentName());
403
404 #if CONFIG_KERN_MONITOR
405         monitor_remove(current_process);
406 #endif
407
408         proc_forbid();
409 #if CONFIG_KERN_HEAP
410         /*
411          * Set the task as zombie, its resources will be freed in proc_new() in
412          * a lazy way, when another process will be created.
413          */
414         proc_addZombie(current_process);
415 #endif
416         current_process = NULL;
417         proc_permit();
418
419         proc_switch();
420
421         /* never reached */
422         ASSERT(0);
423 }
424
425
426 /**
427  * Get the pointer to the user data of the current process
428  */
429 iptr_t proc_currentUserData(void)
430 {
431         return current_process->user_data;
432 }