1 /*
2  *            ThreadX API Runtime Error Support
3  *
4  *      Copyright 1983-2019 Green Hills Software LLC.
5  *
6  *  This program is the property of Green Hills Software LLC.,
7  *  its contents are proprietary information and no part of it
8  *  is to be disclosed to anyone except employees of Green Hills
9  *  Software LLC., or as agreed in writing signed by the President
10  *  of Green Hills Software LLC.
11  */
12 
13 /* #include "tx_ghs.h" */
14 #ifndef TX_DISABLE_ERROR_CHECKING
15 #define TX_DISABLE_ERROR_CHECKING
16 #endif
17 #include "tx_api.h"
18 
19 /* Customized ThreadX API runtime error support routine. */
20 
21 void _rnerr(int num, int linenum, const char*str, void*ptr, ...);
22 
23 /* __ghs_rnerr()
24    This is the custom runtime error checking routine.
25    This implementation uses the existing __rnerr() routine.
26    Another implementation could use the .syscall mechanism,
27    provided MULTI was modified to understand that.
28  */
__ghs_rnerr(char * errMsg,int stackLevels,int stackTraceDisplay,void * hexVal)29 void __ghs_rnerr(char *errMsg, int stackLevels, int stackTraceDisplay, void *hexVal) {
30     TX_INTERRUPT_SAVE_AREA
31     int num;
32     /*
33        Initialize the stack levels value.
34 
35        Add 3 to account for the calls to _rnerr, __rnerr, and
36        __ghs_rnerr.
37 
38        If the implementation changes, calls to __ghs_rnerr
39        will not need to be changed.
40 
41        Zero is not permitted, so substitute 3 in that case.
42        */
43     num = (stackLevels+3) & 0xf;
44     if (!num) {
45         num = 3;
46     }
47     /*
48        Shift the stack levels value to bits 12..15 and
49        insert the stack trace display value in bit 11.
50        Bits 0..10 are unused.
51      */
52     num = (num << 12) | (stackTraceDisplay ? 0x800 : 0);
53 
54     /* This will mask all interrupts in the RTEC code, which is probably
55        unacceptable for many targets. */
56     TX_DISABLE
57     _rnerr(num, -1, (const char *)hexVal, (void *)errMsg);
58     TX_RESTORE
59 }
60 
61 
62 /* ThreadX thread stack checking runtime support routine. */
63 
64 extern char              __ghsbegin_stack[];
65 
__stkchk(void)66 void __stkchk(void) {
67     int i;
68     TX_THREAD   *current_thread_ptr;
69 
70     /* Pickup current thread pointer.  */
71     TX_THREAD_GET_CURRENT(current_thread_ptr)
72 
73     if(current_thread_ptr)
74     {
75         if((unsigned)(&i) <=
76                 (unsigned)(current_thread_ptr -> tx_thread_stack_start))
77         {
78             _rnerr(21, -1, 0, 0);
79         }
80     }
81     else
82     {
83         if((unsigned)(&i) <= (unsigned)__ghsbegin_stack)
84         {
85             _rnerr(21, -1, 0, 0);
86         }
87     }
88 }
89