proc: Split cooperative scheduler in coop.c
[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 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief Simple realtime multitasking scheduler.
35  *        Context switching is only done cooperatively.
36  *
37  * \version $Id$
38  * \author Bernie Innocenti <bernie@codewiz.org>
39  * \author Stefano Fedrigo <aleph@develer.com>
40  */
41
42 #include "proc_p.h"
43 #include "proc.h"
44
45 #include "cfg/cfg_arch.h"  /* ARCH_EMUL */
46 #include <cfg/module.h>
47
48 #include <cpu/irq.h>
49 #include <cpu/types.h>
50 #include <cpu/attr.h>
51 #include <cpu/frame.h>
52
53 #include <string.h>           /* memset() */
54
55 /*
56  * The scheduer tracks ready processes by enqueuing them in the
57  * ready list.
58  *
59  * \note Access to the list must occur while interrupts are disabled.
60  */
61 REGISTER List ProcReadyList;
62
63 /*
64  * Holds a pointer to the TCB of the currently running process.
65  *
66  * \note User applications should use proc_current() to retrieve this value.
67  */
68 REGISTER Process *CurrentProcess;
69
70 #if (ARCH & ARCH_EMUL)
71 /*
72  * In hosted environments, we must emulate the stack on the real process stack.
73  *
74  * Access to this list must be protected by PROC_ATOMIC().
75  */
76 extern List StackFreeList;
77 #endif
78
79 /** The main process (the one that executes main()). */
80 struct Process MainProcess;
81
82
83 static void proc_init_struct(Process *proc)
84 {
85         /* Avoid warning for unused argument. */
86         (void)proc;
87
88 #if CONFIG_KERN_SIGNALS
89         proc->sig_recv = 0;
90 #endif
91
92 #if CONFIG_KERN_PREEMPTIVE
93         proc->forbid_cnt = 0;
94 #endif
95
96 #if CONFIG_KERN_HEAP
97         proc->flags = 0;
98 #endif
99 }
100
101 MOD_DEFINE(proc);
102
103 void proc_init(void)
104 {
105         LIST_INIT(&ProcReadyList);
106
107         /*
108          * We "promote" the current context into a real process. The only thing we have
109          * to do is create a PCB and make it current. We don't need to setup the stack
110          * pointer because it will be written the first time we switch to another process.
111          */
112         proc_init_struct(&MainProcess);
113         CurrentProcess = &MainProcess;
114
115 #if CONFIG_KERN_MONITOR
116         monitor_init();
117         monitor_add(CurrentProcess, "main");
118 #endif
119
120         MOD_INIT(proc);
121 }
122
123
124 /**
125  * Create a new process, starting at the provided entry point.
126  *
127  * \return Process structure of new created process
128  *         if successful, NULL otherwise.
129  */
130 struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(void), iptr_t data, size_t stack_size, cpustack_t *stack_base)
131 {
132         Process *proc;
133         size_t i;
134         const size_t PROC_SIZE_WORDS = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
135 #if CONFIG_KERN_HEAP
136         bool free_stack = false;
137 #endif
138         TRACEMSG("name=%s", name);
139
140 #if (ARCH & ARCH_EMUL)
141         /* Ignore stack provided by caller and use the large enough default instead. */
142         PROC_ATOMIC(stack_base = (cpustack_t *)list_remHead(&StackFreeList));
143
144         stack_size = CONFIG_PROC_DEFSTACKSIZE;
145 #elif CONFIG_KERN_HEAP
146         /* Did the caller provide a stack for us? */
147         if (!stack_base)
148         {
149                 /* Did the caller specify the desired stack size? */
150                 if (!stack_size)
151                         stack_size = CONFIG_PROC_DEFSTACKSIZE + sizeof(Process);
152
153                 /* Allocate stack dinamically */
154                 if (!(stack_base = heap_alloc(stack_size)))
155                         return NULL;
156
157                 free_stack = true;
158         }
159 #else
160         /* Stack must have been provided by the user */
161         ASSERT_VALID_PTR(stack_base);
162         ASSERT(stack_size);
163 #endif
164
165 #if CONFIG_KERN_MONITOR
166         /* Fill-in the stack with a special marker to help debugging */
167 #warning size incorrect
168         memset(stack_base, CONFIG_KERN_STACKFILLCODE, stack_size / sizeof(cpustack_t));
169 #endif
170
171         /* Initialize the process control block */
172         if (CPU_STACK_GROWS_UPWARD)
173         {
174                 proc = (Process*)stack_base;
175                 proc->stack = stack_base + PROC_SIZE_WORDS;
176                 if (CPU_SP_ON_EMPTY_SLOT)
177                         proc->stack++;
178         }
179         else
180         {
181                 proc = (Process*)(stack_base + stack_size / sizeof(cpustack_t) - PROC_SIZE_WORDS);
182                 proc->stack = (cpustack_t*)proc;
183                 if (CPU_SP_ON_EMPTY_SLOT)
184                         proc->stack--;
185         }
186
187         proc_init_struct(proc);
188         proc->user_data = data;
189
190 #if CONFIG_KERN_HEAP | CONFIG_KERN_MONITOR | (ARCH & ARCH_EMUL)
191         proc->stack_base = stack_base;
192         proc->stack_size = stack_size;
193         #if CONFIG_KERN_HEAP
194         if (free_stack)
195                 proc->flags |= PF_FREESTACK;
196         #endif
197 #endif
198
199         /* Initialize process stack frame */
200         CPU_PUSH_CALL_FRAME(proc->stack, proc_exit);
201         CPU_PUSH_CALL_FRAME(proc->stack, entry);
202
203         /* Push a clean set of CPU registers for asm_switch_context() */
204         for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
205                 CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
206
207         /* Add to ready list */
208         ATOMIC(SCHED_ENQUEUE(proc));
209         ATOMIC(LIST_ASSERT_VALID(&ProcReadyList));
210
211 #if CONFIG_KERN_MONITOR
212         monitor_add(proc, name);
213 #endif
214
215         return proc;
216 }
217
218 /** Rename a process */
219 void proc_rename(struct Process *proc, const char *name)
220 {
221 #if CONFIG_KERN_MONITOR
222         monitor_rename(proc, name);
223 #else
224         (void)proc; (void)name;
225 #endif
226 }
227
228
229 /**
230  * Terminate the current process
231  */
232 void proc_exit(void)
233 {
234         TRACE;
235
236 #if CONFIG_KERN_MONITOR
237         monitor_remove(CurrentProcess);
238 #endif
239
240 #if CONFIG_KERN_HEAP
241         /*
242          * The following code is BROKEN.
243          * We are freeing our own stack before entering proc_schedule()
244          * BAJO: A correct fix would be to rearrange the scheduler with
245          *  an additional parameter which frees the old stack/process
246          *  after a context switch.
247          */
248         if (CurrentProcess->flags & PF_FREESTACK)
249                 heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
250         heap_free(CurrentProcess);
251 #endif
252
253 #if (ARCH & ARCH_EMUL)
254 #warning This is wrong
255         /* Reinsert process stack in free list */
256         PROC_ATOMIC(ADDHEAD(&StackFreeList, (Node *)(CurrentProcess->stack
257                 - (CONFIG_PROC_DEFSTACKSIZE / sizeof(cpustack_t)))));
258
259         /*
260          * NOTE: At this point the first two words of what used
261          * to be our stack contain a list node. From now on, we
262          * rely on the compiler not reading/writing the stack.
263          */
264 #endif /* ARCH_EMUL */
265
266         CurrentProcess = NULL;
267         proc_schedule();
268         /* not reached */
269 }
270
271
272 /**
273  * Get the pointer to the current process
274  */
275 struct Process *proc_current(void)
276 {
277         return CurrentProcess;
278 }
279
280 /**
281  * Get the pointer to the user data of the current process
282  */
283 iptr_t proc_current_user_data(void)
284 {
285         return CurrentProcess->user_data;
286 }