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 extern TX_THREAD        *_tx_thread_current_ptr;
66 
__stkchk(void)67 void __stkchk(void) {
68     int i;
69     if(_tx_thread_current_ptr)
70     {
71         if((unsigned)(&i) <=
72                 (unsigned)(_tx_thread_current_ptr -> tx_thread_stack_start))
73         {
74             _rnerr(21, -1, 0, 0);
75         }
76     }
77     else
78     {
79         if((unsigned)(&i) <= (unsigned)__ghsbegin_stack)
80         {
81             _rnerr(21, -1, 0, 0);
82         }
83     }
84 }
85