altos: Add stack-guard code. Uses STM MPU to trap stack overflow.
[fw/altos] / src / core / ao_task.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_task.h>
20 #if HAS_SAMPLE_PROFILE
21 #include <ao_sample_profile.h>
22 #endif
23 #if HAS_STACK_GUARD
24 #include <ao_mpu.h>
25 #endif
26
27 #define AO_NO_TASK_INDEX        0xff
28
29 __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
30 __data uint8_t ao_num_tasks;
31 __data uint8_t ao_cur_task_index;
32 __xdata struct ao_task *__data ao_cur_task;
33
34 #ifdef ao_arch_task_globals
35 ao_arch_task_globals
36 #endif
37
38 #define AO_CHECK_STACK  0
39
40 #if AO_CHECK_STACK
41 static uint8_t  in_yield;
42
43 static inline void ao_check_stack(void) {
44         uint8_t q;
45         if (!in_yield && ao_cur_task && &q < &ao_cur_task->stack[0])
46                 ao_panic(AO_PANIC_STACK);
47 }
48 #else
49 #define ao_check_stack()
50 #endif
51
52 void
53 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
54 {
55         uint8_t task_id;
56         uint8_t t;
57         if (ao_num_tasks == AO_NUM_TASKS)
58                 ao_panic(AO_PANIC_NO_TASK);
59         for (task_id = 1; task_id != 0; task_id++) {
60                 for (t = 0; t < ao_num_tasks; t++)
61                         if (ao_tasks[t]->task_id == task_id)
62                                 break;
63                 if (t == ao_num_tasks)
64                         break;
65         }
66         ao_tasks[ao_num_tasks++] = task;
67         task->task_id = task_id;
68         task->name = name;
69         task->wchan = NULL;
70         /*
71          * Construct a stack frame so that it will 'return'
72          * to the start of the task
73          */
74         ao_arch_init_stack(task, start);
75 }
76
77 __xdata uint8_t ao_idle;
78
79 /* Task switching function. This must not use any stack variables */
80 void
81 ao_yield(void) ao_arch_naked_define
82 {
83         ao_arch_save_regs();
84
85         if (ao_cur_task_index == AO_NO_TASK_INDEX)
86                 ao_cur_task_index = ao_num_tasks-1;
87         else
88         {
89 #if HAS_SAMPLE_PROFILE
90                 uint16_t        tick = ao_sample_profile_timer_value();
91                 uint16_t        run = tick - ao_cur_task->start;
92                 if (run > ao_cur_task->max_run)
93                         ao_cur_task->max_run = run;
94                 ++ao_cur_task->yields;
95 #endif
96                 ao_arch_save_stack();
97         }
98
99         ao_arch_isr_stack();
100
101 #if AO_CHECK_STACK
102         in_yield = 1;
103 #endif
104         /* Find a task to run. If there isn't any runnable task,
105          * this loop will run forever, which is just fine
106          */
107         {
108                 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
109                 for (;;) {
110                         ++ao_cur_task_index;
111                         if (ao_cur_task_index == ao_num_tasks)
112                                 ao_cur_task_index = 0;
113
114                         ao_cur_task = ao_tasks[ao_cur_task_index];
115
116                         /* Check for ready task */
117                         if (ao_cur_task->wchan == NULL)
118                                 break;
119
120                         /* Check if the alarm is set for a time which has passed */
121                         if (ao_cur_task->alarm &&
122                             (int16_t) (ao_time() - ao_cur_task->alarm) >= 0)
123                                 break;
124
125                         /* Enter lower power mode when there isn't anything to do */
126                         if (ao_cur_task_index == ao_last_task_index)
127                                 ao_arch_cpu_idle();
128                 }
129 #if HAS_SAMPLE_PROFILE
130         ao_cur_task->start = ao_sample_profile_timer_value();
131 #endif
132         }
133 #if HAS_STACK_GUARD
134         ao_mpu_stack_guard(ao_cur_task->stack);
135 #endif
136 #if AO_CHECK_STACK
137         cli();
138         in_yield = 0;
139 #endif
140         ao_arch_restore_stack();
141 }
142
143 uint8_t
144 ao_sleep(__xdata void *wchan)
145 {
146         ao_cur_task->wchan = wchan;
147         ao_yield();
148         if (ao_cur_task->wchan) {
149                 ao_cur_task->wchan = NULL;
150                 ao_cur_task->alarm = 0;
151                 return 1;
152         }
153         return 0;
154 }
155
156 void
157 ao_wakeup(__xdata void *wchan)
158 {
159         uint8_t i;
160
161         ao_check_stack();
162         for (i = 0; i < ao_num_tasks; i++)
163                 if (ao_tasks[i]->wchan == wchan)
164                         ao_tasks[i]->wchan = NULL;
165 }
166
167 void
168 ao_alarm(uint16_t delay)
169 {
170         /* Make sure we sleep *at least* delay ticks, which means adding
171          * one to account for the fact that we may be close to the next tick
172          */
173         if (!(ao_cur_task->alarm = ao_time() + delay + 1))
174                 ao_cur_task->alarm = 1;
175 }
176
177 void
178 ao_clear_alarm(void)
179 {
180         ao_cur_task->alarm = 0;
181 }
182
183 static __xdata uint8_t ao_forever;
184
185 void
186 ao_delay(uint16_t ticks)
187 {
188         ao_alarm(ticks);
189         ao_sleep(&ao_forever);
190         ao_clear_alarm();
191 }
192
193 void
194 ao_exit(void)
195 {
196         ao_arch_critical(
197                 uint8_t i;
198                 ao_num_tasks--;
199                 for (i = ao_cur_task_index; i < ao_num_tasks; i++)
200                         ao_tasks[i] = ao_tasks[i+1];
201                 ao_cur_task_index = AO_NO_TASK_INDEX;
202                 ao_yield();
203                 );
204         /* we'll never get back here */
205 }
206
207 void
208 ao_task_info(void)
209 {
210         uint8_t         i;
211         __xdata struct ao_task *task;
212
213         for (i = 0; i < ao_num_tasks; i++) {
214                 task = ao_tasks[i];
215                 printf("%12s: wchan %04x\n",
216                        task->name,
217                        (int) task->wchan);
218         }
219 }
220
221 void
222 ao_start_scheduler(void)
223 {
224         ao_cur_task_index = AO_NO_TASK_INDEX;
225         ao_cur_task = NULL;
226         ao_yield();
227 }