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