713c457f9c701820cf8d32a26e11d9d719b926db
[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
143 #if CONFIG_KERN_PREEMPT
144         preempt_init();
145 #endif
146
147         MOD_INIT(proc);
148 }
149
150
151 #if CONFIG_KERN_HEAP
152
153 /**
154  * Free all the resources of all zombie processes previously added to the zombie
155  * list.
156  */
157 static void proc_freeZombies(void)
158 {
159         Process *proc;
160
161         while (1)
162         {
163                 PROC_ATOMIC(proc = (Process *)list_remHead(&zombie_list));
164                 if (proc == NULL)
165                         return;
166
167                 if (proc->flags & PF_FREESTACK)
168                 {
169                         PROC_ATOMIC(heap_freemem(&proc_heap, proc->stack_base,
170                                 proc->stack_size + PROC_SIZE_WORDS * sizeof(cpu_stack_t)));
171                 }
172         }
173 }
174
175 /**
176  * Enqueue a process in the zombie list.
177  */
178 static void proc_addZombie(Process *proc)
179 {
180         Node *node;
181 #if CONFIG_KERN_PREEMPT
182         ASSERT(!proc_preemptAllowed());
183 #endif
184
185 #if CONFIG_KERN_PRI
186         node = &(proc)->link.link;
187 #else
188         node = &(proc)->link;
189 #endif
190         LIST_ASSERT_VALID(&zombie_list);
191         ADDTAIL(&zombie_list, node);
192 }
193
194 #endif /* CONFIG_KERN_HEAP */
195
196 /**
197  * Create a new process, starting at the provided entry point.
198  *
199  *
200  * \note The function
201  * \code
202  * proc_new(entry, data, stacksize, stack)
203  * \endcode
204  * is a more convenient way to create a process, as you don't have to specify
205  * the name.
206  *
207  * \return Process structure of new created process
208  *         if successful, NULL otherwise.
209  */
210 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)
211 {
212         Process *proc;
213         LOG_INFO("name=%s", name);
214 #if CONFIG_KERN_HEAP
215         bool free_stack = false;
216
217         /*
218          * Free up resources of a zombie process.
219          *
220          * We're implementing a kind of lazy garbage collector here for
221          * efficiency reasons: we can avoid to introduce overhead into another
222          * kernel task dedicated to free up resources (e.g., idle) and we're
223          * not introducing any overhead into the scheduler after a context
224          * switch (that would be *very* bad, because the scheduler runs with
225          * IRQ disabled).
226          *
227          * In this way we are able to release the memory of the zombie tasks
228          * without disabling IRQs and without introducing any significant
229          * overhead in any other kernel task.
230          */
231         proc_freeZombies();
232
233         /* Did the caller provide a stack for us? */
234         if (!stack_base)
235         {
236                 /* Did the caller specify the desired stack size? */
237                 if (!stack_size)
238                         stack_size = KERN_MINSTACKSIZE;
239
240                 /* Allocate stack dinamically */
241                 PROC_ATOMIC(stack_base =
242                         (cpu_stack_t *)heap_allocmem(&proc_heap, stack_size));
243                 if (stack_base == NULL)
244                         return NULL;
245
246                 free_stack = true;
247         }
248
249 #else // CONFIG_KERN_HEAP
250
251         /* Stack must have been provided by the user */
252         ASSERT_VALID_PTR(stack_base);
253         ASSERT(stack_size);
254
255 #endif // CONFIG_KERN_HEAP
256
257 #if CONFIG_KERN_MONITOR
258         /*
259          * Fill-in the stack with a special marker to help debugging.
260          * On 64bit platforms, CONFIG_KERN_STACKFILLCODE is larger
261          * than an int, so the (int) cast is required to silence the
262          * warning for truncating its size.
263          */
264         memset(stack_base, (int)CONFIG_KERN_STACKFILLCODE, stack_size);
265 #endif
266
267         /* Initialize the process control block */
268         if (CPU_STACK_GROWS_UPWARD)
269         {
270                 proc = (Process *)stack_base;
271                 proc->stack = stack_base + PROC_SIZE_WORDS;
272                 // On some architecture stack should be aligned, so we do it.
273                 proc->stack = (cpu_stack_t *)((uintptr_t)proc->stack + (sizeof(cpu_aligned_stack_t) - ((uintptr_t)proc->stack % sizeof(cpu_aligned_stack_t))));
274                 if (CPU_SP_ON_EMPTY_SLOT)
275                         proc->stack++;
276         }
277         else
278         {
279                 proc = (Process *)(stack_base + stack_size / sizeof(cpu_stack_t) - PROC_SIZE_WORDS);
280                 // On some architecture stack should be aligned, so we do it.
281                 proc->stack = (cpu_stack_t *)((uintptr_t)proc - ((uintptr_t)proc % sizeof(cpu_aligned_stack_t)));
282                 if (CPU_SP_ON_EMPTY_SLOT)
283                         proc->stack--;
284         }
285         /* Ensure stack is aligned */
286         ASSERT((uintptr_t)proc->stack % sizeof(cpu_aligned_stack_t) == 0);
287
288         stack_size -= PROC_SIZE_WORDS * sizeof(cpu_stack_t);
289         proc_initStruct(proc);
290         proc->user_data = data;
291
292 #if CONFIG_KERN_HEAP | CONFIG_KERN_MONITOR
293         proc->stack_base = stack_base;
294         proc->stack_size = stack_size;
295         #if CONFIG_KERN_HEAP
296         if (free_stack)
297                 proc->flags |= PF_FREESTACK;
298         #endif
299 #endif
300         proc->user_entry = entry;
301         CPU_CREATE_NEW_STACK(proc->stack);
302
303 #if CONFIG_KERN_MONITOR
304         monitor_add(proc, name);
305 #endif
306
307         /* Add to ready list */
308         ATOMIC(SCHED_ENQUEUE(proc));
309
310         return proc;
311 }
312
313 /**
314  * Return the name of the specified process.
315  *
316  * NULL is a legal argument and will return the name "<NULL>".
317  */
318 const char *proc_name(struct Process *proc)
319 {
320 #if CONFIG_KERN_MONITOR
321         return proc ? proc->monitor.name : "<NULL>";
322 #else
323         (void)proc;
324         return "---";
325 #endif
326 }
327
328 /// Return the name of the currently running process
329 const char *proc_currentName(void)
330 {
331         return proc_name(proc_current());
332 }
333
334 /// Rename a process
335 void proc_rename(struct Process *proc, const char *name)
336 {
337 #if CONFIG_KERN_MONITOR
338         monitor_rename(proc, name);
339 #else
340         (void)proc; (void)name;
341 #endif
342 }
343
344
345 #if CONFIG_KERN_PRI
346 /**
347  * Change the scheduling priority of a process.
348  *
349  * Process piorities are signed ints, whereas a larger integer value means
350  * higher scheduling priority.  The default priority for new processes is 0.
351  * The idle process runs with the lowest possible priority: INT_MIN.
352  *
353  * A process with a higher priority always preempts lower priority processes.
354  * Processes of equal priority share the CPU time according to a simple
355  * round-robin policy.
356  *
357  * As a general rule to maximize responsiveness, compute-bound processes
358  * should be assigned negative priorities and tight, interactive processes
359  * should be assigned positive priorities.
360  *
361  * To avoid interfering with system background activities such as input
362  * processing, application processes should remain within the range -10
363  * and +10.
364  */
365 void proc_setPri(struct Process *proc, int pri)
366 {
367         if (proc->link.pri == pri)
368                 return;
369
370         proc->link.pri = pri;
371
372         if (proc != current_process)
373                 ATOMIC(sched_reenqueue(proc));
374 }
375 #endif // CONFIG_KERN_PRI
376
377 INLINE void proc_run(void)
378 {
379         void (*entry)(void) = current_process->user_entry;
380
381         LOG_INFO("New process starting at %p", entry);
382         entry();
383 }
384
385 /**
386  * Entry point for all the processes.
387  */
388 void proc_entry(void)
389 {
390         /*
391          * Return from a context switch assumes interrupts are disabled, so
392          * we need to explicitly re-enable them as soon as possible.
393          */
394         IRQ_ENABLE;
395         /* Call the actual process's entry point */
396         proc_run();
397         proc_exit();
398 }
399
400 /**
401  * Terminate the current process
402  */
403 void proc_exit(void)
404 {
405         LOG_INFO("%p:%s", current_process, proc_currentName());
406
407 #if CONFIG_KERN_MONITOR
408         monitor_remove(current_process);
409 #endif
410
411         proc_forbid();
412 #if CONFIG_KERN_HEAP
413         /*
414          * Set the task as zombie, its resources will be freed in proc_new() in
415          * a lazy way, when another process will be created.
416          */
417         proc_addZombie(current_process);
418 #endif
419         current_process = NULL;
420         proc_permit();
421
422         proc_switch();
423
424         /* never reached */
425         ASSERT(0);
426 }
427
428
429 /**
430  * Get the pointer to the user data of the current process
431  */
432 iptr_t proc_currentUserData(void)
433 {
434         return current_process->user_data;
435 }