2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* FreeRTOS includes. */
\r
30 #include "FreeRTOS.h"
\r
34 * "Reg test" tasks - These fill the registers with known values, then check
\r
35 * that each register maintains its expected value for the lifetime of the
\r
36 * task. Each task uses a different set of values. The reg test tasks execute
\r
37 * with a very low priority, so get preempted very frequently. A register
\r
38 * containing an unexpected value is indicative of an error in the context
\r
39 * switching mechanism.
\r
42 void vRegTest1Implementation( void *pvParameters );
\r
43 void vRegTest2Implementation( void *pvParameters );
\r
44 void vRegTest3Implementation( void ) __attribute__ ((naked));
\r
45 void vRegTest4Implementation( void ) __attribute__ ((naked));
\r
48 * Used as an easy way of deleting a task from inline assembly.
\r
50 extern void vMainDeleteMe( void ) __attribute__((noinline));
\r
53 * Used by the first two reg test tasks and a software timer callback function
\r
54 * to send messages to the check task. The message just lets the check task
\r
55 * know that the tasks and timer are still functioning correctly. If a reg test
\r
56 * task detects an error it will delete itself, and in so doing prevent itself
\r
57 * from sending any more 'I'm Alive' messages to the check task.
\r
59 extern void vMainSendImAlive( QueueHandle_t xHandle, uint32_t ulTaskNumber );
\r
61 /* The queue used to send a message to the check task. */
\r
62 extern QueueHandle_t xGlobalScopeCheckQueue;
\r
64 /*-----------------------------------------------------------*/
\r
66 void vRegTest1Implementation( void *pvParameters )
\r
68 /* This task is created in privileged mode so can access the file scope
\r
69 queue variable. Take a stack copy of this before the task is set into user
\r
70 mode. Once this task is in user mode the file scope queue variable will no
\r
71 longer be accessible but the stack copy will. */
\r
72 QueueHandle_t xQueue = xGlobalScopeCheckQueue;
\r
74 /* Now the queue handle has been obtained the task can switch to user
\r
75 mode. This is just one method of passing a handle into a protected
\r
76 task, the other reg test task uses the task parameter instead. */
\r
77 portSWITCH_TO_USER_MODE();
\r
79 /* First check that the parameter value is as expected. */
\r
80 if( pvParameters != ( void * ) configREG_TEST_TASK_1_PARAMETER )
\r
82 /* Error detected. Delete the task so it stops communicating with
\r
89 /* This task tests the kernel context switch mechanism by reading and
\r
90 writing directly to registers - which requires the test to be written
\r
91 in assembly code. */
\r
94 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
100 " MOV R11, #111 \n"
\r
102 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
106 " MOV R12, #112 \n"
\r
107 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
108 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
109 " BNE vMainDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
111 " BNE vMainDeleteMe \n"
\r
113 " BNE vMainDeleteMe \n"
\r
115 " BNE vMainDeleteMe \n"
\r
117 " BNE vMainDeleteMe \n"
\r
119 " BNE vMainDeleteMe \n"
\r
121 " BNE vMainDeleteMe \n"
\r
123 " BNE vMainDeleteMe \n"
\r
125 " BNE vMainDeleteMe \n"
\r
126 " CMP R10, #110 \n"
\r
127 " BNE vMainDeleteMe \n"
\r
128 " CMP R11, #111 \n"
\r
129 " BNE vMainDeleteMe \n"
\r
130 " CMP R12, #112 \n"
\r
131 " BNE vMainDeleteMe \n"
\r
132 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
135 /* Send configREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
136 task is still functioning. */
\r
137 vMainSendImAlive( xQueue, configREG_TEST_1_STILL_EXECUTING );
\r
139 /* Go back to check all the register values again. */
\r
140 __asm volatile( " B reg1loop " );
\r
143 /*-----------------------------------------------------------*/
\r
145 void vRegTest2Implementation( void *pvParameters )
\r
147 /* The queue handle is passed in as the task parameter. This is one method of
\r
148 passing data into a protected task, the other reg test task uses a different
\r
150 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
154 /* This task tests the kernel context switch mechanism by reading and
\r
155 writing directly to registers - which requires the test to be written
\r
156 in assembly code. */
\r
159 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
162 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
167 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
172 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
173 " BNE vMainDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
175 " BNE vMainDeleteMe \n"
\r
177 " BNE vMainDeleteMe \n"
\r
179 " BNE vMainDeleteMe \n"
\r
181 " BNE vMainDeleteMe \n"
\r
183 " BNE vMainDeleteMe \n"
\r
185 " BNE vMainDeleteMe \n"
\r
187 " BNE vMainDeleteMe \n"
\r
189 " BNE vMainDeleteMe \n"
\r
191 " BNE vMainDeleteMe \n"
\r
193 " BNE vMainDeleteMe \n"
\r
195 " BNE vMainDeleteMe \n"
\r
196 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
199 /* Send configREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
200 task is still functioning. */
\r
201 vMainSendImAlive( xQueue, configREG_TEST_2_STILL_EXECUTING );
\r
203 /* Go back to check all the register values again. */
\r
204 __asm volatile( " B reg2loop " );
\r
207 /*-----------------------------------------------------------*/
\r
209 void vRegTest3Implementation( void )
\r
213 ".extern pulRegTest3LoopCounter \n"
\r
214 "/* Fill the core registers with known values. */ \n"
\r
229 "/* Fill the VFP registers with known values. */ \n"
\r
230 "vmov d0, r0, r1 \n"
\r
231 "vmov d1, r2, r3 \n"
\r
232 "vmov d2, r4, r5 \n"
\r
233 "vmov d3, r6, r7 \n"
\r
234 "vmov d4, r8, r9 \n"
\r
235 "vmov d5, r10, r11 \n"
\r
236 "vmov d6, r0, r1 \n"
\r
237 "vmov d7, r2, r3 \n"
\r
238 "vmov d8, r4, r5 \n"
\r
239 "vmov d9, r6, r7 \n"
\r
240 "vmov d10, r8, r9 \n"
\r
241 "vmov d11, r10, r11 \n"
\r
242 "vmov d12, r0, r1 \n"
\r
243 "vmov d13, r2, r3 \n"
\r
244 "vmov d14, r4, r5 \n"
\r
245 "vmov d15, r6, r7 \n"
\r
248 "/* Check all the VFP registers still contain the values set above. \n"
\r
249 "First save registers that are clobbered by the test. */ \n"
\r
250 "push { r0-r1 } \n"
\r
252 "vmov r0, r1, d0 \n"
\r
254 "bne reg1_error_loopf \n"
\r
256 "bne reg1_error_loopf \n"
\r
257 "vmov r0, r1, d1 \n"
\r
259 "bne reg1_error_loopf \n"
\r
261 "bne reg1_error_loopf \n"
\r
262 "vmov r0, r1, d2 \n"
\r
264 "bne reg1_error_loopf \n"
\r
266 "bne reg1_error_loopf \n"
\r
267 "vmov r0, r1, d3 \n"
\r
269 "bne reg1_error_loopf \n"
\r
271 "bne reg1_error_loopf \n"
\r
272 "vmov r0, r1, d4 \n"
\r
274 "bne reg1_error_loopf \n"
\r
276 "bne reg1_error_loopf \n"
\r
277 "vmov r0, r1, d5 \n"
\r
279 "bne reg1_error_loopf \n"
\r
281 "bne reg1_error_loopf \n"
\r
282 "vmov r0, r1, d6 \n"
\r
284 "bne reg1_error_loopf \n"
\r
286 "bne reg1_error_loopf \n"
\r
287 "vmov r0, r1, d7 \n"
\r
289 "bne reg1_error_loopf \n"
\r
291 "bne reg1_error_loopf \n"
\r
292 "vmov r0, r1, d8 \n"
\r
294 "bne reg1_error_loopf \n"
\r
296 "bne reg1_error_loopf \n"
\r
297 "vmov r0, r1, d9 \n"
\r
299 "bne reg1_error_loopf \n"
\r
301 "bne reg1_error_loopf \n"
\r
302 "vmov r0, r1, d10 \n"
\r
304 "bne reg1_error_loopf \n"
\r
306 "bne reg1_error_loopf \n"
\r
307 "vmov r0, r1, d11 \n"
\r
309 "bne reg1_error_loopf \n"
\r
311 "bne reg1_error_loopf \n"
\r
312 "vmov r0, r1, d12 \n"
\r
314 "bne reg1_error_loopf \n"
\r
316 "bne reg1_error_loopf \n"
\r
317 "vmov r0, r1, d13 \n"
\r
319 "bne reg1_error_loopf \n"
\r
321 "bne reg1_error_loopf \n"
\r
322 "vmov r0, r1, d14 \n"
\r
324 "bne reg1_error_loopf \n"
\r
326 "bne reg1_error_loopf \n"
\r
327 "vmov r0, r1, d15 \n"
\r
329 "bne reg1_error_loopf \n"
\r
331 "bne reg1_error_loopf \n"
\r
333 "/* Restore the registers that were clobbered by the test. */ \n"
\r
336 "/* VFP register test passed. Jump to the core register test. */ \n"
\r
337 "b reg1_loopf_pass \n"
\r
339 "reg1_error_loopf: \n"
\r
340 "/* If this line is hit then a VFP register value was found to be incorrect. */ \n"
\r
341 "b reg1_error_loopf \n"
\r
343 "reg1_loopf_pass: \n"
\r
346 "bne reg1_error_loop \n"
\r
348 "bne reg1_error_loop \n"
\r
350 "bne reg1_error_loop \n"
\r
352 "bne reg1_error_loop \n"
\r
354 "bne reg1_error_loop \n"
\r
356 "bne reg1_error_loop \n"
\r
358 "bne reg1_error_loop \n"
\r
360 "bne reg1_error_loop \n"
\r
362 "bne reg1_error_loop \n"
\r
364 "bne reg1_error_loop \n"
\r
366 "bne reg1_error_loop \n"
\r
368 "bne reg1_error_loop \n"
\r
370 "bne reg1_error_loop \n"
\r
372 "/* Everything passed, increment the loop counter. */ \n"
\r
373 "push { r0-r1 } \n"
\r
374 "ldr r0, =pulRegTest3LoopCounter \n"
\r
377 "adds r1, r1, #1 \n"
\r
381 "/* Start again. */ \n"
\r
384 "reg1_error_loop: \n"
\r
385 "/* If this line is hit then there was an error in a core register value. \n"
\r
386 "The loop ensures the loop counter stops incrementing. */ \n"
\r
387 "b reg1_error_loop \n"
\r
389 ); /* __asm volatile. */
\r
391 /*-----------------------------------------------------------*/
\r
393 void vRegTest4Implementation( void )
\r
397 ".extern pulRegTest4LoopCounter \n"
\r
398 "/* Set all the core registers to known values. */ \n"
\r
413 "/* Set all the VFP to known values. */ \n"
\r
414 "vmov d0, r0, r1 \n"
\r
415 "vmov d1, r2, r3 \n"
\r
416 "vmov d2, r4, r5 \n"
\r
417 "vmov d3, r6, r7 \n"
\r
418 "vmov d4, r8, r9 \n"
\r
419 "vmov d5, r10, r11 \n"
\r
420 "vmov d6, r0, r1 \n"
\r
421 "vmov d7, r2, r3 \n"
\r
422 "vmov d8, r4, r5 \n"
\r
423 "vmov d9, r6, r7 \n"
\r
424 "vmov d10, r8, r9 \n"
\r
425 "vmov d11, r10, r11 \n"
\r
426 "vmov d12, r0, r1 \n"
\r
427 "vmov d13, r2, r3 \n"
\r
428 "vmov d14, r4, r5 \n"
\r
429 "vmov d15, r6, r7 \n"
\r
433 "/* Check all the VFP registers still contain the values set above. \n"
\r
434 "First save registers that are clobbered by the test. */ \n"
\r
435 "push { r0-r1 } \n"
\r
437 "vmov r0, r1, d0 \n"
\r
439 "bne reg2_error_loopf \n"
\r
441 "bne reg2_error_loopf \n"
\r
442 "vmov r0, r1, d1 \n"
\r
444 "bne reg2_error_loopf \n"
\r
446 "bne reg2_error_loopf \n"
\r
447 "vmov r0, r1, d2 \n"
\r
449 "bne reg2_error_loopf \n"
\r
451 "bne reg2_error_loopf \n"
\r
452 "vmov r0, r1, d3 \n"
\r
454 "bne reg2_error_loopf \n"
\r
456 "bne reg2_error_loopf \n"
\r
457 "vmov r0, r1, d4 \n"
\r
459 "bne reg2_error_loopf \n"
\r
461 "bne reg2_error_loopf \n"
\r
462 "vmov r0, r1, d5 \n"
\r
464 "bne reg2_error_loopf \n"
\r
466 "bne reg2_error_loopf \n"
\r
467 "vmov r0, r1, d6 \n"
\r
469 "bne reg2_error_loopf \n"
\r
471 "bne reg2_error_loopf \n"
\r
472 "vmov r0, r1, d7 \n"
\r
474 "bne reg2_error_loopf \n"
\r
476 "bne reg2_error_loopf \n"
\r
477 "vmov r0, r1, d8 \n"
\r
479 "bne reg2_error_loopf \n"
\r
481 "bne reg2_error_loopf \n"
\r
482 "vmov r0, r1, d9 \n"
\r
484 "bne reg2_error_loopf \n"
\r
486 "bne reg2_error_loopf \n"
\r
487 "vmov r0, r1, d10 \n"
\r
489 "bne reg2_error_loopf \n"
\r
491 "bne reg2_error_loopf \n"
\r
492 "vmov r0, r1, d11 \n"
\r
494 "bne reg2_error_loopf \n"
\r
496 "bne reg2_error_loopf \n"
\r
497 "vmov r0, r1, d12 \n"
\r
499 "bne reg2_error_loopf \n"
\r
501 "bne reg2_error_loopf \n"
\r
502 "vmov r0, r1, d13 \n"
\r
504 "bne reg2_error_loopf \n"
\r
506 "bne reg2_error_loopf \n"
\r
507 "vmov r0, r1, d14 \n"
\r
509 "bne reg2_error_loopf \n"
\r
511 "bne reg2_error_loopf \n"
\r
512 "vmov r0, r1, d15 \n"
\r
514 "bne reg2_error_loopf \n"
\r
516 "bne reg2_error_loopf \n"
\r
518 "/* Restore the registers that were clobbered by the test. */ \n"
\r
521 "/* VFP register test passed. Jump to the core register test. */ \n"
\r
522 "b reg2_loopf_pass \n"
\r
524 "reg2_error_loopf: \n"
\r
525 "/* If this line is hit then a VFP register value was found to be \n"
\r
527 "b reg2_error_loopf \n"
\r
529 "reg2_loopf_pass: \n"
\r
532 "bne reg2_error_loop \n"
\r
534 "bne reg2_error_loop \n"
\r
536 "bne reg2_error_loop \n"
\r
538 "bne reg2_error_loop \n"
\r
540 "bne reg2_error_loop \n"
\r
542 "bne reg2_error_loop \n"
\r
544 "bne reg2_error_loop \n"
\r
546 "bne reg2_error_loop \n"
\r
548 "bne reg2_error_loop \n"
\r
550 "bne reg2_error_loop \n"
\r
552 "bne reg2_error_loop \n"
\r
554 "bne reg2_error_loop \n"
\r
556 "bne reg2_error_loop \n"
\r
558 "/* Increment the loop counter so the check task knows this task is \n"
\r
559 "still running. */ \n"
\r
560 "push { r0-r1 } \n"
\r
561 "ldr r0, =pulRegTest4LoopCounter \n"
\r
564 "adds r1, r1, #1 \n"
\r
568 "/* Yield to increase test coverage. */ \n"
\r
571 "/* Start again. */ \n"
\r
574 "reg2_error_loop: \n"
\r
575 "/* If this line is hit then there was an error in a core register value. \n"
\r
576 "This loop ensures the loop counter variable stops incrementing. */ \n"
\r
577 "b reg2_error_loop \n"
\r
578 ); /* __asm volatile */
\r
580 /*-----------------------------------------------------------*/
\r
582 /* Fault handlers are here for convenience as they use compiler specific syntax
\r
583 and this file is specific to the GCC compiler. */
\r
584 void hard_fault_handler( uint32_t * hardfault_args )
\r
586 volatile uint32_t stacked_r0;
\r
587 volatile uint32_t stacked_r1;
\r
588 volatile uint32_t stacked_r2;
\r
589 volatile uint32_t stacked_r3;
\r
590 volatile uint32_t stacked_r12;
\r
591 volatile uint32_t stacked_lr;
\r
592 volatile uint32_t stacked_pc;
\r
593 volatile uint32_t stacked_psr;
\r
595 stacked_r0 = ((uint32_t) hardfault_args[ 0 ]);
\r
596 stacked_r1 = ((uint32_t) hardfault_args[ 1 ]);
\r
597 stacked_r2 = ((uint32_t) hardfault_args[ 2 ]);
\r
598 stacked_r3 = ((uint32_t) hardfault_args[ 3 ]);
\r
600 stacked_r12 = ((uint32_t) hardfault_args[ 4 ]);
\r
601 stacked_lr = ((uint32_t) hardfault_args[ 5 ]);
\r
602 stacked_pc = ((uint32_t) hardfault_args[ 6 ]);
\r
603 stacked_psr = ((uint32_t) hardfault_args[ 7 ]);
\r
605 /* Inspect stacked_pc to locate the offending instruction. */
\r
608 ( void ) stacked_psr;
\r
609 ( void ) stacked_pc;
\r
610 ( void ) stacked_lr;
\r
611 ( void ) stacked_r12;
\r
612 ( void ) stacked_r0;
\r
613 ( void ) stacked_r1;
\r
614 ( void ) stacked_r2;
\r
615 ( void ) stacked_r3;
\r
617 /*-----------------------------------------------------------*/
\r
619 void HardFault_Handler( void ) __attribute__((naked));
\r
620 void HardFault_Handler( void )
\r
626 " mrseq r0, msp \n"
\r
627 " mrsne r0, psp \n"
\r
628 " ldr r1, [r0, #24] \n"
\r
629 " ldr r2, handler_address_const \n"
\r
631 " handler_address_const: .word hard_fault_handler \n"
\r
634 /*-----------------------------------------------------------*/
\r
636 void MemManage_Handler( void ) __attribute__((naked));
\r
637 void MemManage_Handler( void )
\r
643 " mrseq r0, msp \n"
\r
644 " mrsne r0, psp \n"
\r
645 " ldr r1, [r0, #24] \n"
\r
646 " ldr r2, handler2_address_const \n"
\r
648 " handler2_address_const: .word hard_fault_handler \n"
\r
650 }/*-----------------------------------------------------------*/
\r