thread_arch.c
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_atmega_common
* @{
*
* @file
* @brief Implementation of the kernel's architecture dependent thread interface
*
* @author Hinnerk van Bruinehsen <h.v.bruinehsen@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include "arch/thread_arch.h"
#include "thread.h"
#include "sched.h"
#include "irq.h"
#include "cpu.h"
#include "board.h"
/**
* @brief AVR_CONTEXT_SWAP_INIT initialize the context swap trigger
* Called when threading is first started.
*/
#ifndef AVR_CONTEXT_SWAP_INIT
#error AVR_CONTEXT_SWAP_INIT must be defined in board.h
#endif
/**
* @brief AVR_CONTEXT_SWAP_INTERRUPT_VECT Name of the ISR to use for context swapping
*/
#ifndef AVR_CONTEXT_SWAP_INTERRUPT_VECT
#error AVR_CONTEXT_SWAP_INTERRUPT_VECT must be defined in board.h
#endif
/**
* @brief AVR_CONTEXT_SWAP_TRIGGER executed to start the context swap
* When executed, this should result in the interrupt named in
* AVR_CONTEXT_SWAP_INTERRUPT_VECT being called
*/
#ifndef AVR_CONTEXT_SWAP_TRIGGER
#error ARV_CONTEXT_SWAP_TRIGGER must be defined in board.h
#endif
/*
* local function declarations (prefixed with __)
*/
static void __context_save(void);
static void __context_restore(void);
static void __enter_thread_mode(void);
/**
* @brief Since AVR doesn't support direct manipulation of the program counter we
* model a stack like it would be left by __context_save().
* The resulting layout in memory is the following:
* ---------------thread_t (not created by thread_arch_stack_init) ----------
* local variables (a temporary value and the stackpointer)
* -----------------------------------------------------------------------
* a marker (AFFE) - for debugging purposes (helps finding the stack
* -----------------------------------------------------------------------
* a 16 Bit pointer to sched_task_exit
* (Optional 17 bit (bit is set to zero) for devices with > 128kb FLASH)
* -----------------------------------------------------------------------
* a 16 Bit pointer to task_func
* this is placed exactly at the place where the program counter would be
* stored normally and thus can be returned to when __context_restore()
* has been run
* (Optional 17 bit (bit is set to zero) for devices with > 128kb FLASH)
* -----------------------------------------------------------------------
* saved registers from context:
* r0
* status register
* (Optional EIND and RAMPZ registers)
* r1 - r23
* pointer to arg in r24 and r25
* r26 - r31
* -----------------------------------------------------------------------
*
* After the invocation of __context_restore() the pointer to task_func is
* on top of the stack and can be returned to. This way we can actually place
* it inside of the programm counter of the MCU.
* if task_func returns sched_task_exit gets popped into the PC
*/
char *thread_arch_stack_init(thread_task_func_t task_func, void *arg,
void *stack_start, int stack_size)
{
uint16_t tmp_adress;
uint8_t *stk;
/* AVR uses 16 Bit or two 8 Bit registers for storing pointers*/
stk = (uint8_t *)((uintptr_t)stack_start + stack_size);
/* put marker on stack */
stk--;
*stk = (uint8_t) 0xAF;
stk--;
*stk = (uint8_t) 0xFE;
/* save sched_task_exit */
stk--;
tmp_adress = (uint16_t) sched_task_exit;
*stk = (uint8_t)(tmp_adress & (uint16_t) 0x00ff);
stk--;
tmp_adress >>= 8;
*stk = (uint8_t)(tmp_adress & (uint16_t) 0x00ff);
#if FLASHEND > 0x1ffff
/* Devices with more than 128kb FLASH use a 17 bit PC, we set whole the top byte forcibly to 0 */
stk--;
*stk = (uint8_t) 0x00;
#endif
/* save address to task_func in place of the program counter */
stk--;
tmp_adress = (uint16_t) task_func;
*stk = (uint8_t)(tmp_adress & (uint16_t) 0x00ff);
stk--;
tmp_adress >>= 8;
*stk = (uint8_t)(tmp_adress & (uint16_t) 0x00ff);
#if FLASHEND > 0x1ffff
/* Devices with more than 128kb FLASH use a 17 bit PC, we set whole the top byte forcibly to 0 */
stk--;
*stk = (uint8_t) 0x00;
#endif
/* r0 */
stk--;
*stk = (uint8_t) 0x00;
/* status register (with interrupts enabled) */
stk--;
*stk = (uint8_t) 0x80;
#if defined(EIND)
stk--;
*stk = (uint8_t) 0x00;
#endif
#if defined(RAMPZ)
stk--;
*stk = (uint8_t) 0x00;
#endif
/* r1 - has always to be 0 */
stk--;
*stk = (uint8_t) 0x00;
/*
* Space for registers r2 -r23
*
* use loop for better readability, the compiler unrolls anyways
*/
int i;
for (i = 2; i <= 23 ; i++) {
stk--;
*stk = (uint8_t) 0;
}
/*
* In accordance with the AVR calling conventions *arg has to be inside
* r24 and r25
* */
stk--;
tmp_adress = (uint16_t) arg;
*stk = (uint8_t)(tmp_adress & (uint16_t) 0x00ff);
stk--;
tmp_adress >>= 8;
*stk = (uint8_t)(tmp_adress & (uint16_t) 0x00ff);
/*
* Space for registers r26-r31
*/
for (i = 26; i <= 31; i++) {
stk--;
*stk = (uint8_t) i;
}
stk--;
return (char *) stk;
}
/**
* @brief thread_arch_stack_print prints the stack to stdout.
* It depends on getting the correct values for stack_start, stack_size and sp
* from sched_active_thread.
* Maybe it would be good to change that to way that is less dependant on
* getting correct values elsewhere (since it is a debugging tool and in the
* presence of bugs the data may be corrupted).
*/
void thread_arch_stack_print(void)
{
uint8_t found_marker = 1;
uint8_t *sp = (uint8_t *)sched_active_thread->sp;
uint16_t size = 0;
printf("Printing current stack of thread %" PRIkernel_pid "\n", thread_getpid());
printf("\taddress:\tdata:\n");
do {
printf("\t0x%04x:\t\t0x%04x\n", (unsigned int)sp, (unsigned int)*sp);
sp++;
size++;
if ((*sp == 0xFE) && (*(sp + 1) == 0xAF)) {
found_marker = 0;
}
}
while (found_marker == 1);
printf("stack size: %u bytes\n", size);
}
void thread_arch_start_threading(void) __attribute__((naked));
void thread_arch_start_threading(void)
{
sched_run();
AVR_CONTEXT_SWAP_INIT;
__enter_thread_mode();
}
/**
* @brief Set the MCU into Thread-Mode and load the initial task from the stack and run it
*/
void NORETURN __enter_thread_mode(void) __attribute__((naked));
void NORETURN __enter_thread_mode(void)
{
irq_enable();
__context_restore();
__asm__ volatile("ret");
UNREACHABLE();
}
void thread_arch_yield(void) {
AVR_CONTEXT_SWAP_TRIGGER;
}
// Use this interrupt to perform all context switches
ISR(AVR_CONTEXT_SWAP_INTERRUPT_VECT, ISR_NAKED) {
__context_save();
sched_run();
__context_restore();
__asm__ volatile("reti");
}
__attribute__((always_inline)) static inline void __context_save(void)
{
__asm__ volatile(
"push r0 \n\t"
"in r0, __SREG__ \n\t"
"cli \n\t"
"push r0 \n\t"
#if defined(RAMPZ)
"in r0, __RAMPZ__ \n\t"
"push r0 \n\t"
#endif
#if defined(EIND)
"in r0, 0x3c \n\t"
"push r0 \n\t"
#endif
"push r1 \n\t"
"clr r1 \n\t"
"push r2 \n\t"
"push r3 \n\t"
"push r4 \n\t"
"push r5 \n\t"
"push r6 \n\t"
"push r7 \n\t"
"push r8 \n\t"
"push r9 \n\t"
"push r10 \n\t"
"push r11 \n\t"
"push r12 \n\t"
"push r13 \n\t"
"push r14 \n\t"
"push r15 \n\t"
"push r16 \n\t"
"push r17 \n\t"
"push r18 \n\t"
"push r19 \n\t"
"push r20 \n\t"
"push r21 \n\t"
"push r22 \n\t"
"push r23 \n\t"
"push r24 \n\t"
"push r25 \n\t"
"push r26 \n\t"
"push r27 \n\t"
"push r28 \n\t"
"push r29 \n\t"
"push r30 \n\t"
"push r31 \n\t"
"lds r26, sched_active_thread \n\t"
"lds r27, sched_active_thread + 1 \n\t"
"in r0, __SP_L__ \n\t"
"st x+, r0 \n\t"
"in r0, __SP_H__ \n\t"
"st x+, r0 \n\t"
);
}
__attribute__((always_inline)) static inline void __context_restore(void)
{
__asm__ volatile(
"lds r26, sched_active_thread \n\t"
"lds r27, sched_active_thread + 1 \n\t"
"ld r28, x+ \n\t"
"out __SP_L__, r28 \n\t"
"ld r29, x+ \n\t"
"out __SP_H__, r29 \n\t"
"pop r31 \n\t"
"pop r30 \n\t"
"pop r29 \n\t"
"pop r28 \n\t"
"pop r27 \n\t"
"pop r26 \n\t"
"pop r25 \n\t"
"pop r24 \n\t"
"pop r23 \n\t"
"pop r22 \n\t"
"pop r21 \n\t"
"pop r20 \n\t"
"pop r19 \n\t"
"pop r18 \n\t"
"pop r17 \n\t"
"pop r16 \n\t"
"pop r15 \n\t"
"pop r14 \n\t"
"pop r13 \n\t"
"pop r12 \n\t"
"pop r11 \n\t"
"pop r10 \n\t"
"pop r9 \n\t"
"pop r8 \n\t"
"pop r7 \n\t"
"pop r6 \n\t"
"pop r5 \n\t"
"pop r4 \n\t"
"pop r3 \n\t"
"pop r2 \n\t"
"pop r1 \n\t"
#if defined(EIND)
"pop r0 \n\t"
"out 0x3c, r0 \n\t"
#endif
#if defined(RAMPZ)
"pop r0 \n\t"
"out __RAMPZ__, r0 \n\t"
#endif
"pop r0 \n\t"
"out __SREG__, r0 \n\t"
"pop r0 \n\t"
);
}